skip to Main Content

I am writing a chrome extension and it needs to work on multiple sites.
Is there away I can modify this tabs.query to detect additional urls other than amazon?

It would be better if there was a way to detect website tags like for shopping or video related websites. Im very new to chrome extension programming.

chrome.tabs.query({ url: 'https://www.amazon.com/*', }, (tabs) => {
      alert("site detected");
      tabs.forEach(tab => {
        chrome.tabs.remove(tab.id);
      });
    });

2

Answers


  1. Just filter them out later. Since you’ll be using MV3, you can do it like this:

    (await chrome.tabs.query({})).filter(t => 
      ['https://www.amazon.com/', 'https://www.example.com/']
      .some(m => t.url.startsWith(m))
    ).forEach(t => chrome.tabs.remove(t.id))
    
    Login or Signup to reply.
  2. You can pass multiple URLs as an array.

    chrome.tabs.query({ url: [ /* array of URL patterns */ ]}, tabs => {
        // ...
    });
    

    See the documentation. It states the following for the url property of queryInfo:

    Match tabs against one or more URL patterns. Fragment identifiers are not matched. This property is ignored if the extension does not have the "tabs" permission.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search