I'm trying to use a preload script to work around a CORS header issue in Electron 4.2.3. However, I can't get the preload script to run. A minimal reproduction case:
package.json
{
"name": "your-app",
"version": "0.1.0",
"main": "main.js",
"dependencies": {
"electron": "^4.2.3"
}
}
main.js
const { app, BrowserWindow } = require('electron')
app.on('ready', function() {
const win = new BrowserWindow({
webPreferences: {
preload: `file://${__dirname}/preload.js`,
}
})
win.webContents.openDevTools()
win.loadFile('index.html')
})
preload.js
window.preloadWasRun = 'preload was run'
index.html
<body>
<script>
document.write(window.preloadWasRun || 'preload was not run')
</script>
</body>
No matter what settings I use for webSecurity, nodeIntegration and contextIsolation, it seems that my preload script is just getting ignored. Even if I make a syntax error in the script, it doesn't show any errors anywhere.