Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

electron

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 right-click a UI element and choose "Inspect Element" like I can in Google Chrome. Currently, right-clicking doesn't do anything in my boilerplate Electron app. How can I enable this?

like image 864
Tanner Semerad Avatar asked Sep 17 '15 17:09

Tanner Semerad


People also ask

How do you open the dev tools electron?

To open DevTools, use the shortcut "Ctrl+Shift+I" or the <F12> key. You can check out how to use devtools here.


1 Answers

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) 
like image 191
Tanner Semerad Avatar answered Sep 24 '22 22:09

Tanner Semerad