I'm working on a simple cross-browser extension that redirects requests from one domain to another. Let's say from example.com to google.com. I've decided to use Manifest V3 to support Chrome. This forces me to use the declarativeNetRequest API instead of the webRequest API. The Chrome documentation implies that to redirect requests, an extension must have the declarativeNetRequestWithHostAccess permission and specify used hosts in host_permissions. So here's my working code:
{
"manifest_version": 3,
"name": "extension name",
"version": "1.0",
"description": "extension summary",
"permissions": [
"declarativeNetRequest",
"declarativeNetRequestWithHostAccess"
],
"host_permissions": [
"*://example.com/*"
],
"declarative_net_request": {
"rule_resources": [
{
"id": "ruleset",
"enabled": true,
"path": "rules.json"
}
]
}
}
[
{
"id": 1,
"priority": 1,
"condition": {
"requestDomains": [
"example.com"
]
},
"action": {
"type": "redirect",
"redirect": {
"transform": {
"host": "google.com"
}
}
}
}
]
I noticed that Chrome automatically grants my extension the permission to access sites on example.com. However, Firefox doesn't do this. Redirects just don't work unless I manually click on the extension settings and allow access to example.com. It doesn't work that way with Manifest V2 - there, host_permissions are part of the permissions field, and they permanent, not optional.
I can request host_permissions dynamically with a background script. However, the request can only be triggered on user action, i.e. when the user clicks on my extension in the toolbar.
This creates a situation where when my extension is installed in Firefox, it just doesn't work. The user needs to manually click on the extension to grant it permissions, but they may not realize this.
How can I specify that the host_permissions are not optional, so that they are granted automatically, or ask for them immediately upon installation?
Sadly, I found no solution to this. Here's a workaround I decided to use:
Every time the extension is loaded, and also when a permission is removed, check if the required permissions are granted. If not, open an annoying tab that communicates to the user that they should manually click on the extension in the toolbar. When they click, request the permissions.
Here's roughly the code of the
background.jsscript:and
manifest.json: