0

I am making a chrome extension that changes the formatting of suggestions on an internal CMS our school newspaper uses. I made a context menu option that, when clicked, will perform the formatting change. However, when the context menu is clicked, nothing happens.

1) I have checked that my code for makeVisible works on the javascript console in Chrome.

2) I have made sure the function makeVisible is never called because the console.log("Test") never happens.

3) I have tried switching the plugin to another website besides superdesk.thetriangle.org, still doesn't work.

4) Tried making the background.js script persistent and using the onclick attribute of chrome.contextMenus.create(). Same thing there, onclick never fires.

manifest.json:

{
    "name": "Find Hidden Suggestions",
    "version": "1.0",
    "description": "Highlights hidden suggestions in Superdesk's editor.",
    "manifest_version": 2,
    "permissions": ["*://superdesk.thetriangle.org/*", "contextMenus"],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    }
}

background.js:

function makeVisible(info, tab) {
    console.log("Test");
    var spansAdd = document.querySelectorAll("span[style='color: rgb(0, 180, 0);']");
    var spansDel = document.querySelectorAll("span[style='color: rgb(255, 0, 0); text-decoration: line-through;']");

    spansAdd.forEach((ele) => {
        ele.setAttribute("style", "color: green; border-bottom: 2px solid green;");
    });

    spansDel.forEach((ele) => {
        ele.setAttribute("style", "color: rgb(255, 0, 0); text-decoration: line-through; border-bottom: 2px solid red");
    });
}

chrome.contextMenus.create({
    id: "Suggestions command",
    title: "Make Suggestions Visible",
    contexts: ["all"],
});

chrome.contextMenus.onClicked.addListener(makeVisible);

At this point, I am at a loss as to why a click on the contextMenu is not working.

  • The background script runs in a separate hidden background page so changing its DOM won't do you no good. You need an additional content script. – wOxxOm Jun 11 '19 at 02:17
  • That would explain it. I am relatively new to web development. Thanks! – Brandon K Jun 11 '19 at 23:27
  • 1
    I get an `onClicked of undefined` error when I move the `chrome.contextMenus.onClicked.addListener` to the content script – Johan Aspeling Apr 30 '20 at 17:39

0 Answers0