Can't seem to get chrome.contextMenus.onClicked to fire in Chrome extension

Viewed 5353

I'm trying to write my first browser extension and I can't seem to get the click-handler to fire with a context menu item is clicked. The console.log calls don't seem to spit anything out to the console, and I tried using alert() just in case the "background" scripts (not sure what the difference between background and content scripts is exactly yet), but that didn't seem to do anything either.

When I select some text in an input in my test page, right-click and select either Encrypt or Decrypt, nothing happens; not in the console or in the network tabs in developer tools. What am I doing wrong?

manefest.json:

{
    "manifest_version": 2,

    "name": "my name",
    "description": "my description",
    "version": "1.0",

    "background": {
        "scripts": ["jquery-3.2.1.min.js", "background.js"],
        "persistent": false
    },
    "content_scripts": [
        {
            "matches": ["http://*/*", "https://*/*"],
            "js": ["content_script.js"]
        }
    ],
    "permissions": [
        "activeTab",
        "contextMenus",
        "http://my.hostname.com/"
    ]
}

background.js:

/**
 * A handler which will run the analysis of the DOM element's selected text
 */
function clickHandler(info, tab) {
    "use strict";
    console.log(info);
    console.log(tab);
    // get selected text as encrypt/decrypt
    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, "get selection", null, function(selection) {
            var data = {
                "action": action,
                "selection": selection
            };
            var max_length = 4095;
            if (data.selection.length > max_length) {
                data.selection = data.selection.substring(0, max_length);
            }
            var url = "http://my.hostname.com/";
            jQuery.post(url, data, function (response) {
                chrome.tabs.sendMessage(tabs[0].id, response);
            }, "json");
        });
    });
}

/**
 * Create a context menu items to allow encode/decode
 */
chrome.runtime.onInstalled.addListener(function() {
    chrome.contextMenus.create({
        "id": "encrypt",
        "title": "Encrypt",
        "type": "normal",
        "contexts": ["editable"]
    });

    chrome.contextMenus.create({
        "id": "decrypt",
        "title": "Decrypt",
        "type": "normal",
        "contexts": ["selection"]
    });

    chrome.contextMenus.onClicked.addListener(clickHandler);
});
1 Answers
Related