Search code examples
google-chromegoogle-chrome-extension

Chrome extension manifest permissions v3


Alright I'm making my first chrome extension and I've ran into a bit of a problem:

manifest.json

{
  "name": "...",
  "description": "...",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "permissions": [
    "tabs",
    "scripting",
    "activeTab"
  ],
  "host_permissions": [
    "tabs",
    "scripting",
    "activeTab"
  ]
}

background.js

chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
    if (info.status == 'complete') changeTheme(tab);
});


function changeTheme(tab) {
    chrome.scripting.insertCSS({
            target: {tabId: tab.id},
            css: "h1 { color: #212121; }"
        })
}

The extension devtool console logs this: Error: Cannot access contents of url "long ass url". Extension manifest must request permission to access this host.

How would I "Request" permission?


Solution

  • "host_permissions" is for matching urls not for permissions.

    Change this:

    "host_permissions": [
        "tabs",
        "scripting",
        "activeTab"
      ]
    

    Into this:

    "host_permissions": [
        "https://example.org/foo/bar.html"
      ]
    

    Or any other urls you want the extension to be available on (expect chrome://)

    If you want the extension to be available on all sites use:

    "host_permissions": [
       "<all_urls>"
      ]