Question

Is there any reference implementation on how to do this?

The entire app has an iframe with third party content that I cannot modify. I was able to set a context menu in a blank app, but it doesn't appear when the iframe takes over the entire window.

(I use $(document).on("contextmenu", ..)

How can I make it work in the iframe too?

Was it helpful?

Solution 2

This is in response to tinyproxy's script...

The onload event is great for reporting an iframe's TITLE and URL as the user navigates but I wouldn't use it for clicks because every time the user navigates somewhere (using the left click) your script will be appending a new listener because the onload event will have triggered as the frame loads with new content!

A more correct way to listen for a right-click is…

FrameID.addEventListener('contextmenu',function(){Whatever();});

OTHER TIPS

You should append listener to iframe's content window. Here is a sample code, I think it should fit your needs.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf8">
    <title></title>
    <style type="text/css">
        #gframe{
            width: 100%;
            height: 100%;
            left: 0px;
            top: 0px;
            position: absolute;
        }
    </style>
    <script type="text/javascript">
        var appendContextmenuListener = function(){
            var gframe = document.getElementById('gframe');
            var subWindow = gframe.contentWindow;
            var subDocument = subWindow.document;
            subDocument.oncontextmenu = function(){
                console.log(1);
            }
        };
    </script>
</head>
<body>
    <iframe id="gframe" src="https://www.google.com.hk" 
        onload="appendContextmenuListener()"></iframe>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top