Replace or disable right click menu on Web Browser Control (1 Viewer)

lauro

Member
Local time
Today, 05:48
Joined
May 18, 2012
Messages
59
Hi,
I'm using the WebBrowserControl in an Access form to show HTML or PDF files.
When I right click on the control area I get the context menu on the left for HTML and on the right for PDF.
Screenshot 2023-05-08 221844.png
Screenshot 2023-05-08 222853.png


I would like to replace those menu with my own, but the WebBrowserControl doesn't expose a ShortcutMenuBar property as many other controls like ObjectFrame.
How can I accomplish that or at least to disable the above default ones?
Thanks, Lauro
 

561414

Active member
Local time
Today, 07:48
Joined
May 28, 2021
Messages
280
Something like this should work:
Code:
Sub DisableRightClick()
    Dim doc as Object
    Set doc = MyWebBrowser.Object.Document
    Dim scr as Object
    Set scr = doc.createElement("script")
    scr.Type = "text/javascript"
    scr.innerhtml = "function disableRightClick(event) {event.preventDefault();}"
    doc.head.appendchild scr
    doc.body.setAttribute "oncontextmenu", "disableRightClick(event)"
End Sub
Try it out, let me know how it goes. Remember, of course, to call that procedure from an event of your form, like Form_Open or Form_Load.
 

lauro

Member
Local time
Today, 05:48
Joined
May 18, 2012
Messages
59
Something like this should work:
Code:
Sub DisableRightClick()
    Dim doc as Object
    Set doc = MyWebBrowser.Object.Document
    Dim scr as Object
    Set scr = doc.createElement("script")
    scr.Type = "text/javascript"
    scr.innerhtml = "function disableRightClick(event) {event.preventDefault();}"
    doc.head.appendchild scr
    doc.body.setAttribute "oncontextmenu", "disableRightClick(event)"
End Sub
Try it out, let me know how it goes. Remember, of course, to call that procedure from an event of your form, like Form_Open or Form_Load.
Thank a lot Edgar,
at the line:
scr.innerhtml = "function disableRightClick(event) {event.preventDefault();}"
I'm receiving
1683627670478.png

I was calling the DisableRightClick sub at onLoad form.
 
Last edited:

561414

Active member
Local time
Today, 07:48
Joined
May 28, 2021
Messages
280
Can you post a sample to see how you're loading your pdfs?
 

lauro

Member
Local time
Today, 05:48
Joined
May 18, 2012
Messages
59
Hi Edgar
I uploaded a sample database with one table and one mask.
You should insert the fullname of same html files.
Thanks again, for your interest.
Lauro
 

Attachments

  • TestHTM.accdb
    968 KB · Views: 79

lauro

Member
Local time
Today, 05:48
Joined
May 18, 2012
Messages
59

561414

Active member
Local time
Today, 07:48
Joined
May 28, 2021
Messages
280
Try this one, it has two approaches, one with an embed tag and another with an object tag. I could only disable the browser's right click though.

Maybe check your pdf viewer's options to disable right click. Mine is acrobat reader and it has no option for that. I also tried applying an overlay div but the viewer overtakes the div and positions itself above. I didn't try that extensively, but there could be some other ways to accomplish what you need. It's important to mention that your viewer is what's allowing you to view the file. However, there are other approaches, maybe with an external library that supports ES5, or using an online viewer like Google Driver's viewer or some other viewer, you would have to upload your file there though.

Try to find a viewer that allows you to disable right click.

I have to mention that the attached file works on my end, it might not work on yours. If that's the case, let me know.
 

Attachments

  • TestHTM.accdb
    544 KB · Views: 88

lauro

Member
Local time
Today, 05:48
Joined
May 18, 2012
Messages
59
Try this one, it has two approaches, one with an embed tag and another with an object tag. I could only disable the browser's right click though.

Maybe check your pdf viewer's options to disable right click. Mine is acrobat reader and it has no option for that. I also tried applying an overlay div but the viewer overtakes the div and positions itself above. I didn't try that extensively, but there could be some other ways to accomplish what you need. It's important to mention that your viewer is what's allowing you to view the file. However, there are other approaches, maybe with an external library that supports ES5, or using an online viewer like Google Driver's viewer or some other viewer, you would have to upload your file there though.

Try to find a viewer that allows you to disable right click.

I have to mention that the attached file works on my end, it might not work on yours. If that's the case, let me know.
Thanks again Edgar.
I'm encountering the same error in both versions at line:
scr.innerhtml = "function disableRightClick(event) {event.preventDefault();}"

I found on the web the following file/ example that correctly disable right click in Firefox, Chrome and Edge.
Code:
<!DOCTYPE html>
<html>
<head>
   <title>Example</title>
</head>
<body>
   <h2>Disabling right-clicking on a webpage using JavaScript</h2>
   <p> Right Click is disabled</p>
   <script>
      document.addEventListener("contextmenu", (event) => {
         event.preventDefault();
      });
   </script>
</body>
</html>

When I try to visualize in my test TestHtm.accdb Access database, the WebControl blocks the execution of the script
1683729627392.png


If say Yes to the execution of the script I get syntatical errors at line 10 char 57 (the arrow function).
If I follow the What risks I could incour item menu, Internet Explore 11 opens at the page:

So I guess that the Microsoft Access WebBrowserControl does simulate Internet Explorer behaviour. I inserted my example/file that disable the right click in IE (in the window opened by Access, because I was unable to find the path where is located IE.exe, "C:\Program Files (x86)\Internet Explorer\iexplore.exe" actually opens Edge) and again the script was blocked; when accepting the execution no errors were reported, but the script does not work.

Does this information give you more clues?
Lauro
 

561414

Active member
Local time
Today, 07:48
Joined
May 28, 2021
Messages
280
I tried a few variations and I still haven't been able to do it with the Edge WebBrowser (Office 365). The code works fine in my IE WebBrowser though, the one I posted (Office 2016). It also works if you open it with the Edge Browser itself, not the one embedded in the web browser, I don't know if I'm explaining myself correctly. I tested a bunch of other javascript snippets and some work and others don't, I'm suspecting they butchered the Edge implementation for security or whatever.

In the IE WebBrowser, something as simple as this
Code:
    doc.body.setAttribute "oncontextmenu", "return false;"
    Set emb = doc.createElement("embed")
    emb.src = Me.FileName
    emb.Width = 500
    emb.Height = 400
    doc.body.appendchild emb
Is sufficient to block the right click, without using any script tag. It was so easy to use in IE. I mean, sure, it does not support ES6, but a lot could be done without it. I'll try a few extra approaches in a few days but do keep me posted.
 

Users who are viewing this thread

Top Bottom