forked from segment-boneyard/nightmare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
108 lines (94 loc) · 2.33 KB
/
preload.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
/* eslint-disable no-console */
var ipc = require('electron').ipcRenderer
var sliced = require('sliced')
function send(_event) {
ipc.send.apply(ipc, arguments)
}
// offer limited access to allow
// .evaluate() and .inject()
// to continue to work as expected.
//
// TODO: this could be avoided by
// rewriting the evaluate to
// use promises instead. But
// for now this fixes the security
// issue in: segmentio/nightmare/#1358
window.__nightmare = {
resolve: function(value) {
send('response', value)
},
reject: function(err) {
send('error', error(err))
}
}
// Listen for error events
window.addEventListener(
'error',
function(err) {
send('page', 'error', error(err))
},
true
)
// prevent 'unload' and 'beforeunload' from being bound
var defaultAddEventListener = window.addEventListener
window.addEventListener = function(type) {
if (type === 'unload' || type === 'beforeunload') {
return
}
defaultAddEventListener.apply(window, arguments)
}
// prevent 'onunload' and 'onbeforeunload' from being set
Object.defineProperties(window, {
onunload: {
enumerable: true,
writable: false,
value: null
},
onbeforeunload: {
enumerable: true,
writable: false,
value: null
}
})
// listen for console.log
var defaultLog = console.log
console.log = function() {
send('console', 'log', sliced(arguments))
return defaultLog.apply(this, arguments)
}
// listen for console.warn
var defaultWarn = console.warn
console.warn = function() {
send('console', 'warn', sliced(arguments))
return defaultWarn.apply(this, arguments)
}
// listen for console.error
var defaultError = console.error
console.error = function() {
send('console', 'error', sliced(arguments))
return defaultError.apply(this, arguments)
}
// overwrite the default alert
window.alert = function(message) {
send('page', 'alert', message)
}
// overwrite the default prompt
window.prompt = function(message, defaultResponse) {
send('page', 'prompt', message, defaultResponse)
}
// overwrite the default confirm
window.confirm = function(message, defaultResponse) {
send('page', 'confirm', message, defaultResponse)
}
/**
* Make errors serializeable
*/
function error(err) {
if (!(err instanceof Error)) return err
return {
code: err.code,
message: err.message,
details: err.detail,
stack: err.stack || ''
}
}