gpt4 book ai didi

javascript - ElectronJS。设置窗口名称或确定哪个窗口发出事件

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

有没有一种方法可以使用ipcRenderer标识哪个窗口发出事件,以便订阅该窗口以监听外部事件?
事件处理程序如下所示:

_registerListener(event, {windowName, id, eventName}) {

this._ems[id].on(eventName, (value)=>{
this._windows[windowName].webContents.send(eventName, value);
});
}

我发现的部分解决方案是在WebRenderer上下文中设置窗口的name属性。
我发现的唯一方法是在创建BrowserWindow时使用executeJavaScript方法:
mainWindow.webContents.executeJavaScript("window.name='mainWindow';");

但是此解决方案不适用于我,因为传递给此方法的脚本是在HTML header 中链接的脚本之后执行的。

还有另一种选择BrowserWindow名称或标识名称的方法吗?

谢谢。

最佳答案

找到解决方案。我们可以通过其webContent来识别窗口。
Electron ipcEvent具有字段发送者,它表示窗口的webContent。
因此,在我的情况下,解决方案是像这样进行关闭:

 _registerListener(event, {id, eventName}) {

this._ems[id].on(eventName, (value)=>{
event.sender.webContents.send(eventName, value);
});
}

关于javascript - ElectronJS。设置窗口名称或确定哪个窗口发出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60183883/

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