Frakcool
Frakcool

Reputation: 11143

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.

Upvotes: 7

Views: 5142

Answers (5)

aggregate1166877
aggregate1166877

Reputation: 3150

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.

Upvotes: 0

Edy Segura
Edy Segura

Reputation: 527

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"
}

Upvotes: 3

pharaoh
pharaoh

Reputation: 63

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>

Upvotes: 4

Frakcool
Frakcool

Reputation: 11143

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.

Upvotes: 1

loislo
loislo

Reputation: 14119

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.

Upvotes: 0

Related Questions