Use closure to make 'handlers' really private.

1.2
Ariya Hidayat 2011-06-11 03:51:08 -07:00
parent 3cf4507b13
commit f6c55010b9
1 changed files with 18 additions and 20 deletions

View File

@ -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) {};