forked from segment-boneyard/nightmare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
69 lines (60 loc) · 1.94 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
window.__nightmare = {};
__nightmare.ipc = require('electron').ipcRenderer;
__nightmare.sliced = require('sliced');
// Listen for error events
window.addEventListener('error', function(e) {
__nightmare.ipc.send('page', 'error', e.message, e.error.stack);
});
(function(){
// 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() {
__nightmare.ipc.send('console', 'log', __nightmare.sliced(arguments));
return defaultLog.apply(this, arguments);
};
// listen for console.warn
var defaultWarn = console.warn;
console.warn = function() {
__nightmare.ipc.send('console', 'warn', __nightmare.sliced(arguments));
return defaultWarn.apply(this, arguments);
};
// listen for console.error
var defaultError = console.error;
console.error = function() {
__nightmare.ipc.send('console', 'error', __nightmare.sliced(arguments));
return defaultError.apply(this, arguments);
};
// overwrite the default alert
window.alert = function(message){
__nightmare.ipc.send('page', 'alert', message);
};
// overwrite the default prompt
window.prompt = function(message, defaultResponse){
__nightmare.ipc.send('page', 'prompt', message, defaultResponse);
}
// overwrite the default confirm
window.confirm = function(message, defaultResponse){
__nightmare.ipc.send('page', 'confirm', message, defaultResponse);
}
})()