gpt4 book ai didi

javascript - 如何在渲染器进程中使用 window.open 在 Electron 中打开新窗口?

转载 作者:行者123 更新时间:2023-12-03 12:39:56 30 4
gpt4 key购买 nike

我有一个 package 为 Electron 应用程序的 Angular 应用程序。现在,当我在渲染器进程中调用 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')
});
我试过的东西
  • 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
    });

  • 已添加 nativeWindowOpen标记到创建主 BrowserWindow 的主进程。: 没有工作

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

    最佳答案

    您在使用 secure-electron-template ?您的代码看起来与此类似。 仅供引用 - 我是那个图书馆的作者。
    看起来您从 here 获取了示例代码,我的猜测是 win正在被垃圾收集。尝试创建 win.webContents.on('new-window'打电话看看能不能解决。否则,我建议在 Electron 存储库中发布一个问题 - 我至少在另一种情况下看到它,他们的文档已经过时

    关于javascript - 如何在渲染器进程中使用 window.open 在 Electron 中打开新窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62924505/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com