0

我有一个包装为电子应用程序的角度应用程序。现在,当我在渲染器进程中调用 Window.open 以获取内部路由时,我收到一条消息说Not allowed to load a local resource并打开一个空白窗口。我该如何解决这个问题?

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

我尝试过的事情

  1. 添加了一个监听器new-window没有工作
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. nativeWindowOpen向创建主 BrowserWindow 的主进程添加了标志。:没有工作

我不想使用这些require('electron')模块,或者nodeIntegration:true因为根据可用的在线/文档材料,它增加了安全问题。请建议!

4

2 回答 2

0

如果有人正在寻找答案,这就是我如何解决这个问题的。

根据preload.js我添加以下操作

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

从渲染器进程

(window as any).api.openNewWindow(urlToOpen);
于 2021-02-08T22:07:16.770 回答
0

你在用secure-electron-template吗?您的代码看起来与此类似。仅供参考- 我是那个图书馆的作者。

看起来您从这里获取了示例代码,我的猜测是win正在被垃圾收集。尝试win在通话之外创建.webContents.on('new-window',看看是否可以解决问题。否则,我建议在电子存储库中发布一个问题 - 我至少在另一种情况下看到它,他们的文档已经过时

于 2020-07-17T03:11:00.180 回答