Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable(webContents) not working in preload script #128

Open
sindbad75 opened this issue Apr 8, 2022 · 2 comments
Open

enable(webContents) not working in preload script #128

sindbad75 opened this issue Apr 8, 2022 · 2 comments

Comments

@sindbad75
Copy link

sindbad75 commented Apr 8, 2022

Hello everyone.
I am new on electron and trying to update an electron project from v11.5.0 to latest version (or at least last 17 sub version).
My setup:
"node": "14.16"
"@electron/remote": "^2.0.8",
"electron": "^12.0.0", (same issue with 13.6.9 or 17.3.1)

I am facing an issue trying to move from enableRemoteModule (in BrowserWindow's webPreferences) to enable(webContents).
Here it is about a notification windows which should be opened and display the right content.
I stay on electron 12.0.0/13.6.9 in order to switch easily from one to the other.

Before (enableRemoteModule)

main:

win = new BrowserWindow({
   ...
    webPreferences: {
      webviewTag: true,
      enableRemoteModule: true,
      nativeWindowOpen: true,
      contextIsolation: false,
      preload: ...,
    }
  });

preload

  const win = new remote.BrowserWindow({
    ...,
    webPreferences: {
      enableRemoteModule: true,
      contextIsolation: false,
      preload: ...,
    }
  });

After (enable)

main:

import * as remoteMain from '@electron/remote/main';
[...]

win = new BrowserWindow({
   ...
    webPreferences: {
      webviewTag: true,
      nativeWindowOpen: true,
      contextIsolation: false,
      preload: ...,
    }
  });

  remoteMain.enable(win.webContents)

preload:

  const win = new remote.BrowserWindow({
    ...,
    webPreferences: {
      enableRemoteModule: true,
      contextIsolation: false,
      preload: ...,
    }
  });

remote.require("@electron/remote/main").enable(win.webContents);

I probably miss something important.
In preload.js, with enableRemoteModule: true, every thing works fine, my notification window open and is filled up with its content. Using enable, my notification window open but nothing is displayed in it. I do not have any console warning or error.
In main, using enable seems to work.

I guess it could be related to an initialization issue.

I do not know if it could help but I noticed that remoteMain.enable(win.webContents) in main returns null and remote.require("@electron/remote/main").enable(win.webContents) returns undefined in preload even if both emoteMain.enable and remote.require("@electron/remote/main").enable return the same exact function.

@ouxuwen
Copy link

ouxuwen commented Jul 9, 2022

enable(webContents) is only work in main process

@monhi
Copy link

monhi commented Sep 22, 2022

I also face this problem and could not resolve yet.
I need to enable @electron/remote in preload.js of webview tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants