Javascript 如何在HTML中执行Electron js命令?

Javascript 如何在HTML中执行Electron js命令?,javascript,electron,Javascript,Electron,如何在HTML中执行Electron js命令 我需要从HTML内容中的链接打开一个窗口 这可能吗 谢谢收听 main.js const { ipcMain } = require("electron"); ipcMain.on("changeWindow", function(event, arg) { switch (arg) { case "page1": win.loadURL(

如何在HTML中执行Electron js命令

我需要从HTML内容中的链接打开一个窗口

这可能吗

谢谢收听

main.js

const { ipcMain } = require("electron");

ipcMain.on("changeWindow", function(event, arg) {
    switch (arg) {
        case "page1":
            win.loadURL("Page1 URL");
            break;
        case "page2":
            win.loadURL("Page2 URL");
            break;
        case "page3":
            win.loadURL("Page3 URL");
            break;
        ...
    }
});
index.html

function onButtonClick() {
    ipcRenderer.send("changeWindow", "page2");
}

我会使用预加载

preload.js

然后在main.js中

然后在HTML中,您可以发送打开新窗口的请求

现在进入HTML页面



window.CoreAPI.requestOpenWindow()。然后(()=>{ console.log('done') })
我会为此使用预加载

preload.js

然后在main.js中

然后在HTML中,您可以发送打开新窗口的请求

现在进入HTML页面



window.CoreAPI.requestOpenWindow()。然后(()=>{ console.log('done') })
什么不起作用?未定义ipcRenderer“您需要像导入
ipcMain一样导入它
什么不起作用?未定义ipcRenderer”您需要导入它,就像导入
ipcMain
electron/js2c/renderer\u init.js:91 ReferenceError:recurie未定义如果您试图打开的新网页是您自己的和受信任的站点,那么在创建
浏览器窗口时启用
节点集成
。此错误消息显示在哪个文件中?electron:contextIsolation的默认值已被弃用,并将在electron的未来版本中从false更改为true。有关更多信息,请参阅window.CoreAPI.requestOpenWindow('=>{console.log(data)})-是否可以传递参数?是。您可以使用
args
parameterelectron/js2c/renderer_init.js:91 ReferenceError:Recurie未定义如果您尝试打开的新网页是您自己的和受信任的网站,则在创建
BrowserWindow
时启用
nodeIntegration
。此错误消息显示在哪个文件中?electron:contextIsolation的默认值已被弃用,并将在electron的未来版本中从false更改为true。有关更多信息,请参阅window.CoreAPI.requestOpenWindow('=>{console.log(data)})-是否可以传递参数?是。您可以使用
args
参数访问它们
const { ipcRenderer, contextBridge } = requrie('electron');

contextBridge.exposeInMainWorld('CoreAPI', {
  requestOpenWindow: () => 
    new Promise(resolve => {
      ipcRenderer.send('open_window_request');
      ipcRenderer.on('open_window_response', (_, args) => {
         resolve(args);
      })
    })
});
  mainWindow = new BrowserWindow({
    show: false,
    icon: getAssetPath('icon.png'),
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      preload: "path/to/preload.js", // load the preload.js
    },
  });

  ipcMain.on('open_window_request', (event, args) => {
     // open new window;
     
     // then send some response back if needed
     event.sender.send('open_window_response', { message : 'success' });
  });
<html>
 <script>
   window.CoreAPI.requestOpenWindow().then(() => {
     console.log('done')
   })
 </script>
</html>