如何在Manifest V3安装时请求host_permissions?

问题描述 投票:0回答:1

我正在开发一个简单的跨浏览器扩展,它将请求从一个域重定向到另一个域。假设从

example.com
google.com
。我决定使用 Manifest V3 来支持 Chrome。这迫使我使用
declarativeNetRequest
API 而不是
webRequest
API。 Chrome 文档暗示要重定向请求,扩展程序必须具有 declarativeNetRequestWithHostAccess
 权限并在 
host_permissions
 中指定使用的主机。这是我的工作代码:

{ "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"
                }
            }
        }
    }
]
我注意到 Chrome 会自动授予我的扩展程序访问 

example.com

 上网站的权限。然而,Firefox 不这样做。除非我手动单击扩展设置并允许访问 
example.com
,否则重定向不起作用。它在 Manifest V2 中不起作用 - 在那里,
host_permissions
permissions
 字段的一部分,并且它们是永久的,不是可选的。

我可以使用后台脚本动态请求

host_permissions

。但是,该请求只能在用户操作时触发,即当用户单击工具栏中的我的扩展程序时。

这会造成一种情况,当我的扩展程序安装在 Firefox 中时,它就不起作用。用户需要手动点击扩展程序来授予其权限,但他们可能没有意识到这一点。

如何指定

host_permissions

 不是可选的,以便自动授予它们,或者在安装时立即要求它们?

firefox-addon chrome-extension-manifest-v3 browser-extension
1个回答
0
投票
可悲的是,我没有找到解决方案。这是我决定使用的

解决方法

每次加载扩展程序以及删除权限时,请检查是否授予了所需的权限。如果没有,请打开一个烦人的选项卡,告知用户他们应该手动单击工具栏中的扩展程序。当他们点击时,请求权限。

以下是

background.js

 脚本的大致代码:

const requiredPermissions = { origins: [ // ... ] } function checkPermissions() { chrome.permissions.contains(requiredPermissions, (hasPermissions) => { if (!hasPermissions) { console.debug("Missing permissions") chrome.tabs.create({url: "missing_permissions.html", active: true}); } }); } function handleClick() { console.debug("Requesting permissions") chrome.permissions.request(requiredPermissions, (isGranted) => { // user has allowed or denied the permission request }) } // Register event listeners chrome.permissions.onRemoved.addListener(checkPermissions); chrome.action.onClicked.addListener(handleClick); // Check when the extension loads checkPermissions()

manifest.json

{ ... "action": {}, "background": { "scripts": ["background.js"] } ... }
    
© www.soinside.com 2019 - 2024. All rights reserved.