electron/remote

webview and @electron/remote

linuslundahl opened this issue ยท 10 comments

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?

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

Electron version: 16.0.0
@electron/remote version: 2.0.1

same error

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);
     });
});

@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",

Anyone can help or give some advice?

@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 commented

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);
     });
});

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

replace remote is best solution

replace remote is best solution

this is the best way