Is there a way for wxpython to detect javascript mouse events in wx.html2.WebView?

I have the following python script

import wx, os

import wx.html2

class MyBrowser(wx.Dialog):

def init(self, *args, **kwds):

wx.Dialog.init(self, *args, **kwds)

sizer = wx.BoxSizer(wx.VERTICAL)

self.browser = wx.html2.WebView.New(self)

sizer.Add(self.browser, 1, wx.EXPAND, 10)

self.SetSizer(sizer)

self.SetSize((700, 700))

if name == ‘main’:

app = wx.App()

dialog = MyBrowser(None, -1)

dialog.browser.LoadURL(os.path.join(os.getcwd(), ‘test.htm’))

dialog.Show()

app.MainLoop()

And a sample test.htm script

Click here

Is there a way for a python function to be bound to the “click”?

However, if it's not in place, and if you really want a lot of JS-Python
interaction, I'd suggest sticking with the web app paradigm -- i.e.
communicate between your python app and the browser window via http in the
usual way:

In a separate thread (probably), start up a simple python=based http server
( all the web app frameworks have one -- I'd probably use one of the
lighter-weight ones -- tornado, flaks, etc...). You can have it bound to
localhost port_something_high.

Then point html2 at it. You can then use Javascript to make requests in the
normal javascript way.

Note that if your web-app component is running in a separate thread, you'll
need to use a thread-safe way to call wx code in the main thread --
wx.CallAfter() will suffice for most needs.

-Chris

···

On Fri, Feb 7, 2014 at 1:18 AM, RedHotChiliPepper < sharrukinjosephson@gmail.com> wrote:

I have the following python script
And a sample test.htm script

<!DOCTYPE html>
<html>
<body>
<h1 onclick="alert('hello')">Click here</h1>
</body>
</html>

Is there a way for a python function to be bound to the "click"?

Interesting -- that would be pretty cool.

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Do you have control of the content in the html file?

You can catch clicks by capturing and vetoing a window navigation attempt with
a key string embedded in the location: With a small change to your html file, and
capturing the EVT_WEB_VIEW_NAVIGATING event, we have this python program:

import wx, os
import wx.html2

class MyBrowser(wx.Dialog):
def init(self, *args, **kwds):
wx.Dialog.init(self, *args, **kwds)
sizer = wx.BoxSizer(wx.VERTICAL)
self.browser = wx.html2.WebView.New(self)
sizer.Add(self.browser, 1, wx.EXPAND, 10)
self.SetSizer(sizer)
self.SetSize((700, 700))
self.Bind(wx.html2.EVT_WEB_VIEW_NAVIGATING, self.OnNavigate,self.browser)

CATCH_STRING = “catch-this-click:”

def OnNavigate(self,evt):
targetUrl=evt.GetURL()
#print "Navigate Target: ",targetUrl

if self.CATCH_STRING in targetUrl:
  passed_data = targetUrl.split(self.CATCH_STRING,2)[1]
  print "Click Caught!  Data = ",passed_data
  evt.Veto() # prevent actual navigation.

else:
  pass # let it do the normal thing...

if name == ‘main’:
app = wx.App()
dialog = MyBrowser(None, -1)
url = os.path.join(os.getcwd(), ‘test2.htm’)
print "loading url ",url
dialog.browser.LoadURL(url)
dialog.Show()
app.MainLoop()

and this test file test2.htm:

Click here

Will that be enough for your needs?

···

On Friday, February 7, 2014 4:18:40 AM UTC-5, RedHotChiliPepper wrote:

I have the following python script

Is there a way for a python function to be bound to the “click”?