本文介绍了修改 onHeadersReceived 上的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 chrome 扩展中,我需要在浏览的每个站点的标题中添加一行.在 background.js 文件中,我添加了这样的代码:

In my chrome extension I need to add a line to header of every site browsed. In background.js file I add such code:

var responseListener = function(details){
    var rule = {
        "name": "Access-Control-Allow-Origin",
        "value": "*"
    };
    details.responseHeaders.push(rule);
    return {responseHeaders: details.responseHeaders};
};

 chrome.webRequest.onHeadersReceived.addListener(responseListener,
     {urls: [   "*://*/*" ] },
     ["blocking", "responseHeaders"]);

在调试时调用处理程序并且新添加的标头成功通过了我在堆栈中找到的任何过滤器.但它在网络选项卡的响应标头部分中看不到,并且不会影响任何代码.我使用这些权限:

While debugging the handler is called and newly added header successfully passes any filters I have found upper in the stack. But it is not seen on network tab's Response headers section and does not effects any code. I use these permissions:

"tabs","<all_urls>", "http://*/*" ,"webRequest","webRequestBlocking", "webNavigation"

是否有新的政策或 API 更改不允许执行此类操作,或者我的 10 行代码中存在一些错误?

Is there a new policy or API changed which disallow to do such things or there is some bug in my 10 lines of a code?

推荐答案

Chrome 开发人员工具的网络"选项卡不会显示来自扩展程序的修改.请参阅 https://crbug.com/258064

The Network tab of the Chrome Developer tools does not show the modifications from extensions. See https://crbug.com/258064

如果您想查看您的扩展程序是否已成功修改请求,请访问 chrome://net-internals/#events,点击类型为 URL_REQUEST 的请求并查找 URL_REQUEST_DELEGATE 条目,例如URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED(这是通过 chrome 生成的日志条目的示例.declarativeWebRequest API) 或 "delegate_info = "extension [extension name]"(由 chrome.webRequest).

If you wish to see whether your extension has successfully modified a request, visit chrome://net-internals/#events, click on a request of type URL_REQUEST and look for URL_REQUEST_DELEGATE entries, e.g. URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED (this is an example of a log entry generated via the chrome.declarativeWebRequest API) or "delegate_info = "extension [extension name]" (generated by chrome.webRequest).

这篇关于修改 onHeadersReceived 上的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:53