[Twisted-Python] can QtWebKit fire a twisted deferred?

Kevin Mcintyre kebin70 at gmail.com
Wed May 6 19:24:01 MDT 2015


That was pseudo code, I've been using qt5reactor to happy results.  All the
same good to know on deferreds working in non-twisted code...

My challenge is having only 2 hooks into the DOM - javaScriptAlert and
javaScriptConfirm that ultimately with QtWebEngine aren't available.

thank for the feedback.

On Wed, May 6, 2015 at 3:35 PM, Glyph <glyph at twistedmatrix.com> wrote:

>
> On May 6, 2015, at 11:04 AM, Kevin Mcintyre <kebin70 at gmail.com> wrote:
>
> '''
> I know this is meant for pyqt but I've had little luck this that board.
>
> The short answer is yes, but it's a total hack...and I'm wondering if
> anybody has a more elegant solution.
>
> Specifically I would like to be able to fire a defer from the javascript
> instead of having to call alert.
>
> Below is an example of how I currently gain access...not necessarily a
> working version.
> '''
>
>
> I don't see any import of qt4reactor or Twisted (other than the implied
> import of Deferred) in this code.  Firing a Deferred in this way should be
> totally fine though; you don't actually need the reactor for Deferreds to
> work (it's just important that if it's going to interact with any *other* Twisted
> code it needs to run on the main thread).
>
> Sorry I don't have a better suggestion, but I don't see much wrong with
> what you're doing here :).
>
> -g
>
> url = 'http://www.google.com'
>
> mo = """
>     var observer = new MutationObserver(function(mutations) {
>         for (var x = 0; x < mutations.length; x++) {
>             if (mutations[x].addedNodes.length > 0) {
>                 alert('add nodes')
>             }
>         }
>     });
>     var config = { attributes: true, childList: true, characterData:
> false }
>     var nti = document.querySelector('document.body')
>     observer.observe(nti, config);
> """
>
> page = QWebPage()
> def alarmAction(qframe, msg):
>     print 'deferred action', qframe, msg
> def mutationHook():
>     d = defer.Deferred()
>     d.addCallback(alarmAction)
>     page.javaScriptAlert = d.callback
>     page.mainFrame().documentElement().evaluateJavaScript(mo)
>
>
> page.loadFinished(mutationHook)
> page.mainFrame().setUrl(url)
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20150506/1c91139c/attachment-0002.html>


More information about the Twisted-Python mailing list