首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获得“未捕获(承诺中)错误: firefox扩展的后台脚本中的选项卡”缺少主机权限

获得“未捕获(承诺中)错误: firefox扩展的后台脚本中的选项卡”缺少主机权限
EN

Stack Overflow用户
提问于 2021-12-30 05:24:04
回答 1查看 501关注 0票数 0

这是我的背景剧本;

代码语言:javascript
运行
复制
/**
 * CSS to hide everything on the page,
 */
const hidePage = `body > : {
    display: none;
  }`;


/**
 * When a tab is loaded, check if the plugin is on. If the plugin is on, check if the url matches the page.
 * If it does, block it
 */
browser.tabs.onActivated.addListener(function (activeInfo) {
    browser.storage.local.get("onOff")
        .then((result) => {
            if (Object.entries(result).length === 0 || !result.onOff.value) {
                return;
            }
            browser.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
                let tab = tabs[0];
                console.log(tab.url);
                browser.tabs.insertCSS({code: hidePage})
            }, console.error)
        });
});

这是我的manifest.json

代码语言:javascript
运行
复制
{

    "manifest_version": 2,
    "name": "Beastify",
    "version": "1.0",
  
    "description": "Adds a browser action icon to the toolbar. Click the button to choose a beast. The active tab's body content is then replaced with a picture of the chosen beast. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#beastify",
    "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/beastify",
    "icons": {
      "48": "icons/beasts-48.png"
    },
  
    "permissions": [
      "tabs",
      "activeTab",
      "storage"
    ],
  
    "browser_action": {
      "default_icon": "icons/beasts-32.png",
      "default_title": "BlockIt",
      "default_popup": "popup/blockit.html"
    },

    "background": {
      "scripts": ["background_scripts/blocker.js"]
    },
  
    "web_accessible_resources": [
      "beasts/frog.jpg",
      "beasts/turtle.jpg",
      "beasts/snake.jpg"
    ]
  
  }

有一些多余的东西在那里,因为我正在构建我的插件从一个Firefox扩展教程

导致Uncaught (in promise) Error: Missing host permission for the tab的确切代码是

browser.tabs.insertCSS({code: hidePage})

blocker.js第23行

我相信我确实有正确的权限从这个背景脚本插入css,所以我不知道为什么会发生这个错误。我还尝试执行一个内容脚本,而不是运行上面抛出错误的行,但同样的错误失败了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-30 08:00:33

只有当用户显式调用您的扩展时,activeTab才能工作,如WebExtensionsChrome扩展文档中所描述的那样。显然,它的名称具有很高的误导性:它应该类似于activeTabWhenInvoked

为了访问任何选项卡,无需事先与扩展交互,您必须在manifest.json中添加广泛的主机权限:

代码语言:javascript
运行
复制
  "permissions": ["<all_urls>"],

现在不需要activeTab了,但是您可能仍然希望保持tabs对86岁以上的火狐的权限,参见文件中的说明

onOff为false时,最好完全删除侦听器,这样扩展就不会白费了。您可以为onActivated侦听器使用一个全局命名函数,并通过browser.storage.onChanged事件观察对onOff的更改。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70527776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档