-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrunner.js
77 lines (63 loc) · 2.11 KB
/
runner.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
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg]
if (trace && trace.length) {
msgStack.push('TRACE:')
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''))
});
}
console.log(msgStack.join('\n'))
phantom.exit(1)
}
var fs = require('fs')
var webpage = require('webpage')
var webserver = require('webserver')
var page = webpage.create()
page.viewportSize = { width: 1440, height: 900 }
var url = 'http://www.nytimes.com/'
page.onConsoleMessage = function(message, lineNumber, sourceId) {
log(message, 'page_errors')
}
page.onError = function(message) {
log(message, 'page_errors')
}
page.open(url, function(status) {
log('opened page ' + url + ': ' + status)
page.evaluate(function(width, height) {
document.body.style.width = width + 'px'
document.body.style.height = height + 'px'
}, page.viewportSize.width, page.viewportSize.height)
page.clipRect = {
top: 0,
left: 0,
width: page.viewportSize.width,
height: page.viewportSize.height
}
setInterval(function() {
page.render('/dev/stdout', { format: 'png' })
}, 100)
log('listening on port 8888')
var server = webserver.create()
var service = server.listen(8888, function(request, response) {
var event = JSON.parse(request.post)
log('event ' + JSON.stringify(event))
if (event.type === 'load' && typeof event.url === 'string') {
log('navigating to ' + event.url)
page.evaluate(function(url) {
window.location = url
}, event.url)
} else {
page.sendEvent(event.type, event.x, event.y)
}
response.statusCode = 204
response.headers = {
'Access-Control-Allow-Origin': '*'
}
response.write('')
response.close()
})
})
function log(message, logfile) {
logfile = logfile || 'main'
fs.write('logs/' + logfile, Date() + '\t' + message + '\n', 'a')
}