简体   繁体   English

Firefox插件SDK:右键单击子菜单上下文以显示面板

[英]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的样子;

/**
* 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. 当前的上下文菜单api没有onClick处理程序,相反,您需要使用内容脚本来处理单击,并手动将消息发送回主附加代码。 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: 对于每个上下文菜单项,您需要改为通过创建onMessage处理程序来监听消息事件:

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': 更重要的是,您需要在“ 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 . 有关更多信息,请参见有关处理点击的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM