0

As you can see from the code below, I'm checking the contents of manifest.json to get the matches and exclude_matches in my content_scripts option. I think it works as intended, because I'm seeing the action active only in the URLs listed in "matches" property, not in the ones in "exclude_matches". However, this seems to be too error-prone, because I may have to modify the string replacements in regExpFromMatch if I need to update these match patterns in the future. Is this really making the extension inactive or is it just the action in the toolbar? And, in order to avoid the probable need for repetitive refactoring in the future, I'd like to know if there's a simpler, safer, way to achieve this.

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  if (changeInfo.status === "complete") {
    const {
      content_scripts: [{ matches, exclude_matches }],
    } = chrome.runtime.getManifest();

    const actionEnabled = isActionEnabledInThisURL(
      [matches, exclude_matches],
      tab.url
    );

    if (actionEnabled) {
      chrome.action.enable(tabId);
    } else {
      chrome.action.disable(tabId);
    }

    // *************************************************************

    function isActionEnabledInThisURL(contentMatchPatterns, url) {
      const [isMatch, isExcludedMatch] = contentMatchPatterns.map(
        (urlMatchPattern) =>
          regExpFromMatch(urlMatchPattern).some((regExp) => regExp.test(url))
      );

      return isMatch && !isExcludedMatch;
    }

    function regExpFromMatch(matchArray) {
      return matchArray.map(
        (string) =>
          new RegExp(string.replace(/\//g, `\/`).replace(/\*/g, ".*"), "g")
      );
    }
  }
});
mjfneto
  • 93
  • 1
  • 8
  • You only change the icon. To change `content_scripts` you need to remove it from manifest.json and use chrome.scripting.registerContentScripts instead (in the background script's onInstalled event). – wOxxOm Aug 15 '22 at 17:48
  • Thanks, but I don't actually need to change the `content_scripts`. Or am I missing something in your comment? – mjfneto Aug 15 '22 at 18:36
  • What do you mean by "inactive"? Your extension already runs only in matched sites and not in excluded ones. – wOxxOm Aug 16 '22 at 04:49
  • Some extensions I use, depending on the URL, make the action icons greyed out, and popup is "disabled" (the right word). – mjfneto Aug 16 '22 at 12:28
  • 1
    Assuming your extension does something meaningful when the icon is clicked, a better method is declarativeContent API, [example](https://stackoverflow.com/a/64475504/) + call chrome.action.disable() inside chrome.runtime.onInstalled listener. Otherwise, you don't need the background script and `action` in manifest.json, Chrome should change the icon automatically. – wOxxOm Aug 16 '22 at 12:52
  • Um interesting. Do you have a reference for that? – mjfneto Aug 16 '22 at 14:21
  • For what? I've linked an example. – wOxxOm Aug 16 '22 at 14:29
  • Oops Sorry, I missed that link. I'll test it. It's good to know that it's a bug. Thank you very much. – mjfneto Aug 16 '22 at 15:09

0 Answers0