How can I pass parameter to <webview> preload js synchronously?


#1

Hi there, currently encountered a problem related to <webview>

preload attribute of <webview> can make some scripts to be run before DOM loaded.

However, any way for me to pass parameter synchronously to preload js so that I can ensure those scripts can be run before DOM loaded too?


#2

When you open a new BrowserWindow, the options object you pass can have a key called webPreferences that stores another object with a preload key that’s associated with whatever script you want to preload.


#3

Nice trick @DamnedScholar Thank you!

However, can I change this script content on-the-fly while the <webview> is alive?

For e.g. I want this script has a variable with an increment count on each navigation of the <webview> (AFAIK webpreferences attribute is set once the <webview> is instantiated)


#4

If your main process keeps track of variables, it can pass those variables into the preload script as normal. You just have to make your renderer process capable of sending messages back to the main process whenever you want to update the variable.