Search code examples
javascriptgoogle-chrome-devtoolsnode-webkitchrome-app-developer-tool

Disable DevTools on nwjs 13


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.


Solution

  • 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.