[英]Firefox Add-on sdk: Right click on submenu context to show panel
我有一個上下文菜單,可擴展為兩個子菜單。 我想單擊子菜單以顯示一個彈出菜單,允許用戶輸入。 我梳理了網絡,但徒勞無功。 這是我的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
});
有關更多信息,請參見有關處理點擊的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.