gpt4 book ai didi

javascript - 如何使用 Electron BrowserWindow 和 BrowserView 启用右键单击?

转载 作者:行者123 更新时间:2023-11-30 09:15:49 70 4
gpt4 key购买 nike

我在 BrowserWindow 中有一个 BrowserView(我确实需要两者):

const { app, BrowserWindow, BrowserView } = require('electron');

app.on('ready', () => {
browserWindow = new BrowserWindow({ width: 800, height: 500, frame: false });
bv = new BrowserView({ webPreferences: { nodeIntegration: false }});
bv.setBounds({ x: 0, y: 30, width: 800, height: 470});
bv.webContents.loadURL('https://old.reddit.com');
browserWindow.setBrowserView(bv);
});

在网页上单击鼠标右键不会执行任何操作。 如何像往常一样在 Chrome 中启用右键单击“后退”、“前进”、“重新加载”、“复制”、“粘贴”等?

enter image description here

最佳答案

Electron 在其位于 https://electronjs.org/docs/api/menu 的文档中提供了一些示例菜单

// Importing this adds a right-click menu with 'Inspect Element' option
const remote = require('remote')
const Menu = remote.require('menu')
const MenuItem = remote.require('menu-item')

let rightClickPosition = null

const menu = new Menu()
const menuItem = new MenuItem({
label: 'Inspect Element',
click: () => {
remote.getCurrentWindow().inspectElement(rightClickPosition.x, rightClickPosition.y)
}
})
menu.append(menuItem)

window.addEventListener('contextmenu', (e) => {
e.preventDefault()
rightClickPosition = {x: e.x, y: e.y}
menu.popup(remote.getCurrentWindow())
}, false)

按照此模板,您可以使用自定义 javascript 设置自定义 Angular 色,如 BackForwardReload 等:

Back
const backMenuItem = new MenuItem({
label: 'Back',
click: () => {
window.history.back();
}
})
menu.append(backMenuItem)

Forward
const forwardMenuItem = new MenuItem({
label: 'Forward',
click: () => {
window.history.forward();
}
})
menu.append(forwardMenuItem)

关于javascript - 如何使用 Electron BrowserWindow 和 BrowserView 启用右键单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55188555/

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