我有一个上下文菜单,可以扩展到两个子菜单。我想点击子菜单显示一个弹出菜单,允许用户输入。我到处找人帮忙,但徒劳无功。以下是我的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();
});
你很接近,但还没有完全达到。当前的上下文菜单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
});
有关更多信息,请参阅有关处理单击的文档。