Search code examples
javascriptangularelectronipcrendereripcmain

How to open new window in electron using window.open in renderer process?


I have an angular application which is wrapped as electron app. Now when, in the renderer process I call Window.open for an internal route, I get a message saying Not allowed to load a local resource and a blank window opens. How do I get around this ?

preload.js

const { contextBridge, ipcRenderer } = require("electron");

const validChannels = ['sample-event-1', 'sample-event-2', 'sample-event-3'];
contextBridge.exposeInMainWorld(
    "api", {
        send: (channel, data) => {
            // whitelist channels To Main Process
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        receive: (channel, func) => {
            // From Main Process
            if (validChannels.includes(channel)) {
                console.log('receive: ' + channel);
                ipcRenderer.on(channel, (event, ...args) => func(...args));
            }
        }
    }
);

main.js

        mywindow = new BrowserWindow({
            width: 1024,
            height: 768,
            webPreferences: {
                nodeIntegration: false,
                webSecurity: true,
                allowEval: false,
                allowRunningInsecureContent: false,
                contextIsolation: true, // protect against prototype pollution
                enableRemoteModule: false, // turn off remote
                preload: path.join(__dirname, "preload.js") // use a preload script
            },
            title: this._appTitle,
            autoHideMenuBar: true,
            icon: path.join(__dirname, '/../../favicon.ico')
        });

Things I tried

  1. Added a listener for new-window : Did not work
myWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {
  event.preventDefault()
  const win = new BrowserWindow({
    webContents: options.webContents, // use existing webContents if provided
    show: false
  })
  win.once('ready-to-show', () => win.show())
  if (!options.webContents) {
    win.loadURL(url) // existing webContents will be navigated automatically
  }
  event.newGuest = win
});

  1. Added nativeWindowOpen flag to main process which creating the main BrowserWindow.: Did not work

I do not want to use the require('electron') modules or nodeIntegration:true since as per the material available online/documentation it adds to security concerns. Please suggest!


Solution

  • In case someone is looking for an answer, here is how I worked this one out.

    As per preload.js I added following operation

    const { contextBridge, ipcRenderer, remote } = require("electron");
    
    const validChannels = ['sample-event-1', 'sample-event-2', 'sample-event-3'];
    contextBridge.exposeInMainWorld(
        "api", {
            send: (channel, data) => {
                // whitelist channels To Main Process
                if (validChannels.includes(channel)) {
                    ipcRenderer.send(channel, data);
                }
            },
            receive: (channel, func) => {
                // From Main Process
                if (validChannels.includes(channel)) {
                    console.log('receive: ' + channel);
                    ipcRenderer.on(channel, (event, ...args) => func(...args));
                }
            },
            openNewWindow: (url) => {
                var BrowserWindow = remote.BrowserWindow;
                var win = new BrowserWindow({
                    width: 1024,
                    height: 768,
                    show: true,
                    webPreferences: {
                        nodeIntegration: false,
                        webSecurity: true,
                        allowEval: false,
                        nativeWindowOpen: true,
                        allowRunningInsecureContent: false,
                        contextIsolation: true,
                        enableRemoteModule: true,
                        preload: path.join(__dirname, "preload.js")
                    },
                    autoHideMenuBar: true,
                    icon: path.join(__dirname, 'favicon.ico')
                });
    
                win.loadURL(url.format({
                    pathname: path.join(__dirname, url, 'index.html'),
                    protocol: 'file:',
                    slashes: true,
                }));
            }
        }
    );
    

    From renderer process

    (window as any).api.openNewWindow(urlToOpen);