You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: webContents.executeJavaScript will never execute/fail when no content has been loaded. #31792
I agree to follow the Code of Conduct that this project adheres to.
I have searched the issue tracker for a feature request that matches the one I want to file, without success.
Electron Version
15.3.1
What operating system are you using?
Windows
Operating System Version
Windows 10 20H2
What arch are you using?
x64
Last Known Working Electron version
No response
Expected Behavior
Promise returned by webContents.executeJavaScript should be rejected when no content has been loaded.
Actual Behavior
Promise returned by webContents.executeJavaScript will not fail when no content has been loaded.
The promise will only then succeed or fail once content has been loaded.
Testcase Gist URL
No response
Additional Information
When a window has been opened and no content has been loaded yet (dom-ready event has not been fired, no loadUrl, or similar has been called), all executeJavaScript calls will wait until the content has been loaded.
Which is a problem as the Promise returned by executeJavaScript will not resolve or reject until the content is ready.
I would expect an immediate rejection or an exception to be thrown.
Workaround for this is to wait for the dom-ready or similar event to fire and only then use executeJavaScript.
What you consider unexpected behaviour is currently explicitly intended behaviour. If you want to know if the page is loaded before calling executeJavaScript you will have to determine that state yourself via events
Workaround 1:
In the preload of the webcontents: ipcRenderer.on('channel', () => { ... }). In the main process: webcontents.send('channel').
Workaround 2:
In the main process: webcontents.loadURL('javaScript:window.xxx()'). If the webcontents type is webview, you can use webview.src = javaScript:window.xxx() in the render process.
Activity
MarshallOfSound commentedon Nov 11, 2021
What you consider unexpected behaviour is currently explicitly intended behaviour. If you want to know if the page is loaded before calling
executeJavaScriptyou will have to determine that state yourself via eventsasd281533890 commentedon Aug 23, 2022
Workaround 1:
In the preload of the webcontents:
ipcRenderer.on('channel', () => { ... }). In the main process:webcontents.send('channel').Workaround 2:
In the main process:
webcontents.loadURL('javaScript:window.xxx()'). If the webcontents type is webview, you can usewebview.src = javaScript:window.xxx()in the render process.