Skip to content
This repository
Browse code

Add navigation event hook to PhantomJS.

  • Loading branch information...
commit 40fd210c99568aa013947925dac3c37f3d9134b1 1 parent c153a28
david authored June 14, 2012 ariya committed June 14, 2012
4  src/modules/webpage.js
@@ -117,6 +117,10 @@ exports.create = function (opts) {
117 117
 
118 118
     defineSetter("onLoadFinished", "loadFinished");
119 119
 
  120
+    defineSetter("onUrlChanged", "urlChanged");
  121
+
  122
+    defineSetter("onNavigationRequested", "navigationRequested");
  123
+
120 124
     defineSetter("onResourceRequested", "resourceRequested");
121 125
 
122 126
     defineSetter("onResourceReceived", "resourceReceived");
48  src/webpage.cpp
@@ -139,6 +139,41 @@ public slots:
139 139
         return m_userAgent;
140 140
     }
141 141
 
  142
+    bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type) {
  143
+        bool isMainFrame = (frame == m_webPage->m_mainFrame);
  144
+        // check for all frames (including iframes)
  145
+        //if (frame == m_webPage->m_mainFrame) {
  146
+            QString navigation = "Undefined";
  147
+            switch (type) {
  148
+            case NavigationTypeLinkClicked:
  149
+                navigation = "LinkClicked";
  150
+                break;
  151
+            case NavigationTypeFormSubmitted:
  152
+                navigation = "FormSubmitted";
  153
+                break;
  154
+            case NavigationTypeBackOrForward:
  155
+                navigation = "BackOrForward";
  156
+                break;
  157
+            case NavigationTypeReload:
  158
+                navigation = "Reload";
  159
+                break;
  160
+            case NavigationTypeFormResubmitted:
  161
+                navigation = "FormResubmitted";
  162
+                break;
  163
+            case NavigationTypeOther:
  164
+                navigation = "Other";
  165
+                break;
  166
+            }
  167
+
  168
+            emit m_webPage->navigationRequested(request.url(), navigation, !m_webPage->navigationLocked(), isMainFrame);
  169
+
  170
+            return !m_webPage->navigationLocked();
  171
+        //} else {
  172
+        //    return true;
  173
+        //}
  174
+    }
  175
+
  176
+
142 177
 private:
143 178
     WebPage *m_webPage;
144 179
     QString m_userAgent;
@@ -208,6 +243,7 @@ public slots:
208 243
 WebPage::WebPage(QObject *parent, const Config *config, const QUrl &baseUrl)
209 244
     : REPLCompletable(parent)
210 245
     , m_callbacks(NULL)
  246
+    , m_navigationLocked(false)
211 247
 {
212 248
     setObjectName("WebPage");
213 249
     m_webPage = new CustomPage(this);
@@ -216,6 +252,7 @@ WebPage::WebPage(QObject *parent, const Config *config, const QUrl &baseUrl)
216 252
 
217 253
     connect(m_mainFrame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(registerCallbacksHolder()));
218 254
     connect(m_mainFrame, SIGNAL(javaScriptWindowObjectCleared()), SIGNAL(initialized()));
  255
+    connect(m_mainFrame, SIGNAL(urlChanged(QUrl)), SIGNAL(urlChanged(QUrl)));
219 256
     connect(m_webPage, SIGNAL(loadStarted()), SIGNAL(loadStarted()), Qt::QueuedConnection);
220 257
     connect(m_webPage, SIGNAL(loadFinished(bool)), SLOT(finish(bool)), Qt::QueuedConnection);
221 258
 
@@ -335,6 +372,17 @@ QString WebPage::userAgent() const
335 372
     return m_webPage->m_userAgent;
336 373
 }
337 374
 
  375
+void WebPage::setNavigationLocked(bool lock)
  376
+{
  377
+    m_navigationLocked = lock;;
  378
+}
  379
+
  380
+bool WebPage::navigationLocked()
  381
+{
  382
+    return m_navigationLocked;
  383
+}
  384
+
  385
+
338 386
 void WebPage::setViewportSize(const QVariantMap &size)
339 387
 {
340 388
     int w = size.value("width").toInt();
7  src/webpage.h
@@ -57,6 +57,7 @@ class WebPage: public REPLCompletable, public QWebFrame::PrintCallback
57 57
     Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize)
58 58
     Q_PROPERTY(QVariantMap clipRect READ clipRect WRITE setClipRect)
59 59
     Q_PROPERTY(QVariantMap scrollPosition READ scrollPosition WRITE setScrollPosition)
  60
+    Q_PROPERTY(bool navigationLocked READ navigationLocked WRITE setNavigationLocked)
60 61
     Q_PROPERTY(QVariantMap customHeaders READ customHeaders WRITE setCustomHeaders)
61 62
     Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
62 63
 
@@ -89,6 +90,9 @@ class WebPage: public REPLCompletable, public QWebFrame::PrintCallback
89 90
     void setPaperSize(const QVariantMap &size);
90 91
     QVariantMap paperSize() const;
91 92
 
  93
+    void setNavigationLocked(bool lock);
  94
+    bool navigationLocked();
  95
+
92 96
     void setCustomHeaders(const QVariantMap &headers);
93 97
     QVariantMap customHeaders() const;
94 98
 
@@ -128,6 +132,8 @@ public slots:
128 132
     void javaScriptErrorSent();
129 133
     void resourceRequested(const QVariant &req);
130 134
     void resourceReceived(const QVariant &resource);
  135
+    void urlChanged(const QUrl &url);
  136
+    void navigationRequested(const QUrl &url, const QString &navigationType, bool navigationLocked, bool isMainFrame);
131 137
 
132 138
 private slots:
133 139
     void finish(bool ok);
@@ -159,6 +165,7 @@ private slots:
159 165
     QString m_libraryPath;
160 166
     QWebInspector* m_inspector;
161 167
     WebpageCallbacks *m_callbacks;
  168
+    bool m_navigationLocked;
162 169
 
163 170
     friend class Phantom;
164 171
     friend class CustomPage;

2 notes on commit 40fd210

Ivan De Marino
Collaborator
detro commented on 40fd210 June 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! :)

david

Thanks Ivan ;)

Please sign in to comment.
Something went wrong with that request. Please try again.