2

I have this problem with Mozilla Firefox. I made an addon that finds selected text in a dictionary by opening an online dictionary with translated selected text.

Problem: When I clicked to my custom function in Contexts Menu it should open Online dictionary in new tab unfortunetly it doesn't open anything. In the Contexts Menu I still see my addon option to "find the word in dictionary", but it just doesn't want to open.

my background.js script used for Mozilla.

chrome.contextMenus.create({
    id: "1",
    title: "Najít \"%s\" ve slovníku",
    contexts: ["selection"],
    onclick: openTab(),
});



function openTab() {
    return function (info, tab) {
        let text = info.selectionText;
        let dicLink = "https://slovnik.seznam.cz/preklad/nemecky_cesky/" + text

        chrome.tabs.create({ index: tab.index + 1, url: dicLink, selected: true });
    }
};

Same background.js script but for Chrome:

chrome.contextMenus.create({
    id: "1",
    title: "Najít \"%s\" ve slovníku",
    contexts: ["selection"],
});

chrome.contextMenus.onClicked.addListener(
    openTab()
)

//opens tab and give selected text into german to czech dictionary
function openTab() {
    return function (info, tab) {
        let text = info.selectionText;
        let dicLink = "https://slovnik.seznam.cz/preklad/nemecky_cesky/" + text
       
        chrome.tabs.create({ index: tab.index, url: dicLink, selected: true });
    }
};

Why am I asking: I ask because I used nearly same code to use on Google Chrome where it works perfectly, and from the documentation of Firefox I understood, that the Chrome code should be compatible with Firefox, so I really don't understand what is wrong...

Good to know: For the Chrome version of my addon I used ManifestV3, I have been told that I should use it. On the other hand, Firefox says that it still only supports ManifestV2.

Chrome manifest

{
    "manifest_version": 3,
    "name": "Německý Seznam slovník vyhledáváč",
    "version": "1.0",
    "description": "Otevře německý slovník od Seznamu.",
    "permissions": [
        "contextMenus"
    ],
    "background": {
        "service_worker": "js/background.js"
    },
    "browser_action": {
        "default_icon": "images/icon2.png"
    },
    "icons": {
        "16": "images/icon2.png",
        "32": "images/icon2.png",
        "48": "images/icon2.png",
        "128": "images/icon2.png"
    }
}

Firefox manifest { "manifest_version": 2, "name": "Seznam-slovnik vyhledavac", "version": "1.0", "description": "Otevře německý slovník od Seznamu.", "permissions": [ "contextMenus", "tabs" ], "background": { "scripts": [ "js/background.js" ] }, "browser_action": { "default_icon": "images/icon2.png" }, "icons": { "16": "images/icon2.png", "32": "images/icon2.png", "48": "images/icon2.png", "128": "images/icon2.png" } }

LEXA
  • 21
  • 1
  • 1
    If you open [console of the background script](https://stackoverflow.com/q/38913799) you should see an error saying `onclick` cannot be used with service_worker. Use chrome.contextMenus.onClicked as shown in examples instead of onclick. – wOxxOm Jan 16 '23 at 16:52

0 Answers0