Search code examples
google-chrome-extension

Chrome extension content scripts not running on certain sites


I've been attempting to write a very simple Chrome extension (manifest v3) to automatically close those annoying tabs zoom leaves open after you join a meeting.

So far I have been able to get most pages to automatically close with my extension but it simply refuses to run on certain domains, including the one I actually need it to run on: https://company-name-here.zoom.us/. I ultimately would like to set the content script matchers to just zoom but for now I have expanded it to all sites in an effort to reduce sources of error.

It is not working no matter how I attempt to load the page, be it by clicking the redirect url on a google calendar event, reloading the page manually after it has already been opened, and even manually typing out the url and hitting enter. The zoom home page suffers from the same problem but other sites such as stack overflow show the "Content script loaded" console log and close in 5 seconds as I would expect.

Please find the entire source for the extension below:

manifest.json

{
  "manifest_version": 3,
  "name": "Zoom Auto Closer",
  "version": "1.0",
  "background": {
    "service_worker": "src/background.js"
  },
  "content_scripts": [{
    "run_at": "document_start",
    "matches": ["<all_urls>"],
    "js": ["src/content.js"]
  }]
}

src/content.js

const closeDelay = 5_000;

const closeCurrentTab = () => chrome.runtime.sendMessage('close-tab');

const main = () => {
  console.log('Content script loaded');
  setTimeout(closeCurrentTab, closeDelay);
};

main();

src/background.js

const closeTab = tabId => chrome.tabs.remove(tabId);

const onMessage = (message, sender) => {
  console.log('Received a message:', message);
  switch (message) {
    case 'close-tab': return closeTab(sender.tab.id);
  }
}

const main = () => {
  console.log('Service worker registered');
  chrome.runtime.onMessage.addListener(onMessage);
}

main();

Solution

  • The issue might be with the usage of <all_urls>.

    Google says on the matching patterns docs:

    The special pattern <all_urls> matches any URL that starts with a permitted scheme.

    And the permitted schemes are http:, https:, and file:.

    I am not too familiar with Zoom, but this article suggests that zoom uses the protocol zoommtg: to launch the the desktop program, so this falls outside of what <all_urls> covers.

    Edit:

    Now I see that you stated that the urls start with https:// so that might invalidate what I suggested. Still might be worth trying "*://*.zoom.us/*" instead of <all_urls>.


    You could try using "*://*.zoom.us/*" instead. If that doesn't work you could try ditching the content script and handling everything in the background service worker.

    In the background service worker, you could add a listener for chrome.tabs.onUpdated and check the url value to see if it matches the url for a Zoom tab and close it from there. You would also need to use the Alarms API for the delay.

    This method wouldn't be as efficient because it is called on every tab update (not really worth worrying about), but it is a possible workaround if you can't get the content script method to work.