【问题标题】:You do not have permission to use blocking webRequest listeners. Be sure to declare the webRequestBlocking permission in your manifest您无权使用阻塞 webRequest 侦听器。请务必在清单中声明 webRequestBlocking 权限
【发布时间】:2022-11-21 15:13:20
【问题描述】:

我尝试用 React js 开发我的第一个 chrome 扩展。当我尝试使用 chrome.webRequest API 阻止 chrome 扩展中的 URL 在错误页面中显示两个错误。

'webRequestBlocking' 需要清单版本 2 或更低版本。

未经检查的 runtime.lastError:您无权使用阻塞 webRequest 侦听器。请务必在清单中声明 webRequestBlocking 权限。

并且我在清单文件中声明了“webRequestBlocking”权限。这是我的 manifest.json

{
  "manifest_version": 3,
  "name": "Chrome Extension",
  "description": "First Extension",
  "options_page": "options.html",
  "background": {
  "service_worker": "background.bundle.js",
  "matches": [
   "<all_urls>"
  ]
},
 "action": {
 "default_title": "Open Extension",
 "default_icon": "icon-34.png"
 },
 "icons": {
 "128": "icon-128.png"
 },
 "content_scripts": [
 {
   "matches": [
    "http://*/*",
    "https://*/*",
    "<all_urls>"
   ],
   "js": [
    "contentScript.bundle.js"
   ],
   "css": [
    "content.styles.css"
   ]
  }
 ],
 "devtools_page": "devtools.html",
 "web_accessible_resources": [
 {
  "resources": [
    "content.styles.css",
    "icon-128.png",
    "icon-34.png"
   ],
   "matches": []
  }
 ],
  "permissions": [
  "activeTab",
  "tabs",
  "webRequest",
  "webRequestBlocking"
 ],
 "host_permissions": [
  "<all_urls>"
 ]
}

这是我的 background.js

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    console.log(details);
    return {cancel: true};
  },
  {urls: ["https://reactjs.org/"]},
  ["blocking"]
);

我试过删除 webRequestBlocking 但也一样。谁能帮我解决这个问题?

【问题讨论】:

  • 我们不能使用清单 v2。谷歌浏览器扩展开发文档告知了这一点。 “自 2022 年 1 月 17 日起,Chrome 网上应用店已停止接受新的 Manifest V2 扩展。我们强烈建议新扩展以 Manifest V3 为目标。”
  • 请改用 declarativeNetRequest。

标签: reactjs google-chrome-extension webrequest manifest.json


【解决方案1】:

错误会自行解释'webRequestBlocking' requires manifest version of 2 or lower. 所以你不能使用网络请求阻塞清单版本 3.

但是 chrome 通过使用 declarativeNetRequestWithHostAccess API 提供了一种替代方法,女巫用于通过指定声明性规则来阻止或修改网络请求 你可以查看here了解更多详情。

【讨论】:

    【解决方案2】:

    您可以将manifest_version更改为2,将webRequestBlocking更改为permissions

    【讨论】:

    • 但在 google chrome 扩展开发文档中告知了这一点。 “自 2022 年 1 月 17 日起,Chrome 网上应用店已停止接受新的 Manifest V2 扩展。我们强烈建议新扩展以 Manifest V3 为目标。”
    • 但 webRequestBlocking 在 v3 中不起作用。
    • Update the Manifest V3 migration guide 它可能有助于转换
    【解决方案3】:

    您找到问题的解决方案了吗?

    【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2021-06-20
    • 2022-08-06
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多