2014-04-24 78 views
0

我正在使用kango框架開發跨瀏覽器擴展。我想將我的擴展名添加到瀏覽器的上下文菜單中。我已經在extension_info.json文件中的權限數組中設置了「context_menu」:true,但我的擴展名仍未出現在上下文菜單中(當瀏覽器窗口中有右擊時)。我怎樣才能做到這一點?擴展出現在瀏覽器的上下文菜單中

回答

1

您需要添加一個事件偵聽main.js這樣的:

kango.ui.contextMenuItem.addEventListener(kango.ui.contextMenuItem.event.CLICK, function()  { 
    kango.browser.tabs.getCurrent(function(tab) { 
    tab.dispatchMessage('ContextMenuItemClick'); 
    }); 
}); 

而在content.js需要消耗這樣的事件:

function handleContextMenuClick() { 
var clickedElement = null; 

if ('addEventListener' in document) { 
    document.addEventListener('mousedown', function(event) { 
     if (event.button == 2 && IsSupported()) { 
      clickedElement = event.target;    
      kango.console.log('StopIt menu item click 1'); 
     } 
    }, true); 
} else { 
    document.attachEvent('onmousedown', function(event) { 
     event = event || window.event; 
     if (event.button == 2&& IsSupported()) { 
      clickedElement = event.srcElement; 
      kango.console.log('StopIt menu item click 2'); 
     } 
    }); 
} 

kango.addMessageListener('ContextMenuItemClick', function(event) { 
    kango.console.log("addMessageListener: ContextMenuItemClick added"); 
    }); 
} 

handleContextMenuClick(); 


// Only activate the menu when the user is on facebook or twitter. 
// This should be loaded from a service and updated one each new domain visited. 
function IsSupported() 
    { 
    if(document.domain.indexOf("facebook.") > -1) return true; 
    if(document.domain.indexOf("twitter.") > -1) return true; 

    return false; 
    } 
+0

才能打開彈出通過點擊上下文菜單? –

+0

kango.ui.browserButton.setPopup({url:'popup.html'}); –

相關問題