简体   繁体   中英

Firefox Add-on sdk: Right click on submenu context to show panel

I have a context-menu that expands to two sub-menus. I would like to click on the sub-menu to display a pop-up menu where I will allow for user input. I have combed the web for some help but in vain. Here is what my main.js looks like;

/**
* main.js file defining context mmenu using Item and Menu
*
**/

var cm = require("sdk/context-menu");
var data = require("sdk/self").data;

/**
* Construct a panel, loading its content from the "text-entry.html"
* file in the "data" directory, and loading the "get-text.js" script
*  into it.
**/
var textEntry = require("sdk/panel").Panel({
  contentURL: data.url("text-entry.html"),
  contentScriptFIle: data.url("get-text.js")
});

var quickInkItem = cm.Item({
  label: "Quick Ink",
  contentScriptFile: data.url("testscript.js")
});

var inkToBoardItem = cm.Item({  
   label: "Ink to Board", 
   onClick: handleClick, //Does not work 
   contentScriptFile: data.url("testscript.js")
});

var inkLibsMenu = cm.Menu({
  label: "inkLibs",
  context: cm.SelectorContext("a[href]"),
  items: [quickInkItem, inkToBoardItem]
});

//Show panel when user clicks the ink-to-Board submenu 
function handleClick(){
  textEntry.show();
}

//When the panel is displayed, it generates an event called 'show'
//We will listen to that event and when it happens, send our own "show" event 
//to the panel's script, so the script can prepare the panel for display
textEntry.on('show', function(){
  textEntry.port.emit("show");
});

//Listen for the messages called "text-entered" coming from the content script
//The message payload is the text the user entered.
textEntry.port.on("text-entered", function(text){
  console.log(text);
  textEntry.hide();
}); 

You're close, but not quite there. The current context-menu api does not have an onClick handler, instead you need to use content scripts to handle clicks and message back to the main add-on code manually. It's not great, and we have plans to improve it.

For each context menu item, you need to instead listen for a message event by crearting an onMessage handler:

var quickInkItem = cm.Item({
  label: "Quick Ink",
  onMessage: handleClick, // Works! 
  contentScriptFile: data.url("testscript.js")
});

More importantly, you need to add something like the following code to 'testscript.js':

self.on("click", function(node) {
  console.log(node.href);
  self.postMessage(true); // you could send data as well
});

See the documentation about handling clicks for more info .

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