Chrome Extension cause "Uncaught (in promise) Error: Cannot access a chrome:// URL"
Asked Answered
H

2

15

I am following how to make chrome extension getting started that is posted in chrome official site https://developer.chrome.com/docs/extensions/mv3/getstarted/

And I copied and pasted all code and run on the same way. But in my case, When I run chrome.scripting.executeScript, It causes "Uncaught (in promise) Error: Cannot access a chrome:// URL" error.

I don't know what is problem. Here is my code that had been copied from the link above.

  • manifest.json
{
  "name": "Getting Started Example",
  "description": "Build an Extension!",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "permissions": ["storage", "activeTab", "scripting"],
  "action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "/images/get_started16.png",
      "32": "/images/get_started32.png",
      "48": "/images/get_started48.png",
      "128": "/images/get_started128.png"
    }
  },
  "icons": {
    "16": "/images/get_started16.png",
    "32": "/images/get_started32.png",
    "48": "/images/get_started48.png",
    "128": "/images/get_started128.png"
  }
}
  • background.js
let color = '#3aa757';

chrome.runtime.onInstalled.addListener(() => {
  chrome.storage.sync.set({ color });
  console.log(`default color: ${color}`);
});
  • popoup.js
// Initialize button with user's preferred color
let changeColor = document.getElementById('changeColor');

chrome.storage.sync.get('color', ({ color }) => {
  changeColor.style.backgroundColor = color;
});

// When the button is clicked, inject setPageBackgroundColor into current page
changeColor.addEventListener('click', async () => {
  console.log('clicked');
  console.log(chrome.tabs);

  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  console.log(tab);

  chrome.scripting.executeScript({
    target: { tabId: tab.id },
    function: setPageBackgroundColor,
  });
});

// The body of this function will be executed as a content script inside the
// current page
function setPageBackgroundColor() {
  chrome.storage.sync.get('color', ({ color }) => {
    document.body.style.backgroundColor = color;
  });
}
  • popup.html
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="button.css" />
  </head>
  <body>
    <button id="changeColor"></button>
    <script src="popup.js"></script>
  </body>
</html>

Do you have an idea??

Hyades answered 7/6, 2021 at 15:0 Comment(5)
Uncaught (in promise) Error: Cannot access a chrome:// URL" means that you are trying to inject content script in pages which url starts with chrome://. Thiat's not allowed. Try to activate first a web page a then push the action-icon again.Monopolist
@Monopolist AMG, I got it. I tested in chrome://extension. You are right. Thanks!Hyades
So how to fix that?Wagner
Thanks @Monopolist I also tried on chrome://extensions/ but whenever I tried on web urls, it works properly.Yukoyukon
Have a look at this answer: https://mcmap.net/q/822666/-how-to-avoid-error-cannot-access-a-chrome-urlStephanstephana
G
21

When you try to trigger the event make sure that you are not in chrome://extensions/ or something similar.

Goon answered 9/8, 2021 at 18:7 Comment(3)
I was in the extensions page and hence the error. this worked for me.Fears
How? Limiting host_permissions to https does not prevent the extension running on a chrome:// URL.Chilli
Iam trying to make an extension for edge://bluetooth-internalsFinder
A
12

You can check the URL and avoid inject the script for "chrome://":

// skip urls like "chrome://" to avoid extension error
if (tab.url?.startsWith("chrome://")) return undefined;

chrome.scripting.executeScript({
   //...

I also do this in the background.js because I'm injecting the script there:

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {

  // skip urls like "chrome://" to avoid extension error
  if (tab.url?.startsWith("chrome://")) return undefined;

  if (tab.active && changeInfo.status === "complete") {
    chrome.scripting.executeScript({
       //...

Note. To have access to tab.url you need "tabs" in manifest (V3) "permissions".

Abshire answered 2/10, 2022 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.