Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make WebPage kind of work

  • Loading branch information...
commit a47eca1901a8a3f22b32ecc20213cbf7e527bef8 1 parent c8701ee
execjosh authored April 25, 2013
11  src/js/pjsengine.cpp
@@ -51,6 +51,8 @@ PJSEngine::PJSEngine(QObject *parent)
51 51
 
52 52
 PJSEngine::~PJSEngine()
53 53
 {
  54
+    qDeleteAll(m_pages);
  55
+    m_pages.clear();
54 56
 }
55 57
 
56 58
 bool PJSEngine::init()
@@ -117,6 +119,8 @@ bool PJSEngine::init()
117 119
     phantom.setProperty("_createChildProcess", nativeModules.property("getChildProcess"));
118 120
     phantom.setProperty("createFilesystem", nativeModules.property("getFileSystem"));
119 121
     phantom.setProperty("createSystem", nativeModules.property("getSystem"));
  122
+    phantom.setProperty("createWebPage", me.property("createWebPage"));
  123
+    phantom.setProperty("defaultPageSettings", m_js->newObject());
120 124
     m_js->globalObject().setProperty("phantom", phantom);
121 125
 
122 126
     m_initialized = true;
@@ -164,6 +168,13 @@ QJSValue PJSEngine::loadModule(const QString &moduleSource, const QString &filen
164 168
    return evaluate(scriptSource, filename);
165 169
 }
166 170
 
  171
+QObject *PJSEngine::createWebPage()
  172
+{
  173
+    WebPage *page = new WebPage(this);
  174
+    m_pages.append(page);
  175
+    return page;
  176
+}
  177
+
167 178
 };
168 179
 
169 180
 // vim:ts=4:sw=4:sts=4:et:
6  src/js/pjsengine.h
@@ -31,6 +31,8 @@
31 31
 #define JS_PJSENGINE_H
32 32
 
33 33
 #include <QObject>
  34
+#include <QList>
  35
+#include <QPointer>
34 36
 #include <QJSEngine>
35 37
 #include <QJSValue>
36 38
 
@@ -38,6 +40,8 @@
38 40
 #include "timers.h"
39 41
 #include "nativemodules.h"
40 42
 
  43
+#include "../webpage.h"
  44
+
41 45
 namespace JS
42 46
 {
43 47
 
@@ -60,6 +64,7 @@ public slots:
60 64
 
61 65
     // Module stuff
62 66
     QJSValue loadModule(const QString &moduleSource, const QString &filename);
  67
+    QObject *createWebPage();
63 68
 
64 69
 private:
65 70
     bool m_initialized;
@@ -68,6 +73,7 @@ public slots:
68 73
     JS::Console *m_console;
69 74
     Timers *m_timers;
70 75
     NativeModules *m_nativemodules;
  76
+    QList<QPointer<WebPage> > m_pages;
71 77
 };
72 78
 
73 79
 };
24  src/modules/webpage.js
@@ -244,7 +244,7 @@ function decorateNewPage(opts, page) {
244 244
     } catch (e) {}
245 245
 
246 246
     // deep copy
247  
-    page.settings = JSON.parse(JSON.stringify(phantom.defaultPageSettings));
  247
+    defineProperty(page, "settings", JSON.parse(JSON.stringify(phantom.defaultPageSettings)));
248 248
 
249 249
     definePageSignalHandler(page, handlers, "onInitialized", "initialized");
250 250
 
