diff --git a/ChangeLog b/ChangeLog index 386f1ab1..5f44286e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,7 @@ Version 1.3.0 New features * Presistent cookie support using --cookies=cookies.ini + * Added callback for page initialization (issue 143) Examples diff --git a/examples/unrandomize.js b/examples/unrandomize.js new file mode 100644 index 00000000..b1bcab25 --- /dev/null +++ b/examples/unrandomize.js @@ -0,0 +1,24 @@ +// Modify global object at the page initialization. +// In this example, effectively Math.random() always returns 0.42. + +var page = new WebPage(); + +page.onInitialized = function () { + page.evaluate(function () { + Math.random = function() { + return 42 / 100; + }; + }); +}; + +page.open('http://ariya.github.com/js/random/', function (status) { + var result; + if (status !== 'success') { + console.log('Network error.'); + } else { + console.log(page.evaluate(function () { + return document.getElementById('numbers').textContent; + })); + } + phantom.exit(); +}); diff --git a/src/bootstrap.js b/src/bootstrap.js index 229457d7..16d82852 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -19,6 +19,8 @@ window.WebPage = function() { // deep copy page.settings = JSON.parse(JSON.stringify(phantom.defaultPageSettings)); + defineSetter("onInitialized", "initialized"); + defineSetter("onLoadStarted", "loadStarted"); defineSetter("onLoadFinished", "loadFinished"); diff --git a/src/webpage.cpp b/src/webpage.cpp index 2944bd5e..0c96a685 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -104,6 +104,7 @@ WebPage::WebPage(QObject *parent) m_webPage = new CustomPage(this); m_mainFrame = m_webPage->mainFrame(); + connect(m_mainFrame, SIGNAL(javaScriptWindowObjectCleared()), SIGNAL(initialized())); connect(m_webPage, SIGNAL(loadStarted()), SIGNAL(loadStarted())); connect(m_webPage, SIGNAL(loadFinished(bool)), SLOT(finish(bool))); diff --git a/src/webpage.h b/src/webpage.h index 83c13056..8be197c8 100644 --- a/src/webpage.h +++ b/src/webpage.h @@ -86,6 +86,7 @@ public slots: void mouseMoveTo(int x, int y); signals: + void initialized(); void loadStarted(); void loadFinished(const QString &status); void javaScriptAlertSent(const QString &msg);