-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphantom-script.js
More file actions
59 lines (55 loc) · 1.38 KB
/
phantom-script.js
File metadata and controls
59 lines (55 loc) · 1.38 KB
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
var system = require('system');
var args = system.args;
var resourceWait = 300;
var maxRenderWait = 10000;
var url = '';
var page = require('webpage').create(),
count = 0,
forcedRenderTimeout,
renderTimeout;
if (args.length === 1) {
phantom.exit();
} else {
url = args[args.length - 1];
}
page.viewportSize = {
width: 1280,
height: 1024
};
function doRender() {
var content = page.content;
console.log(content);
phantom.exit();
}
page.onResourceRequested = function (req) {
count += 1;
clearTimeout(renderTimeout);
};
page.onError = function (msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function (t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function
? ' (in function "' + t.function + '")'
: ''));
});
}
};
page.onResourceReceived = function (res) {
if (!res.stage || res.stage === 'end') {
count -= 1;
if (count === 0) {
renderTimeout = setTimeout(doRender, resourceWait);
}
}
};
page.open(url, function (status) {
if (status !== "success") {
phantom.exit();
} else {
forcedRenderTimeout = setTimeout(function () {
doRender();
}, maxRenderWait);
}
});