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

Just starting out? Need help? Post your questions and find answers here.
Jan2004
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jan 07, 2005 7:17 pm

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

Post 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.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5008
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Removed
Missed the target
Last edited by RASHAD on Sun Nov 23, 2025 11:35 am, edited 1 time in total.
Egypt my love
infratec
Always Here
Always Here
Posts: 7691
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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(); });")
infratec
Always Here
Always Here
Posts: 7691
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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

Jan2004
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jan 07, 2005 7:17 pm

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

Post 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?
infratec
Always Here
Always Here
Posts: 7691
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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:
Jan2004
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jan 07, 2005 7:17 pm

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

Post 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/
infratec
Always Here
Always Here
Posts: 7691
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
Jan2004
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jan 07, 2005 7:17 pm

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

Post by Jan2004 »

infratec: Again, an incredibly accurate solution, for which I sincerely thank you. Your codes also serve an educational purpose for me.
BarryG
Addict
Addict
Posts: 4250
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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.
Jan2004
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jan 07, 2005 7:17 pm

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

Post by Jan2004 »

BarryG: Thank you for your important remark.
BarryG
Addict
Addict
Posts: 4250
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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.
Jan2004
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jan 07, 2005 7:17 pm

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

Post by Jan2004 »

BarryG: Right-click at least twice.
BarryG
Addict
Addict
Posts: 4250
Joined: Thu Apr 18, 2019 8:17 am

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

Post by BarryG »

I did, in fact many times. ;) No alerts. Video: https://i.imgur.com/5o6p1gN.mp4
Post Reply