Disable DevTools on nwjs 13

We are developing a Chrome Apps-based application with NWJS version 0.13.0 Alpha, since lower versions do not support Chrome Apps. We need version 13, so we can use serial ports.

However, on Windows or Ubuntu, when I right-clicked, a menu was shown that I disabled (since it was specified that way) with the following function in all my HTML files:

<body oncontextmenu="return false"> <script language="javascript"> document.onmousedown=disableclick; function disableclick(event) { if(event.button==2) { return false; } } </script> 

But on Mac OS X, we had another problem due to the user menu, after reading the format manifest, I found that on my package.json file I needed to add the attribute "no-edit-menu": false , and this menu is no longer displayed, the package.json file looks like this:

 { "main": "main.html", "name": "PAGUSS", "description": "Paguss Payment Services", "version": "0.1.0", "keywords": [ "paguss", "payment" ], "window": { "title": "Paguss", "transparent": true, "icon": "assets/images/64x64.png", "toolbar": false, "frame": true, "resizable": true, "position": "mouse", "min_width": 400, "min_height": 500, "max_width": 1200, "max_height": 800, "no-edit-menu": false }, "webkit": { "plugin": false } } 

Now I tried to change the "toolbar": false, in the package.json file so that there is no toolbar there, and therefore the user cannot open devtools from there, but if they press F12 or Shift-Ctrl-J , they can still open the window devtools I also tried the following line in my above script, trying to disable the devtools window to open without success (at least on Mac OS X, where it is our priority to disable it):

 if(event.button==2 || window.event.keycode==123 || (window.event.keycode==55 && window.event.keycode==58 && window.event.keycode==34)) { return false; } 

I got the above key codes here for an Apple keyboard.

I am really new to Javascript coding, so some of my attempts are incorrect or close to correct.

Is there a way to disable Dev tools from opening on NWJS 13 on any OS?


Edit

I found that an error occurred in the second attempt with keyCodes.

I tried to call the script when right clicking, I changed the code as:

 <script language="javascript"> document.onmousedown=disableclick; document.onkeydown=disableconsole; function disableclick(event) { if(event.button==2) { return false; } } function disableconsole(event) { if (event.keyCode==123) { return false; } } </script> 

Which actually prevents the console from opening developer tools with the F12 key on Linux, however, Windows and OS X still do not work even with this update.

Edit 2

I found that there are different keyCodes for different OS, as shown in this table , so I think that I do not have a successful answer during testing on Windows and OS X because of this.

+5
source share
4 answers

Well, after trying a lot of things and reading a lot of examples, I found that ctrlKey altKey shiftKey and metaKey .

After that, I came up with this script that will prevent users from opening DevTools on NWJS 13 from shortcuts (i.e. F12 on Windows and Linux and ⌘ ⌥ I on Mac). It also disables the clic right menu.

 <script language="javascript"> document.onmousedown=disableclick; document.onkeydown=disableconsole; function disableclick(event) { if(event.button==2) { return false; } } function disableconsole(e) { evtobj = window.event? event : e; if (evtobj.keyCode==123 || //Linux & Windows (evtobj.metaKey && evtobj.altKey && evtobj.keyCode==73)) { //Mac return false; } } </script> 

Edit

Another way to solve this problem was to use the alpha version of NWJS version 3 (without SDK), which was listed in the NWJS Google Group , but I read it after.

0
source

When DevTools opens, there is a devtools-opened event in the window

in this case use closeDevTools () to close Devtools win

 <script type="text/javascript"> var gui = require('nw.gui'); var win = gui.Window.get(); win.on("devtools-opened",function(){ console.info("devtools-opened"); win.closeDevTools(); }); </script> 
+4
source

To disable DevTools, you can use chrome args in your package.json, as shown below:

 { "name": "desktopApp", "main": "index.html", "version": "1.0.0", "chromium-args": "--disable-devtools" } 
+1
source

This is not possible by design. If you try to prevent users from seeing the code or hacking a page, this will not help. Look at the old question.

0
source

Source: https://habr.com/ru/post/1235730/


All Articles