-
Notifications
You must be signed in to change notification settings - Fork 0
/
ready.js
executable file
·108 lines (93 loc) · 2.49 KB
/
ready.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env phantomjs
/***********************************************************
* Author: @mervinej
* Licence: MIT
* Date: 11/27/2013
*
* Run with:
*
* $ phantomjs ./ready.js ./urls.txt
*
* or
*
* $ phantomjs ./ready.js \
* "http://foo.com, http://foo.com/bar"
*
* '--json' returns JSON output for parsing with Phapper
* (http://github.com/jmervine/phapper).
*
***********************************************************/
var webpage = require('webpage');
var system = require('system');
var util = require('../common/util');
var args = system.args.copyArgs();
function usage() {
console.log('Usage: ready.js <URL(s)>|<URL(s) file> [--json]');
phantom.exit();
}
if (args.length === 0) {
usage();
}
var json = args.getArg(['--json', '-j'], false);
var addresses = util.parsePaths(args.shift());
var finished = 0;
if (addresses.length === 0) {
usage();
}
var results = [];
var limit = 15;
var running = 1;
function launcher(){
running--;
while(running < limit && addresses.length > 0){
running++;
collectData(addresses.shift());
}
if(running < 1 && addresses.length < 1){
if (json) {
console.dir(results);
}
phantom.exit();
}
}
function collectData(address) {
var t = Date.now();
var page = webpage.create();
var requests = [];
var ready;
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address\n=> '+address);
} else {
if (json) {
results.push({
address: address,
ready: (ready-t),
complete: (Date.now()-t)
});
} else {
console.log('Regarding: ' + address);
console.log('> ready after: ' + (ready-t) + ' msec');
console.log('> complete after: ' + (Date.now()-t) + ' msec');
console.log(' ');
}
}
(page.close||page.release)();
launcher();
return;
});
page.onInitialized = function() {
page.evaluate(function() {
// $(document).ready(...)
document.addEventListener('DOMContentLoaded', function() {
window.callPhantom({ ready: true });
}, false);
});
};
page.onCallback = function(data) {
if (data && data.ready) {
ready = Date.now();
}
};
}
launcher();