I'm having a problem developing a Chrome extension, does anyone know if there's a possibility of opening my extension from Chrome's contextMenus? Right mouse click to open dont work?

Example: In background.js


    chrome.runtime.onInstalled.addListener(function () {
        chrome.contextMenus.create({
            title: "Test Ex",
            contexts: ["all"],
            id: "test"
        });
    });


    chrome.contextMenus.onClicked.addListener(function (info, tab) {
        if (info.menuItemId === "test") {
            chrome.scripting.executeScript({
                target: { tabId: tab.id },
                function: () => {
                    chrome.runtime.sendMessage({ action: "executeAuth" 
                }
             });
         }
    });

In content.js


    chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
        if (message.action === "executeAuth") {
            executeAuth();
        }
    });

    function executeAuth() {
        window.location.replace(`../Views/home.html`);
    }


But it doesn't work, is there any way I can do this? I tried several solutions I found, but nothing managed to do what I want.

I need help with this problem, I've already tried several things, but the best thing would be to use the pop-up, but it's horrible (ugly). If you can send an example if you have already done this, in manifest v-3.

1

There are 1 best solutions below

0
On

You can fix your issue as wOxxOm said. Here is my solution in the same way:

In background.js:

        chrome.runtime.onInstalled.addListener(function () {
        chrome.contextMenus.create({
            title: "Test Ex",
            contexts: ["all"],
            id: "test"
        });
    });

    chrome.contextMenus.onClicked.addListener(function (info, tab) {
        if (info.menuItemId === "test") {
            chrome.tabs.sendMessage(tab.id, { action: "executeAuth" });
        }
    });

The content.js has no changes.

Read more at here: Chrome extension message passing