Skip to content

[Bug]: webContents.executeJavaScript will never execute/fail when no content has been loaded. #31792

Closed

Description

@markleutloff

Preflight Checklist

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.

Activity

  1. MarshallOfSound commented on Nov 11, 2021

    @MarshallOfSound
    Member

    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

  2. asd281533890 commented on Aug 23, 2022

    @asd281533890
    • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions