Page 1 of 2

How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 10:43 am
by Jan2004
In my PureBasic code, I've implemented a WebViewGadget in my program that opens documents located on the server. How can I prevent the displayed file from being saved by right-clicking on the document and selecting "Save As," or how can I prevent commands from appearing in the context menu when right-clicking on the document? I have access to this HTML document, which is hosted on the server, and I can also make changes to the HTML.

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 11:21 am
by RASHAD
Removed
Missed the target

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 11:22 am
by infratec
Via JS:

Code: Select all

document.addEventListener('contextmenu', event => {
    event.preventDefault();
});
You schould be able to inject this, without needing to change the html pages.


PB example:

Code: Select all

WebViewExecuteScript(0, "document.addEventListener('contextmenu', event => { event.preventDefault(); });")

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 11:33 am
by infratec
Uncomment the WebViewExecuteScript() to see the effect.

Code: Select all

OpenWindow(0, 100, 100, 400, 400, "Hello", #PB_Window_SystemMenu)

WebViewGadget(0, 0, 0, 400, 400)
SetGadgetText(0, "file://" + #PB_Compiler_Home + "examples/sources/Data/WebView/webview.html")

;WebViewExecuteScript(0, "document.addEventListener('contextmenu', event => { event.preventDefault(); });")

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow


Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 12:04 pm
by Jan2004
infratec: Thank you for the quick solution. It works perfectly.

By the way, I have an (educational) question:
When I was looking for a solution, I asked ChatGPT, which gave me this code:

Code: Select all

WebViewExecuteScript(0, "document.addEventListener('contextmenu', event => event.preventDefault());") - similar to the one you provided.
Your code:

Code: Select all

WebViewExecuteScript(0, "document.addEventListener('contextmenu', event => { event.preventDefault(); });")
The difference is adding { before event.prevent and ;} before the parentheses. The ChatGPT code didn't work, but your code does. What do these three characters change?

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 12:33 pm
by infratec
If you look at the JS code, you can see that the { } are used to start a function and end a function.
They are needed in this case, because else JS does not know when the function ends.
It interpete the following ) as part of the function and generates a syntax error.

Look also here:
https://www.w3schools.com/js/js_arrow_function.asp

About KI modells: at the moment I need more time to debate with the model about the fantasies it produce, than waiting for a working answer
in this forum :wink:

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 1:27 pm
by Jan2004
infratec: Thank you very much for your willingness to help and for your answers.

If I'm not abusing your goodwill and friendly attitude, I have one more question. Whenever I'm in Warsaw, I visit the private museum Villa la Fleur (a dozen or so kilometers from Warsaw), which I really like. The museum's websites are secured similarly to the one you showed in the code you shared. But not only that: when I right-click several times in a browser window, the message "ALERT. Content is protected!!" appears.

Is it possible to do something similar in PureBasic for WebViewGadget with the additional option of custom text?

https://villalafleur.com/kolekcja/

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 2:23 pm
by infratec

Code: Select all

OpenWindow(0, 100, 100, 400, 400, "Hello", #PB_Window_SystemMenu)

WebViewGadget(0, 0, 0, 400, 400)
SetGadgetText(0, "file://" + #PB_Compiler_Home + "examples/sources/Data/WebView/webview.html")

WebViewExecuteScript(0, "document.addEventListener('contextmenu', event => { event.preventDefault(); });")

JS$ = "var click_count = 0;" + #CRLF$
JS$ + "function Prot() {" + #CRLF$
JS$ + " click_count++;" + #CRLF$
JS$ + " if(click_count == 3){" + #CRLF$
JS$ + "  click_count = 0;" + #CRLF$
JS$ + "  confirm('You have clicked three times!');" + #CRLF$
JS$ + " }" + #CRLF$
JS$ + "}" + #CRLF$
JS$ + "document.addEventListener('contextmenu', event => { Prot(); });"

Debug JS$

WebViewExecuteScript(0, JS$)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 2:30 pm
by Jan2004
infratec: Again, an incredibly accurate solution, for which I sincerely thank you. Your codes also serve an educational purpose for me.

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Sun Nov 23, 2025 10:07 pm
by BarryG
Just be aware that there's browser extensions that allows right-clicks on any website (I use one myself). That probably doesn't work with the WebViewGadget, though.

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Mon Nov 24, 2025 8:38 am
by Jan2004
BarryG: Thank you for your important remark.

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Mon Nov 24, 2025 8:46 am
by BarryG
No problem. For example, I can right-click anywhere on Villa la Fleur's website to get the context menu. I don't see any "Content protected" alert.

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Mon Nov 24, 2025 11:24 am
by Jan2004
BarryG: Right-click at least twice.

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Mon Nov 24, 2025 12:22 pm
by BarryG
I did, in fact many times. ;) No alerts. Video: https://i.imgur.com/5o6p1gN.mp4

Re: How can I eliminate the context menu in a document opened in WebViewGadget?

Posted: Mon Nov 24, 2025 3:37 pm
by Jan2004