Last active
August 28, 2017 06:43
-
-
Save perry-mitchell/fc7dcd9949f3763ce85eb11b71894ad9 to your computer and use it in GitHub Desktop.
Nightmare Preload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: true, | |
value: null | |
}, | |
onbeforeunload: { | |
enumerable: true, | |
writable: true, | |
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); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment