From f6c55010b9b1620bd231f10faf3770b84e90a52b Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Sat, 11 Jun 2011 03:51:08 -0700 Subject: [PATCH 1/3] Use closure to make 'handlers' really private. --- src/bootstrap.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/bootstrap.js b/src/bootstrap.js index 3446733b..c35c11f7 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -1,52 +1,50 @@ // This allows creating a new web page using the construct "new WebPage", // which feels more natural than "phantom.createWebPage()". window.WebPage = function() { - var page = phantom.createWebPage(); + var page = phantom.createWebPage(), + handlers = {}; // deep copy page.settings = JSON.parse(JSON.stringify(phantom.defaultPageSettings)); - // private, don't touch this - page._handlers = {}; - page.__defineSetter__("onLoadStarted", function(f) { - if (this._handlers && typeof this._handlers.loadStarted === 'function') { + if (handlers && typeof handlers.loadStarted === 'function') { try { - this.loadStarted.disconnect(this._handlers.loadStarted); + this.loadStarted.disconnect(handlers.loadStarted); } catch (e) {} } - this._handlers.loadStarted = f; - this.loadStarted.connect(this._handlers.loadStarted); + handlers.loadStarted = f; + this.loadStarted.connect(handlers.loadStarted); }); page.__defineSetter__("onLoadFinished", function(f) { - if (this._handlers && typeof this._handlers.loadFinished === 'function') { + if (handlers && typeof handlers.loadFinished === 'function') { try { - this.loadFinished.disconnect(this._handlers.loadFinished); + this.loadFinished.disconnect(handlers.loadFinished); } catch (e) {} } - this._handlers.loadFinished = f; - this.loadFinished.connect(this._handlers.loadFinished); + handlers.loadFinished = f; + this.loadFinished.connect(handlers.loadFinished); }); page.__defineSetter__("onResourceRequested", function(f) { - if (this._handlers && typeof this._handlers.resourceRequested === 'function') { + if (handlers && typeof handlers.resourceRequested === 'function') { try { - this.resourceRequested.disconnect(this._handlers.resourceRequested); + this.resourceRequested.disconnect(handlers.resourceRequested); } catch (e) {} } - this._handlers.resourceRequested = f; - this.resourceRequested.connect(this._handlers.resourceRequested); + handlers.resourceRequested = f; + this.resourceRequested.connect(handlers.resourceRequested); }); page.__defineSetter__("onResourceReceived", function(f) { - if (this._handlers && typeof this._handlers.resourceReceived === 'function') { + if (handlers && typeof handlers.resourceReceived === 'function') { try { - this.resourceReceived.disconnect(this._handlers.resourceReceived); + this.resourceReceived.disconnect(handlers.resourceReceived); } catch (e) {} } - this._handlers.resourceReceived = f; - this.resourceReceived.connect(this._handlers.resourceReceived); + handlers.resourceReceived = f; + this.resourceReceived.connect(handlers.resourceReceived); }); page.onAlert = function (msg) {}; From d92723bd29af497a80cf5ea9074025db050e6995 Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Sat, 11 Jun 2011 16:05:53 -0700 Subject: [PATCH 2/3] Handle onAlert event just like the other events. --- src/bootstrap.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bootstrap.js b/src/bootstrap.js index c35c11f7..a10d29fb 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -47,14 +47,19 @@ window.WebPage = function() { this.resourceReceived.connect(handlers.resourceReceived); }); - page.onAlert = function (msg) {}; + page.__defineSetter__("onAlert", function(f) { + if (handlers && typeof handlers.javaScriptAlertSent === 'function') { + try { + this.javaScriptAlertSent.disconnect(handlers.javaScriptAlertSent); + } catch (e) {} + } + handlers.javaScriptAlertSent = f; + this.javaScriptAlertSent.connect(handlers.javaScriptAlertSent); + }); page.onConsoleMessage = function (msg) {}; page.open = function () { - if (typeof this.onAlert === 'function') { - this.javaScriptAlertSent.connect(this.onAlert); - } if (typeof this.onConsoleMessage === 'function') { this.javaScriptConsoleMessageSent.connect(this.onConsoleMessage); } From 693d1c5f2383890ba7dcdb59565a99545843b8ad Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Sat, 11 Jun 2011 17:16:49 -0700 Subject: [PATCH 3/3] Handle 'onConsoleMessage' event just like the other events. --- src/bootstrap.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bootstrap.js b/src/bootstrap.js index a10d29fb..8fe2d55c 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -57,12 +57,17 @@ window.WebPage = function() { this.javaScriptAlertSent.connect(handlers.javaScriptAlertSent); }); - page.onConsoleMessage = function (msg) {}; + page.__defineSetter__("onConsoleMessage", function(f) { + if (handlers && typeof handlers.javaScriptConsoleMessageSent === 'function') { + try { + this.javaScriptConsoleMessageSent.disconnect(handlers.javaScriptConsoleMessageSent); + } catch (e) {} + } + handlers.javaScriptConsoleMessageSent = f; + this.javaScriptConsoleMessageSent.connect(handlers.javaScriptConsoleMessageSent); + }); page.open = function () { - if (typeof this.onConsoleMessage === 'function') { - this.javaScriptConsoleMessageSent.connect(this.onConsoleMessage); - } if (arguments.length === 1) { this.openUrl(arguments[0], 'get', this.settings); return;