A way to prevent developer tools


#1

Is there a way to prevent dev tools from being opened?
Edit to clarify: I’m asking about how to do this in an app that uses the electron framework.


#2

Are you asking about Atom, the text editor, or Electron, the application framework,? Adding a category to your topic would help clarify.


#3

I didn’t notice that this board covered so much other than electron. I’ve edited the OP to clarify.

Thanks.


#4

Still haven’t found a solution to this.

To elaborate in case I wasn’t clear enough, currently in my electron app you can open devtools by pressing Ctrl + Shift + I. I’d like to remove this capability.

There doesn’t seem to be a reliable way to tell if devtools is open via javascript.


#5

Here you go.


#6

Thanks @DamnedScholar

The final solution:
win.webContents.on("devtools-opened", () => { win.webContents.closeDevTools(); });


#7

If you want to prevent users from opening DevTools via the keyboard shortcut just don’t add a DevTools menu item


#8

Except ctrl+shift+i will still open them. See discord for example, no dev tools menu there but you can still open it.


#9

The solution:

mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
devTools: false
}
})


#10

Thank you, this is indeed the solution.


#11

A hacker who wants to break into your code can still just replace the devtools: false with devtools:true

An improved solution detects whether your browser has been resized. Even if the hacker doesn’t resize the app’s window, it will still trigger a window.onResize event if the devtools console opens because the app’s window is the entire window minus the devtools space. In an onResize event you should close devtools using win.webContents.closeDevTools().

            let electron = require("electron");
            electron.remote.getCurrentWindow().webContents.closeDevTools();

Even this solution can be hacked, but it’s better than nothing. Slack, which is an Electron app seems to have come up with a proprietary solution that I personally have not been able to crack.