python-eel / Eel

A little Python library for making simple Electron-like HTML/JS GUI apps
MIT License
6.44k stars 587 forks source link

There is a way to disable developer tools? #292

Closed jerearaujo03 closed 4 years ago

jerearaujo03 commented 4 years ago

I've tried many chrome flags, but I can't disable developer tools. Also, there is a way to make my app not run over chrome? Because in MacOS, when my app opens, there menu bar is from Chrome.

ncotrb commented 4 years ago

Hi, I have disabled the opening of DevTools with Javascript. For MacOS other key codes may have to be considered. The script must be included or linked in every HTML document:

window.addEventListener("keydown", function(event) {
    if (event.keyCode == 116) {
        // block F5 (Refresh)
        event.preventDefault();
        event.stopPropagation();
        return false;

    } else if (event.keyCode == 122) {
        // block F11 (Fullscreen)
        event.preventDefault();
        event.stopPropagation();
        return false;

    } else if (event.keyCode == 123) {
        // block F12 (DevTools)
        event.preventDefault();
        event.stopPropagation();
        return false;

    } else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) {
        // block Strg+Shift+I (DevTools)
        event.preventDefault();
        event.stopPropagation();
        return false;

    } else if (event.ctrlKey && event.shiftKey && event.keyCode == 74) {
        // block Strg+Shift+J (Console)
        event.preventDefault();
        event.stopPropagation();
        return false;
    }
});

Alternatively to Chrome, Electron can be used together with EEL. Electron offers a wide range of customization possibilities.

ncotrb commented 4 years ago

You can also disable the context-menu:

window.oncontextmenu = function(event) {
    // block right-click / context-menu
    event.preventDefault();
    event.stopPropagation();
    return false;
};
samuelhwilliams commented 4 years ago

Hi @jerearaujo03 - disabling developer tools will be down to finding the right flag in the browser you're using. You can try workarounds like those posted above, but I'd probably question why you need to do this - and be careful arbitrarily disabling key presses/context menus as you might confuse some users. But if you're absolutely devoted to disabling developer tools and can't find a browser flag, you'll probably need to resort to something hacky like that.

I'm going to close this for now as there isn't much more advice I can offer that's related to Eel. Hope this is enough for you. :)

ghost commented 3 years ago

Thanks @ncotrb It worked for me

anay-p commented 1 year ago

I made the code that disables the shortcut keys more concise (and removed the deprecated keyCode part):

var disabledKeys = ["F5", "F11", "F12"]
var disabledModifierKeys = ["I", "J"]
document.addEventListener("keydown", (event) => {
    if (disabledKeys.includes(event.key)
    || (event.ctrlKey && disabledModifierKeys.includes(event.key))) {
        event.preventDefault();
        event.stopPropagation();
    }
});

To block any other keys, just add to the list. Also, the key value for a keydown event is in lowercase when a letter is pressed but in uppercase when the shift key is also being pressed so there is no need to check for event.shiftKey.