Collbrothers
Collbrothers

Reputation: 195

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?

Upvotes: 2

Views: 12758

Answers (2)

Dev Ayush
Dev Ayush

Reputation: 133

for the background script (independent of the current page), you have to define the host_permissions key in your manifest file.

and for the individual script, you define it like this:

  "content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["jquery-3.7.1.min.js","content.js"]
  }

Upvotes: 0

ATP
ATP

Reputation: 3249

"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>"
  ]

Upvotes: 4

Related Questions