CSS injection for firefox addon not working

143 Views Asked by At

I'm working on a firefox addon that needs to inject css in the document.

Here is a part of the manifest.json file.

"content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["inject_css.js"]
  }
],
  "permissions": [
    "storage",
    "<all_urls>",
    "activeTab",
    "tabs"
  ]

I tried this code (inject_css.js):

function restoreOptions() {

  function onError(error) {
    console.log(`Error: ${error}`);
  };

  function setCurrentChoice(result) {
    var css = '::selection { background: ' + result.background_color || "auto" + '; color: ' + result.color || "auto" + '}';
    var insertingCSS = browser.tabs.insertCSS({code: css});
    insertingCSS.then(null, console.log("Injection error !"));
  };

  let getting = browser.storage.sync.get();
  getting.then(setCurrentChoice, onError);

};

document.addEventListener("DOMContentLoaded", restoreOptions);

But the css isn't injected but ther isn't any error. However, the code is executed because I tried adding some alert().

You can install the addon here

0

There are 0 best solutions below