frisco
frisco

Reputation: 1917

Firefox context menu item not showing

I have a really simple extension and I want to show a new item on the context menu depending on the content, that should be pretty simple and there are a lot of tutorials showing how this must be done.

The problem is that as soon as I add the listener for popupshowing on the contecontentAreaContextMenu my new element it is not shown even if I just don't hide it.

Here is the xul code:

<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<?xml-stylesheet href="chrome://Prot-On/skin/Prot-On.css" type="text/css"?>

<!DOCTYPE window SYSTEM "chrome://Prot-On/locale/Prot-On.dtd">

<overlay id="Prot-On" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript" src="chrome://Prot-On/content/prot-on.js"/>

<popup id="contentAreaContextMenu">
    <menuitem id="prot-on-main-item" label="Hello, world!" oncommand="proton.hello();"/>
</popup>


</overlay>

And prot-on.js:

var proton = {
    init : function() {
        try {       
            if (document.getElementById("contentAreaContextMenu")) {
                document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", this.showContextMenu, false);
            }
        } catch (e) {
            this.manageException(e);
        }       
    },
    hello : function (){
        try {
            alert("Hello, world2!");
        } catch (e) {
            this.manageException(e);
        }
    }, 
    showContextMenu : function (event){
        try {
            var show = document.getElementById("prot-on-main-item");
            show.setAttribute("hidden", false);
        } catch (e) {
            this.manageException(e);
        }
    }, 
    manageException : function (exception) {
        window.openDialog(
            "chrome://Prot-On/content/resources/html/showErrorBacktrace.htm",
            "errorbacktrace",
            "centerscreen=yes,chrome=yes,modal=yes,resizable=yes",
            exception,
            "display error"
        );
    }   
}


try {
    window.addEventListener("load", proton.init(), false);
} catch (e) {
    proton.manageException(e);
}

If I just comment the lines:

    if (document.getElementById("contentAreaContextMenu")) {
        document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", this.showContextMenu, false);
    }

everything works as expected, so I am not really sure what is the problem.

The manageException function just displays additional information of any exception and its working properly, in case you wonder.

Upvotes: 1

Views: 866

Answers (1)

Wayne
Wayne

Reputation: 60414

You're immediately executing proton.init on window load instead of passing a reference to it to addEventListener.

Change this line:

window.addEventListener("load", proton.init(), false);

To:

window.addEventListener("load", function() {
    proton.init();
}, false);

Upvotes: 1

Related Questions