extension appearing in the context menu of the browsers

329 views Asked by At

I am developing a cross browser extension using kango framework. I want to add my extension to the context menu of the browser. I already have set "context_menu": true in the permissions array in extension_info.json file, but still my extension does not appear in the context menu ( when there is a right click in the browser window). How can I do this?

1

There are 1 answers

2
Dave Gordon On

You need to add an event listener to main.js like this:

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

And in the content.js you need to consume the event like this:

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;
    }