2014-11-01 37 views
0

我有一个上下文菜单,可以扩展到两个子菜单。我想点击子菜单来显示弹出式菜单,我将允许用户输入。我已经梳理了网络的一些帮助,但徒劳无功。这是我的main.js的样子;Firefox Add-on sdk:右键单击子菜单上下文以显示面板

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

回答

0

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

对于每个上下文菜单项,您需要通过crearting一个的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的文档。

相关问题