[BUG-975] Vision: Web Browser: PopUp

move the code from line 44 up to right after your includes (but leave line 46 where it’s at)

line 34 (as well as one of the lines above that which reference browser) might be failing due to those also needing browser to be defined.

if it still fails after that, what error is displayed?

1 Like

this error:

right after you assign the browser variable to event.source.getBrowser()

do an:

if browser:

and tab the rest of your code over.

1 Like

The propertyChange event is firing before the browser component has loaded; it’s loaded async because it’s such a heavyweight object. You’ll probably have to use invokeLater; there’s no property change event to hook into that will fire once the browser is ready.

if event.propertyName == 'componentRunning':
	def addPopupHandler():
		from javax.swing import JFrame
		from java.awt.event import WindowAdapter
		import java.awt.Dimension
		from com.teamdev.jxbrowser.chromium.events import DisposeListener
		from com.teamdev.jxbrowser.chromium.swing import BrowserView
		from com.teamdev.jxbrowser.chromium import PopupContainer, PopupHandler

		
		class MyWindowAdapter(WindowAdapter):
			def windowClosing(self, e):
				browser.dispose()

		class MyDisposeListener(DisposeListener):
			def __init__(self, frame):
				self.frame = frame

			def onDisposed(self, event):
				self.frame.setVisible(False)

		def invokedLater():
			BrowserView.browserView = BrowserView(event.source.browser)
			browserView.setPreferredSize(initialBounds.getSize())
			frame = JFrame("Popup")
			frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
			frame.add(browserView, BorderLayout.CENTER)
			frame.pack()
			frame.setLocation(initialBounds.getLocation())
			frame.setVisible(True)
			#frame.setSize(700,500)
			#frame.setPreferredSize(1365, 768)
			frame.addWindowListener(MyWindowAdapter())

			browser.addDisposeListener(MyDisposeListener(frame))

		class MyPopupContainer(PopupContainer):
			def insertBrowser(self, browser, initialBounds):
				system.gui.invokeLater(invokedLater)

		class MyPopupHandler(PopupHandler):
			def handlePopup(self, params):
				return MyPopupContainer()

		event.source.browser.setPopupHandler(MyPopupHandler())

	system.util.invokeLater(addPopupHandler, 5000)
1 Like

I replaced spaces to tabs and I get error…

I updated my code to use tabs; try again.

1 Like

Thanx!

but see what error I get:


Try making the 5,000 some larger value, like 30,000; it’s just an arbitrary threshold to give your client time to load the web browser component.

1 Like

