开发者问题收集

Chrome 扩展程序运行时出现错误

2018-02-12
8152

前提:

尝试编写 非常简单 的 chrome 扩展程序,作为测试,我想添加控制台日志记录以进行调试。但是,我一直收到此错误

Unchecked runtime.lastError while running webRequestInternal.addEventListener : You need to request host permissions in the manifest file in order to be notified about requests from the webRequest API.

尝试:

我尝试添加所有我能找到的权限,但都失败了。有人能帮帮我吗!

清单文件:

{
    "manifest_version": 2,
    "name": "test",
    "description": "testing app",
    "version": "1.0",
    "background": {
        "scripts": ["small.js"],
        "persistent": true
    },
    "permissions": ["webRequest", "webRequestBlocking", "tabs", "background", "storage"],
    "optional_permissions": ["http://*/*", "https://*/*", "<all_urls>"]
}

small.js

chrome.webRequest.onBeforeRequest.addListener(function(details) {
    if (details.method === "POST") {
        alert('here');
        console.log('logging here');

    } else if (details.method === "GET") {
        alert('there');
        console.log('logging there');
    }
}, {
    urls: ["<all_urls>"]
}, ["blocking", "requestBody"]);
1个回答

我遇到了同样的问题,错误消息也类似。在 ma​​nifest.json 中进行简单更新即可解决问题。

permissions 数组如下所示:

 "permissions": [
    "alarms",
    "contextMenus",
    "storage",
    "notifications",
    "webRequest",
    "webRequestBlocking",
    "<all_urls>"
  ],

permissions 中添加 <all_urls> 即可解决您的问题。

Abrar
2018-11-05