forked from fouber/page-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
451 lines (431 loc) · 13.3 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
var child_process = require('child_process');
var spawn = child_process.spawn;
var fs = require('fs');
var path = require('path');
var Url = require('url');
var util = require("util");
var DEFAULT_DATA_DIRNAME = process.cwd();
var PHANTOMJS_SCRIPT_DIR = path.join(__dirname, 'phantomjs');
var PHANTOMJS_SCRIPT_FILE = path.join(PHANTOMJS_SCRIPT_DIR, 'index.js');
var _ = require('./util.js');
var _exists = fs.existsSync || path.existsSync;
var phantomjs = require('phantomjs');
var binPath = phantomjs.path;
/**
* mkdir -p
* @param {String} path
* @param {Number} mode
*/
function mkdirp(path, mode){
if (typeof mode === 'undefined') {
//511 === 0777
mode = 511 & (~process.umask());
}
if(_exists(path)) return;
path.replace(/\\/g, '/').split('/').reduce(function(prev, next) {
if(prev && !_exists(prev)) {
fs.mkdirSync(prev, mode);
}
return prev + '/' + next;
});
if(!_exists(path)) {
fs.mkdirSync(path, mode);
}
}
/**
* base64 encode
* @param {String|Buffer} data
* @returns {String}
*/
function base64(data){
if(data instanceof Buffer){
//do nothing for quickly determining.
} else if(data instanceof Array){
data = new Buffer(data);
} else {
//convert to string.
data = new Buffer(String(data || ''));
}
return data.toString('base64');
}
/**
* merge settings
* @param {Object} settings
* @returns {*}
*/
function mergeSettings(settings){
var defaultSettings = {
// remove phantomejs application cache before capture
// @see https://github.com/fouber/page-monitor/issues/3
cleanApplicationCache: false,
// phantom cli options
// @see http://phantomjs.org/api/command-line.html
cli: {
'--max-disk-cache-size' : '0',
'--disk-cache' : 'false',
'--ignore-ssl-errors' : 'yes'
},
// webpage settings
// @see http://phantomjs.org/api/webpage/
page: {
viewportSize: {
width: 320,
height: 568
},
settings: {
resourceTimeout: 20000,
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53'
}
},
walk: {
invisibleElements : [
'applet', 'area', 'audio', 'base', 'basefont',
'bdi', 'bdo', 'big', 'br', 'center', 'colgroup',
'datalist', 'form', 'frameset', 'head', 'link',
'map', 'meta', 'noframes', 'noscript', 'optgroup',
'option', 'param', 'rp', 'rt', 'ruby', 'script',
'source', 'style', 'title', 'track', 'xmp'
],
ignoreChildrenElements: [
'img', 'canvas', 'input', 'textarea', 'audio',
'video', 'hr', 'embed', 'object', 'progress',
'select', 'table'
],
styleFilters: [
'margin-left', 'margin-top', 'margin-right', 'margin-bottom',
'border-left-color', 'border-left-style', 'border-left-width',
'border-top-color', 'border-top-style', 'border-top-width',
'border-right-color', 'border-right-style', 'border-right-width',
'border-bottom-color', 'border-bottom-style', 'border-bottom-width',
'border-top-left-radius', 'border-top-right-radius',
'border-bottom-left-radius', 'border-bottom-right-radius',
'padding-left', 'padding-top', 'padding-right', 'padding-bottom',
'background-color', 'background-image', 'background-repeat',
'background-size', 'background-position',
'list-style-image', 'list-style-position', 'list-style-type',
'outline-color', 'outline-style', 'outline-width',
'font-size', 'font-family', 'font-weight', 'font-style', 'line-height',
'box-shadow', 'clear', 'color', 'display', 'float', 'opacity', 'text-align',
'text-decoration', 'text-indent', 'text-shadow', 'vertical-align', 'visibility',
'position'
],
// attributes to mark an element
attributeFilters: [ 'id', 'class' ],
excludeSelectors: [],
removeSelectors: [], // remove elements before walk
ignoreTextSelectors: [], // ignore content change of text node or image change
ignoreStyleSelectors: [], // ignore style change
ignoreChildrenSelectors: [], //
root: 'body'
},
diff: {
// LCS diff priority, `head` or `tail`
priority: 'head',
// highlight mask styles
highlight: {
add: {
title: '新增(Added)',
backgroundColor: 'rgba(127, 255, 127, 0.3)',
borderColor: '#090',
color: '#060',
textShadow: '0 1px 1px rgba(0, 0, 0, 0.3)'
},
remove: {
title: '删除(Removed)',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
borderColor: '#999',
color: '#fff'
},
style: {
title: '样式(Style)',
backgroundColor: 'rgba(255, 0, 0, 0.3)',
borderColor: '#f00',
color: '#f00'
},
text: {
title: '文本(Text)',
backgroundColor: 'rgba(255, 255, 0, 0.3)',
borderColor: '#f90',
color: '#c30'
}
}
},
events: {
init: function(token){
/*
do something before page init,
@see http://phantomjs.org/api/webpage/handler/on-initialized.html
*/
},
beforeWalk: function(token){
/*
do something before walk dom tree,
retrun a number to delay screenshot
*/
}
},
render: {
format: 'jpeg', // @see http://phantomjs.org/api/webpage/method/render.html
quality: 80, // @see http://phantomjs.org/api/webpage/method/render.html
ext: 'jpg', // the same as format, if not specified
delay: 1000, // delay(ms) before screenshot.
timeout: 60 * 1000 // render timeout, max waiting time
},
path: {
root: DEFAULT_DATA_DIRNAME, // data and screenshot save path root
// save path format, it can be a string
// like this: '{hostname}/{port}/{pathname}/{query}{hash}'
format: function(url, opt){
return [
opt.hostname, (opt.port ? '-' + opt.port : ''), '/',
base64(opt.path + (opt.hash || '')).replace(/\//g, '.')
].join('');
}
}
};
// special handling of events
if(settings && settings.events){
_.map(settings.events, function(key, value){
if(typeof value === 'function'){
value = value.toString().replace(/^(function\s+)anonymous(?=\()/, '$1');
settings.events[key] = value;
}
});
}
return _.merge(defaultSettings, settings || {});
}
/**
*
* @param {String} path
* @returns {String}
*/
function escapePath(path){
if(path === '/'){
return '-';
} else {
return path.replace(/^\//, '').replace(/^\.|[\\\/:*?"<>|]/g, '-');
}
}
/**
* path format
* @param {String|Function} pattern
* @param {String} url
* @param {Object} opt
* @returns {String}
*/
function format(pattern, url, opt){
switch (typeof pattern){
case 'function':
return pattern(url, opt);
case 'string':
var pth = [];
String(pattern).split('/').forEach(function(item){
pth.push(item.replace(/\{(\w+)\}/g, function(m, $1){
return escapePath((opt[$1] || ''));
}));
});
return pth.join('/');
default :
throw new Error('unsupport format');
}
}
var LOG_VALUE_MAP ={};
var logTypes = (function(){
var types = [];
_.map(_.log, function(key, value){
LOG_VALUE_MAP[value] = key.toLowerCase();
types.push(_.escapeReg(value));
});
return types.join('|');
})();
var LOG_SPLIT_REG = new RegExp('(?:^|[\r\n]+)(?=' + logTypes + ')');
var LOG_TYPE_REG = new RegExp('^(' + logTypes + ')');
/**
* why not events.EventEmitter?
* because it can NOT emit an 'error' event,
* but i need, fuck off.
* @constructor
*/
var EventEmitter = function(){
this._listeners = {};
};
/**
* add event listener
* @param {string} type
* @param {Function} callback
*/
EventEmitter.prototype.on = function(type, callback){
if(!this._listeners.hasOwnProperty(type)){
this._listeners[type] = [];
}
this._listeners[type].push(callback);
};
/**
* remove event listener
* @param {string} type
* @param {Function} callback
*/
EventEmitter.prototype.off = function(type, callback){
if(this._listeners.hasOwnProperty(type)){
var listeners = [];
for(var i = 0, len = this._listeners[type].length; i < len; i++){
var listener = this._listeners[type][i];
if(listener !== callback){
listeners.push(listener);
}
}
this._listeners[type] = listeners;
}
};
/**
* dispatch event
* @param {string} type
*/
EventEmitter.prototype.emit = function(type){
if(this._listeners.hasOwnProperty(type)){
var args = [].splice.call(arguments, 1);
var self = this;
this._listeners[type].forEach(function(callback){
callback.apply(self, args);
});
}
};
/**
* Monitor Class Constructor
* @param {String} url
* @param {Object} options
* @constructor
*/
var Monitor = function(url, options){
EventEmitter.call(this);
options = mergeSettings(options);
this.url = options.url = url;
this.running = false;
options.path.dir = path.join(
options.path.root || DEFAULT_DATA_DIRNAME,
format(options.path.format, url, Url.parse(url))
);
if(!fs.existsSync(options.path.dir)){
mkdirp(options.path.dir);
}
this.options = options;
this._initLog();
};
// inherit from EventEmitter
util.inherits(Monitor, EventEmitter);
/**
* init log
* @private
*/
Monitor.prototype._initLog = function(){
var log = this.log = {};
_.map(_.log, function(key){
log[key.toLowerCase()] = [];
});
};
/**
* capture webpage and diff
* @param {Function} callback
* @param {Boolean} noDiff
* @returns {*}
*/
Monitor.prototype.capture = function(callback, noDiff){
if(this.running) return;
this.running = true;
var self = this;
var type = _.mode.CAPTURE;
if(!noDiff){
type |= _.mode.DIFF;
}
this._initLog();
return this._phantom(
[
PHANTOMJS_SCRIPT_FILE,
type,
this.url,
JSON.stringify(this.options)
],
function(code, log){
// TODO with code
self.running = false;
callback.call(self, code, log);
}
);
};
/**
* diff with two times
* @param {Number|String|Date} left
* @param {Number|String|Date} right
* @param {Function} callback
* @returns {*}
*/
Monitor.prototype.diff = function(left, right, callback){
if(this.running) return;
this.running = true;
var self = this;
var type = _.mode.DIFF;
this._initLog();
if(_.is(left, 'Date')){
left = left.getDate();
}
if(_.is(right, 'Date')){
right = right.getDate();
}
return this._phantom(
[
PHANTOMJS_SCRIPT_FILE,
type, left, right,
JSON.stringify(this.options)
],
function(code, log){
// TODO with code
self.running = false;
callback.call(self, code, log);
}
);
};
/**
* spawn phantom
* @param {Array} args
* @param {Function} callback
* @returns {*}
* @private
*/
Monitor.prototype._phantom = function(args, callback){
var arr = [];
_.map(this.options.cli, function(key, value){
arr.push(key + '=' + value);
});
this.emit('debug', 'cli arguments: ' + JSON.stringify(arr));
arr = arr.concat(args);
var proc = spawn(binPath, arr);
proc.stdout.on('data', this._parseLog.bind(this));
proc.stderr.on('data', this._parseLog.bind(this));
proc.on('exit', function(code){
callback(code);
});
return proc;
};
/**
* parse log from phantom
* @param {String} msg
* @private
*/
Monitor.prototype._parseLog = function(msg){
var self = this;
String(msg || '').split(LOG_SPLIT_REG).forEach(function(item){
item = item.trim();
if(item){
var type = 'debug';
item = item.replace(LOG_TYPE_REG, function(m, $1){
type = LOG_VALUE_MAP[$1] || type;
return '';
});
self.emit(type, item);
if(self.log.hasOwnProperty(type)){
self.log[type].push(item);
}
}
});
};
module.exports = Monitor;