-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphantomscript.js
146 lines (135 loc) · 5.05 KB
/
phantomscript.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var webpage = require('webpage');
var system = require('system');
var opts = {
preloadurl : '',
preloadevalcode : '',
preloadevaldelay : 0,
preloaddelay : 0,
url : '',
file : '',
width : 0,
height : 0,
delay : 0,
evaldelay : 0,
evalcode : '',
clip : '',
clipwidth : 0,
clipheight : 0,
cliptop : 0,
clipleft : 0,
clipwithiframe : '',
iframescrollto : 0,
iframescrolldelay : 0
};
var arg_opts = JSON.parse( system.args[1] );
for ( var property in arg_opts ) {
if ( arg_opts.hasOwnProperty(property) ) {
opts[ property ] = arg_opts[ property ];
}
}
['width','height','delay','evaldelay','clipwidth','clipheight','cliptop','clipleft','iframescrollto','iframescrolldelay','preloadevaldelay','preloaddelay'].forEach( function( property ) {
opts[ property ] = parseInt( opts[ property ] );
} );
var page = webpage.create();
if ( opts.preloadurl ) {
page.open( opts.preloadurl, function( success ) {
if ( opts.preloadevalcode ) {
page.injectJs('jquery/jquery-1.12.4.min.js');
setTimeout( function() {
page.evaluate( function( codetoeval ){
eval( codetoeval );
}, opts.preloadevalcode );
}, opts.preloadevaldelay );
}
setTimeout( function() {
dorealpageload();
}, opts.preloaddelay );
} );
}
else {
dorealpageload();
}
function dorealpageload() {
page.viewportSize = { width: opts.width, height: opts.height };
if ( opts.clip || opts.clipwithiframe || opts.clipwidth || opts.clipheight || opts.cliptop || opts.clipleft ) {
var clipwidth = opts.clipwidth || opts.width;
var clipheight = opts.clipheight || opts.height;
var cliptop = opts.cliptop || 0;
var clipleft = opts.clipleft || 0;
page.clipRect = { top: cliptop, left: clipleft, width: clipwidth, height: clipheight };
}
if ( opts.clipwithiframe ) {
page.open( 'about:blank', function( status ) {
page.evaluate(function(u,w,h){
var obj = document.createElement("object");
obj.name = "realpage";
obj.data = u;
obj.width = w;
obj.height = h;
obj.style.width = w + 'px';
obj.style.height = h + 'px';
document.body.appendChild(obj);
document.body.style.margin = '0px';
document.body.style.padding = '0px';
}, opts.url, page.clipRect.width, page.clipRect.height );
page.switchToFrame("realpage");
page.onLoadFinished = function( status ) {
if ( status !== 'success' ) {
console.log('ERROR: Loading address "' + url + '" was not succesfull');
phantom.exit(1);
}
else {
if ( opts.evalcode ) {
window.setTimeout( function() {
page.switchToFrame("realpage");
page.injectJs('jquery/jquery-1.12.4.min.js');
page.switchToFrame("realpage");
page.evaluate( function( codetoeval ){
eval( codetoeval );
}, opts.evalcode );
}, opts.evaldelay );
}
if ( opts.iframescrollto ) {
window.setTimeout( function() {
page.switchToFrame("realpage");
page.evaluate( function( to ){
document.body.scrollTop = to;
}, opts.iframescrollto );
}, opts.iframescrolldelay );
}
window.setTimeout( function() {
page.switchToFrame("realpage");
page.evaluate(function(){
document.body.style.overflow = 'hidden';
});
page.switchToMainFrame();
page.render(opts.file);
phantom.exit();
}, opts.delay );
}
};
});
}
else {
page.open( opts.url, function( status ) {
if ( status !== 'success' ) {
console.log('ERROR: Loading address "' + url + '" was not succesfull');
phantom.exit(1);
}
else {
if ( opts.evalcode ) {
window.setTimeout( function() {
page.injectJs('jquery/jquery-1.12.4.min.js');
page.evaluate( function( codetoeval ){
eval( codetoeval );
}, opts.evalcode );
}, opts.evaldelay );
}
window.setTimeout( function() {
page.render(opts.file);
phantom.exit();
}, opts.delay );
}
});
}
};