Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
How to prevent chrome extension to run on chrome://

Absolute JS beginner here, please be gentle and pardon my ignorance.

In the background service_worker (background.js) of a Chrome extension I'm trying to build (manifest_version: 3) I use the chrome.action API to control the toolbar icon's behaviour.

chrome.action.onClicked.addListener(async function (tab) {  
    chrome.scripting.executeScript({
        target: {tabId: tab.id},
        files: ['iconClicked.js']
    });
});

Now when a user clicks the icon whilst being on chrome://extensions, this produces an error:

Uncaught (in promise) Error: Cannot access a chrome:// URL

What can I do to prevent my script to run on chrome:// URLs?

I tried wrapping it in a condition checking the current URL:

if (window.location.href.includes("urls/where/i/want/my/script/to/run") == true) {
    chrome.action.onClicked.addListener(async function (tab) {
        chrome.scripting.executeScript({
            target: {tabId: tab.id},
            files: ['iconClicked.js']
        });
    });
};

But, alas, apparently on chrome:// "window" is not defined:

ReferenceError: window is not defined

How else can I effectively prevent my extension from throwing errors on chrome:// (ideally w/out using jQuery or any other libraries)?

Any hints are greatly appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The background script runs as a service worker so it has no relation to location of a web page.

Since executeScript returns a Promise by default so you can catch it to ignore the error:

chrome.action.onClicked.addListener(async tab => {
  await chrome.scripting.executeScript({
    target: {tabId: tab.id},
    files: ['iconClicked.js'],
  }).catch(() => {});
});

Or wrap in try with await:

chrome.action.onClicked.addListener(async tab => {
  try {
    const results = await chrome.scripting.executeScript({
      target: {tabId: tab.id},
      files: ['iconClicked.js'],
    });
    // console.log(results);
  } catch (e) {
    // ignoring the error
  }
});

And for the sake of completeness, ManifestV3 can also be used with callbacks:

chrome.action.onClicked.addListener(tab => {
  chrome.scripting.executeScript({
    target: {tabId: tab.id},
    files: ['iconClicked.js'],
  }, () => chrome.runtime.lastError); // ignoring the error
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!