Help accessing the chrome.permissions api

73 views
Skip to first unread message

Jon Howard

unread,
Jan 15, 2021, 10:41:33 AM1/15/21
to Chromium Extensions
Hi,
Does anyone have a full working example of accessing the chrome.permissions api from within an injected content script.

I've been trying to see if I can request cookie permission which has been configured to be an optional permission within an v2 manifest file but every time I try and access chrome.permissions it's not defined within the chrome.* namespace within my injected script.

Here is my current (v2) manifest file.

{
"manifest_version": 2,
"name": "Browser Extension",
"version": "5.0.0",
"description": "MV 2",
"homepage_url": "http://www.homepage.com",
"optional_permissions": [
"cookies",
"<all_urls>"
],
"permissions": [
"alarms",
"storage",
"tabs",
"webNavigation"
],
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"browser_action": {
"default_title": "My Popup",
"default_popup": "popup.html",
"default_icon": {
"16": "icons/icon-16.png",
"19": "icons/icon-19.png",
"32": "icons/icon-32.png",
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
}
},
"content_scripts": [
{
"js": [
"serps.js"
],
"matches": [
"<all_urls>"
],
"include_globs": [
"*://www.google.*/*",
"*://www.bing.*/*"
],
"run_at": "document_idle",
"all_frames": false
},
{
"js": [
"retailer.js"
],
"matches": [
"<all_urls>"
],
"exclude_globs": [
"*://*.google.*/*",
"*://*.bing.*/*",
"*://*.youtube.*/*"
],
"run_at": "document_idle",
"all_frames": false
}
],
"web_accessible_resources": [],
"icons": {
"16": "icons/icon-16.png",
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
},
"minimum_chrome_version": "87"
}

Simeon Vincent

unread,
Jan 15, 2021, 9:21:26 PM1/15/21
to Jon Howard, Chromium Extensions
Jon,

You can't use that API in content scripts; see the content scripts docs for details on which APIs are available in this context. You can work around this by posting a message to the background script, executing the query in the background context, then posting a message to the content script.

Cheers,

Simeon - @dotproto
Chrome Extensions DevRel


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/4534297d-6cc6-450e-9db8-cfc106859633n%40chromium.org.

Jon Howard

unread,
Jan 19, 2021, 9:24:36 AM1/19/21
to Chromium Extensions, Simeon Vincent, Chromium Extensions, Jon Howard
@dotproto - Thanks for the heads up 8-)
Reply all
Reply to author
Forward
0 new messages