PDA

View Full Version : [QtWebKit] Inject JavaScript properly when DOM loads..



fab_74
27th March 2011, 13:17
Hi there :)

I'd like to make a web browser that can integrate an extension like Greasemonkey (using custom JavaScript scripts).

I already know about evaluateJavaScript(const QString &scriptSource), but there's... a problem.

Indeed, I can't inject JS properly. If I call this function using the signal loadStarted(), the JavaScript will be injected before page is rendered, means it won't wok at all. If I call it using the signal loadFinished(), it will be way too slow! Especially when you have a slow internet, like myself...
The only solution I found is using loadProgress(int), and injecting script around 70% of loading, but it's slow and not accurate.

I need to find a signal that can tell me if DOM is loaded, like greasemonkey does...

Do you have any ideas? Thank you in advance.

wysota
27th March 2011, 16:56
In my opinion you can't inject the script before the page is fully loaded as the script can reference part of the document that is not loaded yet. You want the script to execute "onLoad", you don't need it earlier. In other words the resources may not be loaded yet but the raw page body has to be fully present (and before onLoad is triggered). You can either intercept the page content before it goes into webkit and inject a onLoad attribute to the body element or simply execute your work with the loadFinished() signal or QWebFrame::initialLayoutCompleted() signal. I'm lazy and I'm triggering my functionality with loadFinished().

fab_74
27th March 2011, 17:58
May you live forever. That was EXACTLY what I wanted. It works perfectly with initialLayoutCompleted(). I don't know how I can thank you... thank you so much! ;) ;) ;)