Disable DevTools on nwjs 13
Asked Answered
C

5

7

We're developing an app based on Chrome Apps with NWJS 0.13.0 Alpha version, because lower versions don't support Chrome Apps. We need version 13 so we can use Serial Ports.

However in Windows or Ubuntu, when pressing right clic it showed a menu which I disabled (because it was specified that way) with the following function in all of my HTML's:

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

But in Mac OS X we had another problem because of a custom menu, after reading the Manifest Format I found that on my package.json file I needed to add "no-edit-menu": false attribute and that menu doesn't show anymore, the package.json file is the following:

{
    "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 "toolbar": false, on the package.json file so there's no toolbar and thus user can't open devtools from there, but if they press F12 or Shift-Ctrl-J they're still able to open devtools window. I also tried the following line in my above script in an attempt to try to disable devtools window to open without success (at least on Mac OS X where it's 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 from here for Apple's keyboard.

I'm really new into Javascript coding so probably some of my attempts aren't right or close to be right.

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


Edit

I found there was an error on my 2nd attempt with keyCodes.

I was trying to call the script on right click event, 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 dev tools using F12 key on Linux, however Windows and OS X are still not working even with this update.

Edit 2

I've found that there are different keyCodes for the different OS as seen on this table so I guess I haven't got a successful response while testing on Windows and OS X because of it.

Casto answered 11/11, 2015 at 19:12 Comment(0)
C
1

Alright, after trying lots of things and reading tons of examples I found there were ctrlKey altKey shiftKey and metaKey properties.

After that, I came with this script which will prevent users from opening DevTools on NWJS 13 from shortcuts (i.e. F12 on Windows and Linux and ⌘ ⌥ I on Mac). And also disables right clic 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 of solving this was using the NWJS alpha version 3 (without SDK), which was specified on the NWJS Google Group but I read it after.

Casto answered 12/11, 2015 at 20:26 Comment(0)
A
4

When DevTools open ,window has a event devtools-opened

in this event 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>
Appendage answered 10/12, 2015 at 5:41 Comment(3)
However this feature you're saying is to close Devtools, what I wanted to do was prevent the opening of this window.Casto
user open devtools,then we capture a event "devtools-opened", in event ,we closeDevTools ,user cannot see DevTools window.Appendage
I'll give it a try tomorrow, but check my answer anyway. There's a pre built NWJS which doesn't include DevTools (The one without SDK), by running the app with this build it simply doesn't open dev tools...Casto
J
3

To disable the DevTools you can use chromium args in your package.json as demonstrated below:

{
    "name": "desktopApp",
    "main": "index.html",
    "version": "1.0.0",
    "chromium-args": "--disable-devtools"
}
Joinder answered 30/7, 2016 at 21:1 Comment(2)
I'll try this later; which version of NWJS did this feature came out?Casto
When using this, the event 'contextmenu' will also disable =/Laird
C
1

Alright, after trying lots of things and reading tons of examples I found there were ctrlKey altKey shiftKey and metaKey properties.

After that, I came with this script which will prevent users from opening DevTools on NWJS 13 from shortcuts (i.e. F12 on Windows and Linux and ⌘ ⌥ I on Mac). And also disables right clic 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 of solving this was using the NWJS alpha version 3 (without SDK), which was specified on the NWJS Google Group but I read it after.

Casto answered 12/11, 2015 at 20:26 Comment(0)
S
0

It is not possible by design. If you are trying to prevent the users from seeing the code or hacking the page it will not help. See the old question.

Schematism answered 11/11, 2015 at 21:34 Comment(2)
Yeah but that link is for prevent the execution of code through console. What I'm trying to achieve is prevent the console from opening i.e. if you press F12 or Shift-Ctrl-J (Windows or Linux) it will open developer tools, which is what I want to prevent, like disabling right click menu as I did on 1st script, something like that.Casto
I'm trying to do the opposite of this question and also check update.Casto
C
0

I'm a bit surprised no one has pointed this out, but the only reason you can launch DevTools at all is because you're using the SDK version, AKA the debug version that you're not supposed to be giving customers anyway.

Just use the prod [normal] version of NW.js. In the prod version you cannot open the DevTools window at all, not even programmatically. Attempting to do so forcibly will give you a blank window. The accepted answer does not actually stop a smart person from launching DevTools.

Cognize answered 14/12, 2021 at 11:28 Comment(1)
Actually I answered my own question and on the edit you can see I gave the same answer as youCasto

© 2022 - 2024 — McMap. All rights reserved.