0

我正在使用 kango 框架开发跨浏览器扩展。我想将我的扩展添加到浏览器的上下文菜单中。我已经在 extension_info.json 文件的权限数组中设置了 "context_menu": true ,但我的扩展仍然没有出现在上下文菜单中(当在浏览器窗口中单击鼠标右键时)。我怎样才能做到这一点?

4

1 回答 1

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;
    }
于 2014-08-12T23:03:40.287 回答