I still get an error((

and… how to set width and height? I tried:

frame.setPreferredSize(1365, 768)
frame.setSize(700,500)
browserView.setPreferredSize(Dimension(300,300))

nothing helped

it’s strange, but after 10 seconds script just freezes the webbrowser, so I can’t click any button in it…

if event.propertyName == 'componentRunning':
	def addPopupHandler():
		from javax.swing import JFrame, WindowConstants
		from java.awt.event import WindowAdapter
		from java.awt import Dimension, BorderLayout
		from com.teamdev.jxbrowser.chromium.events import DisposeListener
		from com.teamdev.jxbrowser.chromium.swing import BrowserView
		from com.teamdev.jxbrowser.chromium import PopupContainer, PopupHandler
		from functools import partial

		class MyWindowAdapter(WindowAdapter):
			def __init__(self, browser):
				self.browser = browser
				
			def windowClosing(self, e):
				self.browser.dispose()

		def showPopup(browser, initialBounds):
			browserView = BrowserView(browser)
			browserView.setPreferredSize(initialBounds.getSize())
			frame = JFrame("Popup")
			frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
			frame.add(browserView, BorderLayout.CENTER)
			frame.pack()
			frame.setLocation(initialBounds.getLocation())
			frame.setVisible(True)
			frame.setMinimumSize(Dimension(1365, 768))
			frame.addWindowListener(MyWindowAdapter(browser))

		class MyPopupContainer(PopupContainer):
			def insertBrowser(self, browser, initialBounds):
				system.util.invokeLater(partial(showPopup, browser, initialBounds))

		class MyPopupHandler(PopupHandler):
			def handlePopup(self, params):
				return MyPopupContainer()

		event.source.browser.setPopupHandler(MyPopupHandler())

	system.util.invokeLater(addPopupHandler, 5000)

Worked for me. It’ll get a little race-ey if you log in and log out too fast; the invokeLater may end up firing at a bad time, but this should work.

2 Likes

OMG! It works, but I still get an error…
image

so…
I did that:

		try:
			browser.setPopupHandler(MyPopupHandler())
		except:
			1

and it gives no error…
I am freaky happy!

THANX GUYS SO MUCH FOR HELP!!! :heart_eyes: :smiling_face_with_three_hearts: :kissing_closed_eyes:

2 Likes

glad you got it working!!

1 Like

Ignition version 8.1.0
I use perspective page via Vision Web Browser (as said before here), so I have a problem with popups at all… they don’t open… when I press the button nothing’s happen…

I pasted Mark Down element to the page and did insert this kind of code to the source property:

'<html><head><style></style></head><body><a href="www.google.com" target="_blank">test</a></body></html>'

image
so… the “test” link doesn’t work… popup window doesn’t open…

any suggestions? what do I do wrong?

Is there any solution?

Link’s property target:_blank doesn’t work as well

Popup Zulu opens only in Designer Perspective module… :tipping_hand_woman:

I don’t think you’re doing anything wrong, it’s a bug with the web browser module that should be fixed in the coming days.

1 Like

The fix is scheduled to go into 8.1.1. for now you could do something like this which borrows from the previous examples. I would recommend waiting for the 8.1.1 fix, but if you need to do some testing now:

if event.propertyName == 'componentRunning':
	def addPopupHandler():
		from javax.swing import JFrame, WindowConstants
		from java.awt.event import WindowAdapter
		from java.awt import Dimension, BorderLayout

		from com.teamdev.jxbrowser.view.swing import BrowserView
		from com.teamdev.jxbrowser.browser.callback import CreatePopupCallback, OpenPopupCallback

		from functools import partial

		class MyWindowAdapter(WindowAdapter):
			def __init__(self, browser):
				self.browser = browser
				
			def windowClosing(self, e):
				self.browser.close()


		def showPopup(browser, initialBounds):

			# Initialize our browser view
			browserView = BrowserView.newInstance(browser)
			browserView.setPreferredSize(Dimension(initialBounds.width(), initialBounds.height()))

			# Create a new frame
			frame = JFrame("Popup")
			frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
			frame.add(browserView, BorderLayout.CENTER)
			frame.pack()
			frame.setLocation(initialBounds.x(), initialBounds.y())
			frame.setVisible(True)
			frame.setMinimumSize(Dimension(1365, 768))
			frame.addWindowListener(MyWindowAdapter(browser))

		#
		# A Callback to allow the creation of a popup
		#
		class AllowPopupCreation(CreatePopupCallback):
			def on(self, params):
				return CreatePopupCallback.Response.create()

		#
		# An OpenPopupCallback which actually performs the opening of the popup
		#
		class MyPopupHandler(OpenPopupCallback):
			def on(self, params):
				popupBrowser = params.popupBrowser()
				initialBounds = params.initialBounds()
				system.util.invokeLater(partial(showPopup, popupBrowser, initialBounds))
				return OpenPopupCallback.Response.proceed()


		event.source.browser.set(CreatePopupCallback, AllowPopupCreation())
		event.source.browser.set(OpenPopupCallback, MyPopupHandler())

	system.util.invokeLater(addPopupHandler, 5000)
1 Like

thanx!
I will wait for newest version of Ignition…
For now we have another solution for this problem…

This is included in the nightly builds as of now and will be included in the 8.1.1 release

1 Like

Thanx! :hugs: