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

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?

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.

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)

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.