forked from ariya/phantomjs
/
bootstrap.js
76 lines (64 loc) · 2.55 KB
/
bootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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(),
handlers = {};
function defineSetter(handlerName, signalName) {
page.__defineSetter__(handlerName, function(f) {
if (handlers && typeof handlers[signalName] === 'function') {
try {
this[signalName].disconnect(handlers[signalName]);
} catch (e) {}
}
handlers[signalName] = f;
this[signalName].connect(handlers[signalName]);
});
}
// deep copy
page.settings = JSON.parse(JSON.stringify(phantom.defaultPageSettings));
defineSetter("onLoadStarted", "loadStarted");
defineSetter("onLoadFinished", "loadFinished");
defineSetter("onResourceRequested", "resourceRequested");
defineSetter("onResourceReceived", "resourceReceived");
defineSetter("onAlert", "javaScriptAlertSent");
defineSetter("onConsoleMessage", "javaScriptConsoleMessageSent");
page.open = function () {
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(arguments.callee);
} catch (e) {}
}
});
// Append the script tag to the body
this._appendScriptElement(scriptUrl);
};
return page;
}