OshoParth
OshoParth

Reputation: 1552

How to call the a predefined function on click of a context menu?

I have added a context menu item on a page for my add-on. On click of this context menu item i want one of my predefined function to be executed but am unable to figure out a way for it

This is the code for context menu

var contextMenu = require("sdk/context-menu");
var menuItem = contextMenu.Item({
  label: "Log Selection",
  contentScript: 'self.on("click", onClick);',
});

function onClick(state) {
//This function calls many other functions thus can not be defined locally
            console.log("onClick() called");
}

assuming that there is a function onClick() which needs to be called on click of the context menu. How to call onClick() function?

Upvotes: 1

Views: 692

Answers (1)

Paul Heil
Paul Heil

Reputation: 293

This should work for you. Modified from my extension which returns information on the clicked node.

function unassignUSER(upChange) {
    console.log("onClick() called");
}

var contextMenu = require("sdk/context-menu");
var menuItem = contextMenu.Item({
    label: "Log Selection",
    contentScript: 'self.on("click", function (node) {' +
                 '  var upChange = "data"' +
                 '  self.postMessage(upChange);' +
                 '});',
    onMessage: function (upChange) {
        unassignUSER(upChange);
    }
});

Upvotes: 1

Related Questions