How to add a right-click menu in Electron that has “Inspect Element” option like Chrome?

前端 未结 2 1957
执笔经年
执笔经年 2020-12-14 14:46

I\'m building an Electron application and I would like to inspect specific UI elements. I have the Chrome dev tools open for development, but what I want is to be able to ri

相关标签:
2条回答
  • 2020-12-14 15:46

    Try electron-context-menu. It adds inspect element, copy and paste.

    0 讨论(0)
  • 2020-12-14 15:47

    Electron has a built-in function called win.inspectElement(x, y).

    Including this function as an option in a right-click context menu is possible by creating an Electron Menu with a MenuItem. Call the following in the client (aka renderer process) 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)
    
    0 讨论(0)
提交回复
热议问题