@@ -277,7 +277,7 @@ function decorateNewPage(opts, page) {
277 277
 
278 278
     page.onError = phantom.defaultErrorHandler;
279 279
 
280  
-    page.open = function (url, arg1, arg2, arg3, arg4) {
  280
+    defineProperty(page, "open", function open(url, arg1, arg2, arg3, arg4) {
281 281
         var thisPage = this;
282 282
 
283 283
         if (arguments.length === 1) {
@@ -329,14 +329,14 @@ function decorateNewPage(opts, page) {
329 329
             return;
330 330
         }
331 331
         throw "Wrong use of WebPage#open";
332  
-    };
  332
+    });
333 333
 
334 334
     /**
335 335
      * Include an external JavaScript file and notify when done.
336 336
      * @param scriptUrl URL to the Script to include
337 337
      * @param onScriptLoaded If provided, this call back is executed when the inclusion is done
338 338
      */
339  
-    page.includeJs = function (scriptUrl, onScriptLoaded) {
  339
+    defineProperty(page, "includeJs", function includeJs(scriptUrl, onScriptLoaded) {
340 340
         // Register temporary signal handler for 'alert()'
341 341
         this.javaScriptAlertSent.connect(function (msgFromAlert) {
342 342
             if (msgFromAlert === scriptUrl) {
@@ -353,7 +353,7 @@ function decorateNewPage(opts, page) {
353 353
 
354 354
         // Append the script tag to the body
355 355
         this._appendScriptElement(scriptUrl);
356  
-    };
  356
+    });
357 357
 
358 358
     /**
359 359
      * evaluate a function in the page
@@ -361,7 +361,7 @@ function decorateNewPage(opts, page) {
361 361
      * @param   {...}       args    function arguments
362 362
      * @return  {*}                 the function call result
363 363
      */
364  
-    page.evaluate = function (func, args) {
  364
+    defineProperty(page, "evaluate", function evaluate(func, args) {
365 365
         var str, arg, argType, i, l;
366 366
         if (!(func instanceof Function || typeof func === 'string' || func instanceof String)) {
367 367
             throw "Wrong use of WebPage#evaluate";
@@ -387,7 +387,7 @@ function decorateNewPage(opts, page) {
387 387
         }
388 388
         str = str.replace(/,$/, '') + '); }';
389 389
         return this.evaluateJavaScript(str);
390  
-    };
  390
+    });
391 391
 
392 392
     /**
393 393
      * evaluate a function in the page, asynchronously
@@ -397,7 +397,7 @@ function decorateNewPage(opts, page) {
397 397
      * @param   {number}    timeMs  time to wait before execution
398 398
      * @param   {...}       args    function arguments
399 399
      */
400  
-    page.evaluateAsync = function (func, timeMs, args) {
  400
+    defineProperty(page, "evaluateAsync", function evaluateAsync(func, timeMs, args) {
401 401
         // Remove the first 2 arguments because we are going to consume them
402 402
         var args = Array.prototype.slice.call(arguments, 2),
403 403
             numArgsToAppend = args.length,
@@ -418,7 +418,7 @@ function decorateNewPage(opts, page) {
418 418
         args.splice(0, 0, funcTimeoutWrapper);
419 419
 
420 420
         this.evaluate.apply(this, args);
421  
-    };
  421
+    });
422 422
 
423 423
     /**
424 424
      * get cookies of the page
@@ -449,13 +449,13 @@ function decorateNewPage(opts, page) {
449 449
      * @param {string}       selector  css selector for the file input element
450 450
      * @param {string,array} fileNames the name(s) of the file(s) to upload
451 451
      */
452  
-    page.uploadFile = function(selector, fileNames) {
  452
+    defineProperty(page, "uploadFile", function uploadFile(selector, fileNames) {
453 453
         if (typeof fileNames == "string") {
454 454
             fileNames = [fileNames];
455 455
         }
456 456
 
457 457
         this._uploadFile(selector, fileNames);
458  
-    };
  458
+    });
459 459
 
460 460
     // Copy options into page
461 461
     if (opts) {
@@ -476,7 +476,7 @@ function decorateNewPage(opts, page) {
476 476
     // @see https://developer.mozilla.org/en/DOM/window.prompt
477 477
     definePageCallbackHandler(page, handlers, "onPrompt", "_getJsPromptCallback");
478 478
 
479  
-    page.event = {};
  479
+    defineProperty(page, "event", {});
480 480
     page.event.modifier = {
481 481
         shift:  0x02000000,
482 482
         ctrl:   0x04000000,

0 notes on commit a47eca1

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