From 75403737c4a7c12eaf3573eaa9fca7bbce5be701 Mon Sep 17 00:00:00 2001 From: Ivan De Marino Date: Thu, 9 Jun 2011 16:11:39 +0100 Subject: [PATCH] Added "includeJs()" to "page". * It includes a script in the page * It uses a callback to ensure any code dependent on the include runs afterwards * It uses the signal "javaScriptAlertSent" to do the trick (is there another way to be notified of the "onLoad" event from outside the page context?) * It uses a "private" slot "_appendScriptElement" to pass the script url in the page context (is there a better way?) --- examples/phantomwebintro.js | 19 +++++++++++++++++++ src/bootstrap.js | 17 +++++++++++++++++ src/consts.h | 5 +++++ src/webpage.cpp | 4 ++++ src/webpage.h | 1 + 5 files changed, 46 insertions(+) create mode 100644 examples/phantomwebintro.js diff --git a/examples/phantomwebintro.js b/examples/phantomwebintro.js new file mode 100644 index 00000000..062a51dd --- /dev/null +++ b/examples/phantomwebintro.js @@ -0,0 +1,19 @@ +// Read the Phantom webpage '#intro' element text using jQuery and "includeJs" + +var page = new WebPage(); + +page.onConsoleMessage = function(msg) { + console.log(msg); +}; + +page.open("http://www.phantomjs.org", function(status) { + if ( status === "success" ) { + page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() { + page.evaluate(function() { + console.log("$(\"#intro\").text() -> " + $("#intro").text()); + }); + phantom.exit(); + }); + } +}); + diff --git a/src/bootstrap.js b/src/bootstrap.js index e9126dca..daa5f46d 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -83,5 +83,22 @@ window.WebPage = function() { throw "Wrong use of WebPage#open"; }; + page.includeJs = function(scriptUrl, onScriptLoaded) { + // Register temporary signal handler for 'alert()' + this.javaScriptAlertSent.connect(function(msgFromAlert) { + if ( msgFromAlert === scriptUrl ) { + // Resource loaded, time to fire the callback + onScriptLoaded(scriptUrl); + // And disconnect the signal handler + try { + this.javaScriptAlertSent.disconnect(this); + } catch (e) {} + } + }); + + // Append the script tag to the body + this._appendScriptElement(scriptUrl); + }; + return page; } diff --git a/src/consts.h b/src/consts.h index 86079ac5..6708791c 100644 --- a/src/consts.h +++ b/src/consts.h @@ -42,4 +42,9 @@ "el.dispatchEvent(ev);" \ "})(this);" +#define JS_APPEND_SCRIPT_ELEMENT "var el = document.createElement('script');" \ + "el.onload = function() { alert('%1'); };" \ + "el.src = '%1';" \ + "document.body.appendChild(el);" + #endif // CONSTS_H diff --git a/src/webpage.cpp b/src/webpage.cpp index 3041631d..13979919 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -514,3 +514,7 @@ bool WebPage::injectJs(const QString &jsFilePath) { } return false; } + +void WebPage::_appendScriptElement(const QString &scriptUrl) { + m_mainFrame->evaluateJavaScript( QString(JS_APPEND_SCRIPT_ELEMENT).arg(scriptUrl) ); +} diff --git a/src/webpage.h b/src/webpage.h index d1714ad0..24be48d4 100644 --- a/src/webpage.h +++ b/src/webpage.h @@ -73,6 +73,7 @@ public slots: QVariant evaluate(const QString &code); bool render(const QString &fileName); bool injectJs(const QString &jsFilePath); + void _appendScriptElement(const QString &scriptUrl); // moc does not understand QT_VERSION_CHECK and hence the encoded hex #if QT_VERSION >= 0x040600