-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-phantom.js
95 lines (66 loc) · 1.82 KB
/
test-phantom.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
var phantom = require('phantom');
var cheerio = require('cheerio');
var fs = require('fs');
//'http://www.endclothing.com/'
loadDynamicPage('http://www.endclothing.com/us/brands/bamford-grooming-department', function(){
console.log("all ok");
});
function loadDynamicPage(url, endCB) {
console.log('loading dynamic page with polyfill');
var obj = [];
var urls = [];
var pg= null, ph = null;
phantom.create(['--ignore-ssl-errors=yes', '--load-images=yes'])
.then(function (instance) {
ph = instance;
return ph.createPage();
})
.then(function(page){
pg = page;
return pg.property('viewportSize', {width: 1024, height: 768});
}).then(function(){
return pg.setting('userAgent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8')
}).then(function(){
return pg.property('customHeaders', {
"Accept-Language": "en-US,en;q=0.5"
});
}).then(function(){
return pg.on('onResourceRequested', false, function(requestData, networkRequest){
urls.push(requestData.url); // this would push the url into the urls array above
});
}).then(function(){
return pg.on('onInitialized', true, function(page){
this.injectJs('./helpers/phantom/polyfill.js');
});
}).then(function(){
return pg.open(url);
}).then(function(status){
if(status){
setTimeout(function(){
pg.evaluate(function () {
window.scrollBy(0, 10000);
return window.pageYOffset;
}).then(function (r) {
pg.property('content')
.then(function (content) {
console.log(content)
var $ = cheerio.load(content);
var body = $('body');
pg.close();
ph.exit();
endCB(body[0]);
});
});
}, 5000);
}else{
pg.close();
ph.exit();
endCB();
}
}).catch(function(error){
console.log(error);
pg.close();
ph.exit();
endCB();
})
}