How can I eliminate the context menu in a document opened in WebViewGadget?
How can I eliminate the context menu in a document opened in WebViewGadget?
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?
Removed
Missed the target
Missed the target
Last edited by RASHAD on Sun Nov 23, 2025 11:35 am, edited 1 time in total.
Egypt my love
Re: How can I eliminate the context menu in a document opened in WebViewGadget?
Via JS:
You schould be able to inject this, without needing to change the html pages.
PB example:
Code: Select all
document.addEventListener('contextmenu', event => {
event.preventDefault();
});
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?
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?
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:
Your code:
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?
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.
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?
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
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
Re: How can I eliminate the context menu in a document opened in WebViewGadget?
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/
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?
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?
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?
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?
BarryG: Thank you for your important remark.
Re: How can I eliminate the context menu in a document opened in WebViewGadget?
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?
BarryG: Right-click at least twice.
Re: How can I eliminate the context menu in a document opened in WebViewGadget?
I did, in fact many times.
No alerts. Video: https://i.imgur.com/5o6p1gN.mp4



