Check if a URL matches one of the patterns in a chrome extension manifest v3 content script "matches" field

191 Views Asked by At

I have a chrome extension in which I'm using the chrome.runtime.onInstalled event to reinject my scripts when an update happens.

chrome.runtime.onInstalled.addListener((details) => {
  installContentScript();
});

Inside of installContentScript() I have the following code snippet that reinjects my scripts into iframes. Its working, but it throws errors because its naively trying to reinject my scripts into ALL iframes.

chrome.webNavigation.getAllFrames({ tabId: id }, (frames) => {
          for (const frame of frames) {
            try{
                chrome.scripting.executeScript({
                    target: { tabId: id, frameIds: [frame.frameId] },
                    files: javaScripts,
                  });
            }catch(e){
                console.log(e);
            }
          }
        });

So my question is, given a url, how can I check to see if that url matches one of the patterns defined in my manifest?

{
  "matches": ["*://mail.google.com/*"], //<- this?
  "js": ["script.js"],
  "all_frames": true,
  "run_at": "document_idle"
}
0

There are 0 best solutions below