// 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();

    // 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') {
            try {
                this.loadStarted.disconnect(this._handlers.loadStarted);
            } catch (e) {}
        }
        this._handlers.loadStarted = f;
        this.loadStarted.connect(this._handlers.loadStarted);
    });

    page.__defineSetter__("onLoadFinished", function(f) {
        if (this._handlers && typeof this._handlers.loadFinished === 'function') {
            try {
                this.loadFinished.disconnect(this._handlers.loadFinished);
            } catch (e) {}
        }
        this._handlers.loadFinished = f;
        this.loadFinished.connect(this._handlers.loadFinished);
    });

    page.__defineSetter__("onResourceRequested", function(f) {
        if (this._handlers && typeof this._handlers.resourceRequested === 'function') {
            try {
                this.resourceRequested.disconnect(this._handlers.resourceRequested);
            } catch (e) {}
        }
        this._handlers.resourceRequested = f;
        this.resourceRequested.connect(this._handlers.resourceRequested);
    });

    page.__defineSetter__("onResourceReceived", function(f) {
        if (this._handlers && typeof this._handlers.resourceReceived === 'function') {
            try {
                this.resourceReceived.disconnect(this._handlers.resourceReceived);
            } catch (e) {}
        }
        this._handlers.resourceReceived = f;
        this.resourceReceived.connect(this._handlers.resourceReceived);
    });

    page.onAlert = function (msg) {};

    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);
        }
        if (arguments.length === 1) {
            this.openUrl(arguments[0], 'get', this.settings);
            return;
        }
        if (arguments.length === 2) {
            this.onLoadFinished = arguments[1];
            this.openUrl(arguments[0], 'get', this.settings);
            return;
        } else if (arguments.length === 3) {
            this.onLoadFinished = arguments[2];
            this.openUrl(arguments[0], arguments[1], this.settings);
            return;
        } else if (arguments.length === 4) {
            this.onLoadFinished = arguments[3];
            this.openUrl(arguments[0], {
                operation: arguments[1],
                data: arguments[2]
                }, this.settings);
            return;
        }
        throw "Wrong use of WebPage#open";
    };

    page.includeJs = function(scriptUrl, onScriptLoaded) {
        // Register temporary signal handler for 'alert()'
        this.javaScriptAlertSent.connect(function(msgFromAlert) {
            if ( msgFromAlert === scriptUrl ) {
                // Resource loaded, time to fire the callback
                onScriptLoaded(scriptUrl);
                // And disconnect the signal handler
                try {
                    this.javaScriptAlertSent.disconnect(this);
                } catch (e) {}
            }
        });

        // Append the script tag to the body
        this._appendScriptElement(scriptUrl);
    };

    return page;
}

@@ -195,16 +195,39 @@
 ebPage()
+,%0A        handlers = %7B%7D
 ;%0A%0A    /
@@ -320,67 +320,8 @@
 );%0A%0A
-    // private, don't touch this%0A    page._handlers = %7B%7D;%0A%0A
     
@@ -377,38 +377,32 @@
 ) %7B%0A        if (
-this._
 handlers && type
@@ -396,38 +396,32 @@
 dlers && typeof 
-this._
 handlers.loadSta
@@ -497,38 +497,32 @@
 rted.disconnect(
-this._
 handlers.loadSta
@@ -565,38 +565,32 @@
       %7D%0A        
-this._
 handlers.loadSta
@@ -624,38 +624,32 @@
 Started.connect(
-this._
 handlers.loadSta
@@ -726,38 +726,32 @@
 ) %7B%0A        if (
-this._
 handlers && type
@@ -745,38 +745,32 @@
 dlers && typeof 
-this._
 handlers.loadFin
@@ -848,38 +848,32 @@
 shed.disconnect(
-this._
 handlers.loadFin
@@ -917,38 +917,32 @@
       %7D%0A        
-this._
 handlers.loadFin
@@ -978,38 +978,32 @@
 inished.connect(
-this._
 handlers.loadFin
@@ -1086,38 +1086,32 @@
 ) %7B%0A        if (
-this._
 handlers && type
@@ -1105,38 +1105,32 @@
 dlers && typeof 
-this._
 handlers.resourc
@@ -1218,38 +1218,32 @@
 sted.disconnect(
-this._
 handlers.resourc
@@ -1292,38 +1292,32 @@
       %7D%0A        
-this._
 handlers.resourc
@@ -1363,38 +1363,32 @@
 quested.connect(
-this._
 handlers.resourc
@@ -1483,22 +1483,16 @@
     if (
-this._
 handlers
@@ -1502,22 +1502,16 @@
  typeof 
-this._
 handlers
@@ -1605,38 +1605,32 @@
 ived.disconnect(
-this._
 handlers.resourc
@@ -1686,22 +1686,16 @@
         
-this._
 handlers
@@ -1755,22 +1755,16 @@
 connect(
-this._
 handlers

Use closure to make 'handlers' really private.