简体   繁体   中英

Change gContextMenu flags in Firefox extension

In my extension I need, under certain circumstances, to display the context menu as if it had been clicked on a different element. Eg: I right-click on a certain element on a page, but the context menu should be displayed as if under the cursor there wasn't the element above, but rather a different element I specify.

I tried doing this in the popupshowing handler this by:

window.addEventListener( "load", function() {
    var contextMenu = document.getElementById("contentAreaContextMenu");
    if (contextMenu) {
        contextMenu.addEventListener("popupshowing", function(event) {
            if (document.popupNode !== newElement) {
                event.preventDefault();
                synthesizeMouse(newElement, 2, 2, { type: "contextmenu", button: 2 }, newElement.ownerDocument.defaultView);
            }
        }, false);
    }
}, false );

newElement is the element that should be "clicked on", and synthesizeMouse is defined here . Unfortunately, it doesn't work (if I actually click on newElement the context menu is shown correctly, if I click anywhere else the context menu is not shown at all). Can anybody see why this isn't working?

Why not set document.popupNode to your element, call gContextMenu.init(contextMenu, gBrowser); and then clear document.popupNode again?

This is just a bit of a wild guess, but because you're doing this while the context menu from the first event is already opening, maybe this is is blocking the context menu from opening again. Try sending the event 10ms later, so that the original context menu has already closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM