google-chrome-extension - 1 个用户指定域的 host_permissions

标签 google-chrome-extension

我们有一个扩展程序,当前具有对 3 个不同域的 host_permission 访问权限。我们想添加第四个。问题是,第四个域将由用户在扩展程序的选项页面中设置,并且可以是任何内容。

我已经阅读过有关使用 all_urls 作为 host_permission 的内容,并相信可以通过这种方式完成。我的问题是:

  1. 是否没有其他方法可以向用户在选项中设置的特定域授予权限? (类似后台脚本的东西会在那里看到一个新域,并提示用户授予访问该 1 个域的权限)

  2. 使用 all_urls 时,通过 Google 审批流程是否存在潜在问题?我们将使用 all_urls 仅访问 1 个域,我读到这至少在过去是一个问题,但在我们的情况下,1 个域在安装时是未知的。

最佳答案

此示例在单击请求按钮时将主机添加到源。
另外,单击 getAll 按钮可在控制台上显示来源和权限。

在没有用户手势的情况下添加到源将导致以下错误:

"Unchecked runtime.lastError: This function must be called during a user gesture"

list .json

{
    "name": "optional_host_permissions",
    "version": "1.0",
    "manifest_version": 3,
    "host_permissions": [
        "*://*.google.com/"
    ],
    "optional_host_permissions": [
        "<all_urls>"
    ],
    "action": {
        "default_popup": "popup.html"
    }
}

myscript.js

const elmRequest = document.getElementById("request");
const elmGetAll = document.getElementById("getAll");

elmRequest.onclick = () => {
    request();
}

elmGetAll.onclick = () => {
    getAll();
}

function request() {
    chrome.permissions.request({
        origins: ["https://hoge.com/"]
    }, (granted) => {
        console.log(granted);
    });
}

function getAll() {
    chrome.permissions.getAll((permissions) => {
        console.log(permissions);
    });
}

popup.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
</head>

<body style="min-width:250px">
    <input type="button" id="request" value="request"><br>
    <input type="button" id="getAll" value="getAll">
    <script src="myscript.js"></script>
</body>

</html>

关于google-chrome-extension - 1 个用户指定域的 host_permissions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74047166/

相关文章:

javascript - 在 Chrome 扩展中渲染 Twig 模板

oauth - 我的扩展程序的自定义 URL

javascript - Chrome 扩展使用网站加载的 jQuery 实例

javascript - Chrome 扩展程序,JQuery 错误 "Uncaught TypeError: Object [object Object] has no method ' 文本'"

google-chrome - 后台js中的chrome扩展卸载事件

javascript - Chrome 扩展中的 Vue.js

javascript - Firefox Web 扩展 "Can' t 访问死对象”错误

google-chrome-extension - 双向通信在 Chrome 扩展中返回错误

javascript - Chrome 扩展程序内容脚本未注入(inject)特定网站

javascript - 让用户在浏览器扩展中记录快捷方式