How to disable right click menu in HTMLViewer

Hi,

Is there a way to disable right click menu in HTMLViewer?
I draw JavaScript Charts using HTMLViewer and want to hide right-click menu.

Thanks.

Use a JavaScript.

There are plenty examples on the internet (in both web site pages and tutorials).

Here even text selection is prohibited !

[Yes, I dislike that; but it is possible]

Note: there is always a way to store on the user disk the text, images, sounds, video, etc. you put on an internet page.

Const Context As String :

[code]document.addEventListener(“contextmenu”, myFunction);

function myFunction(e) {e.preventDefault(); return false;
}[/code]

[code]Sub DocumentComplete(URL as String)
me.ExecuteJavaScript(context)

End Sub
[/code]

[code][/code]

Great!

This is the reason I like Xojo. There are so many experts in this forum.

Thank you again.
It works now.

[quote=211844:@changwon lee]Great!

This is the reason I like Xojo. There are so many experts in this forum.

Thank you again.
It works now.[/quote]

The method I posted for you first with very precise instructions may be short, it works perfectly. I took the time to test it and not simply to paste some code found on the Internet. I find it unfair that you marked the second reply as answer when it is not even specific to Xojo. You really do not encourage efforts to help you :confused:

Michel,

I’m ceartain, many others appreciate your code. Please don’t be discouraged!

Thank you, Tim.

Ha ha. wish I’d found this yesterday… I instead wrote a bunch of declares and handled the delegate… It was more code than Emile’s Javascript!

[quote=211833:@Michel Bujardet]Const Context As String :

[code]document.addEventListener(“contextmenu”, myFunction);

function myFunction(e) {e.preventDefault(); return false;
}[/code]

[code]Sub DocumentComplete(URL as String)
me.ExecuteJavaScript(context)

End Sub
[/code][/quote]

Is there something similar for left mouse click?

have found something that works in OSX

[code]document.addEventListener(“click”,handler,true);

function handler(e){
e.stopPropagation();
e.preventDefault();
}[/code]

Michel:

Sorry about you felt like that.
I really got your help so much and without that, I couldn’t think I would proceed my project.
It seemed that I was not cautious about it.

Always appreciate your effort. Of course.

Thanks.

[quote=230307:@Axel Schneider]have found something that works in OSX

[code]document.addEventListener(“click”,handler,true);

function handler(e){
e.stopPropagation();
e.preventDefault();
}[/code][/quote]

Good. You found the solution before I had a chance to post it. Basically, any event can be trapped like that. BUT be aware that it will shunt click for all elements on the page. This includes links.

To reinstate click or the contextual menu, see removeListener http://www.w3schools.com/jsref/met_element_removeeventlistener.asp

lol shao shen and I are usually knee* deep in HTMLViewer anyway, you should have just asked!

For some reason I never thought of using JavaScript, I just went straight in to the WebView documentation, said swear words a lot, procrastinated and then did what I thought needed to be done, only to find I did the more inefficient method .

I must be getting old man.

Michel, in WebHtmlViewer there is no such an event as DocumentComplete. So where I have to put it?
I tried to call it after loading a pdf doc but does not work.

Unfortunately, Micheal, WebHTMLViewer is an iFrame, which means there are strict limitations to accessing the content.

You got to use Javascript to access the document inside the iFrame like this:
https://www.w3schools.com/jsref/prop_frame_contentdocument.asp
https://www.w3schools.com/howto/howto_js_element_iframe.asp

I believe the event loadeddata is what you are looking for.
https://www.w3schools.com/jsref/dom_obj_event.asp

To implement it, use addeventlistener
https://www.w3schools.com/jsref/met_element_addeventlistener.asp

You can then trigger an event in a WebSDK object with Xojo.triggerServerEvent(). See WebControlsSDK.pdf in the Extra folder next to the Xojo executable.

I never needed to implement that particular type of event, so unfortunately I have no turnkey ready code for you. Sorry. Hope giving you all the needed references will help you develop your own.

Thanks alot… i wonder if using just xojo code if there Is anyother way so to restrict the user from downloading a pic or pdf from a htmlviewer (as long as the user by by pressing right click, a menu opens for save/download) ?

Years ago, some web sites owners did not want users to be able to download pictures. So they used some Javascript to disable right click altogether. Guess what ? People found workarounds.

It is so easy to download pictures with the developer tools, trying to prevent that is kind of laughable.

Heck, even without diving into the page code, grabbing a screen shot is so easy, trying to disable right click is futile.

Note that it takes 30 seconds and google to learn these tricks.

You are right but i am talking for lay people. For those they want and can whatever method will fail.