Взаимодействие pywebkitgtk и javascript

Сабж можно осуществить c помощью дополнительной библиотеки: pyjavascriptcore
Есть сборки для убунты в ppa
Пакеты будут в следующих версиях ubuntu и debian


Пользоваться примерно так:
$ import javascriptcore

$ cx=javascriptcore.JSContext(frame.get_global_context())
cx.globalObject.document.write('test')
$ func=cx.evaluateScript('window.foo=function(a){return a*2;}')
$ cx.globalObject.window.foo
Out: <javascriptcore.JSBoundMethod object at 0x94e84dc>
$ func(6)
Out: 12.0


class MyWebView(webkit.WebView):

def get_selection(self, callback, *args):
self.eval('return document.getSelection()', callback, *args)

def _got_response_cb(self, view, msg, n, something, callback, *args):
self.disconnect(self._got_response)
callback(msg.strip(), *args)
view.stop_emission('console-message')

def eval(self, js, callback, *args):
self._got_response = self.connect("console-message", self._got_response_cb, callback, *args)
self.execute_script('console.log((function(){%s\n})());' % js)



Потом наткнулся на более простой способ в интернетах:

class MyWebView(webkit.WebView):

def get_selection(self):
self.execute_script('oldtitle=document.title;document.title=document.getSelection();')
sel = self.get_main_frame().get_title().strip()
self.execute_script('document.title=oldtitle;oldtitle=null;')
if sel == self.title:
return
return sel
Add post to:   Delicious Reddit Slashdot Digg Technorati Google
Make comment

Comments

No comments for this post

Required. 30 chars of fewer.

Required.

captcha image Please, enter symbols, which you see on the image