Search code examples
javascriptfirefox-addonfirefox-addon-sdk

Cannot intercept context menu item click


Can anyone tell me what is wrong with this code? When user selects a word and does a right-click he can choose 'Open Wiki-Link' - that's working fine. But for some reason nothing happens on a click, the code in onMessage is not been executed. Why?

exports.main = function() {

    var tabs = require('tabs');
    //var sel = require('selection');
    var cm = require('context-menu');

    var menuItem = cm.Item({
        label: 'Open Wiki-Link',
        context: cm.SelectionContext(),
        contextScript: 'self.on("click", function() {' +
            'var text = window.getSelection().toString();' +
            'self.postMessage(text);' +    
        '});',
        onMessage: function(text) {
            if (text.length === 0) {
                throw ('No text selected');
            }
            tabs.open('http://de.wikipedia.org/wiki/' + text);
        }
    });
};

Solution

  • Your code seems correct and matches the examples from the documentation pretty closely. I think that the only issue is a typo: it should be contentScript, not contextScript.