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

webview and @electron/remote #96

Open
linuslundahl opened this issue Nov 16, 2021 · 10 comments
Open

webview and @electron/remote #96

linuslundahl opened this issue Nov 16, 2021 · 10 comments

Comments

@linuslundahl
Copy link

Electron version: 15.3.1
@electron/remote version: 2.0.1

When adding a <webview /> that has a preload script that imports @electron/remote I get Error: @electron/remote is disabled for this WebContents..

electronRemoteMain.enable(window.webContents); is ran on the BrowserWindow that renders the webview and the wrapping React component is able to use @electron/remote, but the webview hasn't been enabled.

Is there a way of enabling the remote module for a webview?

@linuslundahl linuslundahl changed the title WebView and @electron/remote webview and @electron/remote Nov 16, 2021
@linuslundahl
Copy link
Author

This used to work in version 1.x with the { webPreferences: { enableRemoteModule: true } } setting added to the BrowserWindow.

@kl-nevermore
Copy link

kl-nevermore commented Nov 19, 2021

Electron version: 16.0.0
@electron/remote version: 2.0.1

same error

@kl-nevermore
Copy link

This used to work in version 1.x with the { webPreferences: { enableRemoteModule: true } } setting added to the BrowserWindow.
I found a solution

const mainWindow = new BrowserWindow(opts)
remoteMain.enable(mainWindow.webContents);
mainWindow.webContents.on('will-attach-webview', () => {
      const all = webContents.getAllWebContents();
      all.forEach((item) => {
        remoteMain.enable(item);
     });
});

@ruben-cruz
Copy link

@nevermore-kl I tried your code but didn't worked. Below follows the electron and @electron/remote versions that I'm using. Do you have any suggestion to enable @electron/remote inside an electron-tab webview?

"electron": "^19.0.2",
"@electron/remote": "^2.0.8",
"electron-tabs": "^0.7.3",

@ruben-cruz
Copy link

Anyone can help or give some advice?

@ruben-cruz
Copy link

ruben-cruz commented Jun 7, 2022

@nevermore-kl, after adding your code I also add to set the webPreferences inside webviewAttributes when adding a new TAB like this:

...
title: "test",
src: "./test.html",
webviewAttributes: {
    'nodeintegration': true,
    'contextIsolation': false,
    'webPreferences': 'nodeIntegration=true, contextIsolation=false',
    'preload': "./preLoadTest.js",
},
...

Without setting the 'webPreferences': 'nodeIntegration=true, contextIsolation=false' the @electron/remote won't be able to work even when enabled.

@daief
Copy link

daief commented Aug 24, 2022

Changing will-attach-webview to did-attach-webview works to me. In may case, I cannot get the new webview instance in will-attach-webview event, so I try did-attach-webview.

const mainWindow = new BrowserWindow(opts)
remoteMain.enable(mainWindow.webContents);
mainWindow.webContents.on('did-attach-webview', () => {
      const all = webContents.getAllWebContents();
      all.forEach((item) => {
        remoteMain.enable(item);
     });
});

@kl-nevermore
Copy link

sorry I forgot I set it before
now,I replaced remote with ipc(electron/electron#21408 (comment))

@kl-nevermore
Copy link

replace remote is best solution

@codingforme
Copy link

replace remote is best solution

this is the best way

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

5 participants