我有一个 chrome MV3 扩展,它具有以下 manifest.json:
{
"manifest_version": 3,
"permissions": [
"scripting",
"tabs"
],
"host_permissions": ["<all_urls>"]
}
当我安装扩展程序并拥有现有选项卡时,我无法在这些现有选项卡上执行脚本:
chrome.scripting.executeScript({
target: {tabId: 1305686273},
func: function(){ console.log('hi'); }
});
我在后台服务工作线程脚本中收到此错误:
Cannot access contents of the page. Extension manifest must request permission to access the respective host.
我的理解是tabs
权限让我无需用户手势即可查询所有选项卡,并且 "host_permissions": ["<all_urls>"]
应该让我在所有选项卡上执行脚本而无需用户手势。
我错了,还是缺少一些东西来允许在没有用户手势的情况下在页面上编写脚本?
附注运行chrome.tabs.get(1305686273).then(console.log)
按预期工作。
最佳答案
chrome.scripting
似乎会对安装扩展程序之前存在的选项卡引发此错误。
Cannot access contents of the page. Extension manifest must request permission to access the respective host.
对于任何新创建的选项卡,脚本都按预期工作,无论用户是否单击了扩展程序。
我发现一种解决方法是在这些预先存在的选项卡上调用 chrome.tabs.reload(tabId)
,然后 chrome.scripting
将按预期工作。
将向 Chrome 团队提出错误。
关于google-chrome-extension - chrome.scripting.executeScript 无法在具有 `tabs` 和 `<all_urls>` 主机权限的现有选项卡上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74318853/