Firefox Add-on sdk:右键单击子菜单上下文以显示面板

时间:2014-11-01 20:37:46

标签: javascript jquery firefox-addon-sdk

我有一个上下文菜单,可以扩展到两个子菜单。我想点击子菜单显示一个弹出菜单,我将允许用户输入。我已经梳理了网络以获得一些帮助但是徒劳无功。这是我的main.js的样子;

/**
* 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();
}); 

1 个答案:

答案 0 :(得分:0)

你关闭了,但并不完全。当前的上下文菜单api没有onClick处理程序,而是需要使用内容脚本来手动处理点击和消息回主要附加代码。它并不好,我们有计划改进它。

对于每个上下文菜单项,您需要通过创建onMessage处理程序来监听消息事件:

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

更重要的是,您需要将以下代码添加到' testscript.js':

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

请参阅有关处理点击次数for more info的文档。