Permalink
Browse files

Add navigation event hook to PhantomJS.

  • Loading branch information...
1 parent c153a28 commit 40fd210c99568aa013947925dac3c37f3d9134b1 @davidrapin davidrapin committed with Jun 15, 2012
Showing with 59 additions and 0 deletions.
  1. +4 −0 src/modules/webpage.js
  2. +48 −0 src/webpage.cpp
  3. +7 −0 src/webpage.h
View
@@ -117,6 +117,10 @@ exports.create = function (opts) {
defineSetter("onLoadFinished", "loadFinished");
+ defineSetter("onUrlChanged", "urlChanged");
+
+ defineSetter("onNavigationRequested", "navigationRequested");
+
defineSetter("onResourceRequested", "resourceRequested");
defineSetter("onResourceReceived", "resourceReceived");
View
@@ -139,6 +139,41 @@ public slots:
return m_userAgent;
}
+ bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type) {
+ bool isMainFrame = (frame == m_webPage->m_mainFrame);
+ // check for all frames (including iframes)
+ //if (frame == m_webPage->m_mainFrame) {
+ QString navigation = "Undefined";
+ switch (type) {
+ case NavigationTypeLinkClicked:
+ navigation = "LinkClicked";
+ break;
+ case NavigationTypeFormSubmitted:
+ navigation = "FormSubmitted";
+ break;
+ case NavigationTypeBackOrForward:
+ navigation = "BackOrForward";
+ break;
+ case NavigationTypeReload:
+ navigation = "Reload";
+ break;
+ case NavigationTypeFormResubmitted:
+ navigation = "FormResubmitted";
+ break;
+ case NavigationTypeOther:
+ navigation = "Other";
+ break;
+ }
+
+ emit m_webPage->navigationRequested(request.url(), navigation, !m_webPage->navigationLocked(), isMainFrame);
+
+ return !m_webPage->navigationLocked();
+ //} else {
+ // return true;
+ //}
+ }
+
+
private:
WebPage *m_webPage;
QString m_userAgent;
@@ -208,6 +243,7 @@ public slots:
WebPage::WebPage(QObject *parent, const Config *config, const QUrl &baseUrl)
: REPLCompletable(parent)
, m_callbacks(NULL)
+ , m_navigationLocked(false)
{
setObjectName("WebPage");
m_webPage = new CustomPage(this);
@@ -216,6 +252,7 @@ WebPage::WebPage(QObject *parent, const Config *config, const QUrl &baseUrl)
connect(m_mainFrame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(registerCallbacksHolder()));
connect(m_mainFrame, SIGNAL(javaScriptWindowObjectCleared()), SIGNAL(initialized()));
+ connect(m_mainFrame, SIGNAL(urlChanged(QUrl)), SIGNAL(urlChanged(QUrl)));
connect(m_webPage, SIGNAL(loadStarted()), SIGNAL(loadStarted()), Qt::QueuedConnection);
connect(m_webPage, SIGNAL(loadFinished(bool)), SLOT(finish(bool)), Qt::QueuedConnection);
@@ -335,6 +372,17 @@ QString WebPage::userAgent() const
return m_webPage->m_userAgent;
}
+void WebPage::setNavigationLocked(bool lock)
+{
+ m_navigationLocked = lock;;
+}
+
+bool WebPage::navigationLocked()
+{
+ return m_navigationLocked;
+}
+
+
void WebPage::setViewportSize(const QVariantMap &size)
{
int w = size.value("width").toInt();
View
@@ -57,6 +57,7 @@ class WebPage: public REPLCompletable, public QWebFrame::PrintCallback
Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize)
Q_PROPERTY(QVariantMap clipRect READ clipRect WRITE setClipRect)
Q_PROPERTY(QVariantMap scrollPosition READ scrollPosition WRITE setScrollPosition)
+ Q_PROPERTY(bool navigationLocked READ navigationLocked WRITE setNavigationLocked)
Q_PROPERTY(QVariantMap customHeaders READ customHeaders WRITE setCustomHeaders)
Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
@@ -89,6 +90,9 @@ class WebPage: public REPLCompletable, public QWebFrame::PrintCallback
void setPaperSize(const QVariantMap &size);
QVariantMap paperSize() const;
+ void setNavigationLocked(bool lock);
+ bool navigationLocked();
+
void setCustomHeaders(const QVariantMap &headers);
QVariantMap customHeaders() const;
@@ -128,6 +132,8 @@ public slots:
void javaScriptErrorSent();
void resourceRequested(const QVariant &req);
void resourceReceived(const QVariant &resource);
+ void urlChanged(const QUrl &url);
+ void navigationRequested(const QUrl &url, const QString &navigationType, bool navigationLocked, bool isMainFrame);
private slots:
void finish(bool ok);
@@ -159,6 +165,7 @@ private slots:
QString m_libraryPath;
QWebInspector* m_inspector;
WebpageCallbacks *m_callbacks;
+ bool m_navigationLocked;
friend class Phantom;
friend class CustomPage;

2 comments on commit 40fd210

Collaborator

detro replied Jun 15, 2012

Oh, I missed this pull request: this is really cool!
It's a very important set of events to have!!!

Good work David! :)

Contributor

davidrapin replied Jun 15, 2012

Thanks Ivan ;)

Please sign in to comment.