我正在构建一个Electron应用程序,想要检查特定的UI元素。我已经打开了Chrome开发工具,但是我想要的是能够右键单击UI元素并选择“检查元素”,就像在Google Chrome中一样。目前,在我的Electron模板应用程序中右键单击没有任何反应。如何启用此功能?
我正在构建一个Electron应用程序,想要检查特定的UI元素。我已经打开了Chrome开发工具,但是我想要的是能够右键单击UI元素并选择“检查元素”,就像在Google Chrome中一样。目前,在我的Electron模板应用程序中右键单击没有任何反应。如何启用此功能?
要在右键菜单中包含此函数作为选项,可以通过创建带有MenuItem
的ElectronMenu
实现。 在客户端(也称为渲染器进程)JavaScript中调用以下内容:
// 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)
试试使用electron-context-menu。它添加了检查元素
,复制
和粘贴
功能。
remote.getCurrentWindow()
才能使其正常工作,而不能将其缓存到 currentWindow 常量中。 - enjalotBrowserWindow
上不存在inspectElement
方法。您可以改用remote.getCurrentWindow().webContents.inspectElement
方法。 - Mats