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

@electron/remote for multiple windows #81

Closed
otsybulsky opened this issue Sep 22, 2021 · 10 comments
Closed

@electron/remote for multiple windows #81

otsybulsky opened this issue Sep 22, 2021 · 10 comments

Comments

@otsybulsky
Copy link

Hi! I use electron 14 and @electron/remote 2.0.1 for main window successfully. Then I try open child window.
I get an error

core.js:6479 ERROR Error: @electron/remote is disabled for this WebContents. 
Call require("@electron/remote/main").enable(webContents) to enable it.
    at IpcMainImpl.<anonymous> (C:\projects\xProject\node_modules\@electron\remote\dist\src\main\server.js:317:61)
    at IpcMainImpl.emit (events.js:376:20)
    at Object.<anonymous> (electron/js2c/browser_init.js:161:10490)
    at Object.emit (events.js:376:20)

Please help me understand how to open multiple child window with call require("@electron/remote/main").enable(webContents).

Thank you for help.

@otsybulsky otsybulsky changed the title @electron/remote" for multiple windows @electron/remote for multiple windows Sep 22, 2021
@huyinghuan
Copy link

like this?

import { enable as enableWebContents } from "@electron/remote/main"
const win = new BrowserWindow({...})
enableWebContents(win.webContents)

@otsybulsky
Copy link
Author

This works well for the main process. But not for child windows. It looks like I need to use remote.require ("@ electron / remote / main"). Enable (#72 (comment)). I hope this will resolve the issue. Finally I keep moving!

@tonton-pixel
Copy link

tonton-pixel commented Sep 23, 2021

Thanks to @otsybulsky for this useful hint.
I had the same issue in my Electron app making use of a "child" window (Electron 15.0.0 and @electron/remote 2.0.1), and I can confirm that this does indeed work flawlessly from a renderer process:

const remote = require ('@electron/remote');
// [...]
remote.require ('@electron/remote/main').enable (applyWindow.webContents);

https://github.com/tonton-pixel/color-ramp-formulator/blob/f8af8c466d20b4e963668cc4fa9a5b6028c6a0ed/renderer/renderer.js#L3

https://github.com/tonton-pixel/color-ramp-formulator/blob/f8af8c466d20b4e963668cc4fa9a5b6028c6a0ed/renderer/renderer.js#L1648

@imndx
Copy link

imndx commented Sep 24, 2021

Thanks to @otsybulsky for this useful hint.
I had the same issue in my Electron app making use of a "child" window (Electron 15.0.0 and @electron/remote 2.0.1), and I can confirm that this does indeed work flawlessly from a renderer process:

const remote = require ('@electron/remote');
// [...]
remote.require ('@electron/remote/main').enable (applyWindow.webContents);

https://github.com/tonton-pixel/color-ramp-formulator/blob/f8af8c466d20b4e963668cc4fa9a5b6028c6a0ed/renderer/renderer.js#L3

https://github.com/tonton-pixel/color-ramp-formulator/blob/f8af8c466d20b4e963668cc4fa9a5b6028c6a0ed/renderer/renderer.js#L1648

Thanks to @tonton-pixel , but your solution not works for me, the child window created from the main window still complains: Uncaught Error: @electron/remote is disabled for this WebContents. Call require("@electron/remote/main").enable(webContents) to enable it.".

The following is my code, anyone has any idea? Thanks.

// click some button in the main window
            let win = new BrowserWindow(
                {
                    width: width,
                    height: height,
                    minWidth: width,
                    minHeight: height,
                    resizable: true,
                    maximizable: false,
                    webPreferences: {
                        scrollBounce: false,
                        nativeWindowOpen: true,
                        nodeIntegration: true,
                        contextIsolation: false,
                    },
                }
            );

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

@otsybulsky
Copy link
Author

My code contains this flow:
remote = window.require('@electron/remote');
let win = new remote.BrowserWindow({...
remote.require("@electron/remote/main").enable(win.webContents)

@imndx
Copy link

imndx commented Sep 24, 2021

My code contains this flow:
remote = window.require('@electron/remote');
let win = new remote.BrowserWindow({...
remote.require("@electron/remote/main").enable(win.webContents)

Thanks for your reply.

I tried your flow, but still the same error.

@imndx
Copy link

imndx commented Sep 26, 2021

Finally, I figured out a solution, I CANNOT explain why, BUT it works for me.

  1. in the main process, add the following lines:
    app.on('remote-require', (event, args) => {
        // event.preventDefault();
        event.returnValue = require('@electron/remote/main');
    });
  1. in the redenerer process, where create the new BrowserWindow, add the following lines:
const remote = require('@electron/remote');
....

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

@TheToto
Copy link

TheToto commented Oct 21, 2021

I create windows from renderer and I use nativeWindowOpen: true,
I my case it works with (in main process) :

const remoteMain = require('@electron/remote/main')

...

mainWin.webContents.on('did-create-window', (childWin) => {
    remoteMain.enable(childWin.webContents)
})

@kingbotss
Copy link

It should be called immediately after window created.

@Sweet-KK
Copy link

your solution not work for me too, It will show error: "@electron/remote" cannot be required in the browser process. Instead require("@electron/remote/main")

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

7 participants