BrowserWindow Preload doesn't run


#1

I’m trying to run the JS behind a browser extension that I built within Electron. To do that I know I’ll need to stub out a bunch of chrome APIs, which is fine.

I’ve built a system for loading my extension down from the website it’s hosted on, and I can access the files, and load them into BrowserWindows, but I’m now at the point where I need to start stubbing.

It looks to me like the preload functionality is perfect for this. But no matter what I do it doesn’t seem to run and there’s no error.

Here’s the code I’m using to open the browser window: https://github.com/toolkit-for-ynab/unofficial-desktop-for-ynab/blob/master/app/toolkit/toolkit-helper.js#L19

And here’s the preload script at the moment: https://github.com/toolkit-for-ynab/unofficial-desktop-for-ynab/blob/master/app/toolkit/chrome-api.js

Both window.chrome and window.yup_loaded are undefined when I look at dev tools.

And process.argv doesn’t even seem to contain a preload script at all.

["/Users/kevin/desktop-for-ynab/node_modules/electron-prebuilt/dist/Electron.app/Contents/Frameworks/Electron Helper.app/Contents/MacOS/Electron Helper", "--type=renderer", "--no-sandbox", "--primordial-pipe-token=6A13E99ACDDD0E19F0144D7E142A003A", "--lang=en-US", "--node-integration=true", "--enable-pinch", "--num-raster-threads=2", "--enable-zero-copy", "--disable-partial-raster", "--enable-gpu-memory-buffer-compositor-resources", "--content-image-texture-target=3553,3553,3553,3553,3553,34037,3553,3553,3553,3553,34037,3553,34037,34037", "--video-image-texture-target=3553,3553,3553,3553,3553,34037,3553,3553,3553,3553,34037,3553,34037,34037", "--channel=846.3.1832410827"]

How do I get the preload script to run before other scripts on the page I’m loading run?


#2

Specify the --enable-logging command-line switch when launching Electron, and check the console for errors.


#3

Alrighty, that settles it.

I was passing a file URL in instead of an actual path. So I changed:

`file://${__dirname}/chrome-api.js`

to

path.join(__dirname, 'chrome-api.js')

and it’s loading correctly now. Thanks for your help!