electron: .getWebContentsId is not a function with contextIsolation enabled

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 4.0.0 through 16.0.0-beta.2
  • Operating System:
    • NixOS 21.11 x86_64
  • Last Known Working Electron version:
    • none

Expected Behavior

Within a preload script, I should be able to get the WebContents ID of a <webview> element using webview.getWebContentsId() (after the dom-ready event fires), whether contextIsolation is enabled or disabled.

Actual Behavior

This only works if contextIsolation is disabled. With contextIsolation enabled, I get Uncaught TypeError: webview.getWebContentsId is not a function.

(Some other way to do this would be fine too, but I haven’t found one.)

To Reproduce

Electron Fiddle: https://gist.github.com/5a21657fe926111ad18026905d1ed81d

Observe the error in the console, then change contextIsolation to false and observe that there’s no error.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 21 (14 by maintainers)

Commits related to this issue

Most upvoted comments

@xmedeko That gives you the webContentsId for the current WebContents, not for a <webview> element.