2014-11-01 72 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的文檔。

相關問題