Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent users from opening the browser console through shortcut keys #135

Closed
Sunyang12345678 opened this issue Mar 7, 2024 · 4 comments
Closed

Comments

@Sunyang12345678
Copy link

Hello, in the packaged exe application, in flaskwebgui, through the options, users are prohibited from calling the browser console through shortcut keys or right-clicking.

@Sunyang12345678
Copy link
Author

image
Just remove this interface. If you don't remove it, users can easily understand that the program is packaged in a web page.

@ClimenteA
Copy link
Owner

Some javascript is needed here to listen to keystrokes and ignore those which are browser specific. I'll try to figure out a solution.

@Sunyang12345678
Copy link
Author

ok,thank you

@ClimenteA
Copy link
Owner

Adding native menu on right click event is not an easy task. Tried below code, but copy and paste functionality don't work because the default dialog with copy/paste it's a native feature outside javascript reach for security reasons.

You can prevent a right click or F12 (or other keys/events) from happening by adding the code below in your index.html file:

<script>

        // Prevent F12 key
        document.onkeydown = function (e) {
            if (e.key === "F12") {
                e.preventDefault();
            }
        };

        // Prevent right-click 
        document.addEventListener("contextmenu", function (e) {
            e.preventDefault();
        });

    </script>

Here if you want to try to implement the copy/paste (I didn't managed to do it).
The thing is that the right click widget must be customizable depending on the UI.

<script>

        // Prevent right-click to show copy/paste widget instead
        
        document.addEventListener("contextmenu", function (e) {
            e.preventDefault();

            const cursorX = e.clientX;
            const cursorY = e.clientY;

            const dropdown = document.createElement('div');
            dropdown.style.position = 'absolute';
            dropdown.style.left = `${cursorX}px`;
            dropdown.style.top = `${cursorY}px`;
            dropdown.style.color = 'black';
            dropdown.style.backgroundColor = 'white';
            dropdown.style.paddingTop = '5px';
            dropdown.style.paddingBottom = '5px';
            dropdown.style.paddingLeft = '10px';
            dropdown.style.paddingRight = '10px';
            dropdown.style.cursor = 'pointer';

            // Add "Copy" item
            const copyItem = document.createElement('div');
            copyItem.textContent = 'Copy';
            dropdown.style.marginBottom = '5px';
            copyItem.addEventListener('click', async () => {
                try {
                    const textToCopy = document.selection.createRange().text;
                    alert(textToCopy)
                    await navigator.clipboard.writeText(textToCopy);
                    dropdown.remove();
                    console.log(`Text '${textToCopy}' copied to clipboard successfully.`);
                } catch (error) {
                    console.error(`Error copying to clipboard: ${error}`);
                }
            });
            dropdown.appendChild(copyItem);

            // Add "Paste" item
            const pasteItem = document.createElement('div');
            pasteItem.textContent = 'Paste';
            dropdown.style.marginBottom = '5px';
            pasteItem.addEventListener('click', async () => {
                try {
                    const clipboardText = await navigator.clipboard.readText();
                    console.log(`Pasted text: ${clipboardText}`);
                    dropdown.remove();
                } catch (error) {
                    console.error(`Error pasting from clipboard: ${error}`);
                }
            });
            dropdown.appendChild(pasteItem);

            document.body.appendChild(dropdown);
        });

    </script>

So, we can prevent the event from hapening, but we limit somehow the user (he can't do right-click copy/paste).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants