Chrome扩展程序可修改响应标头

时间:2015-07-11 00:47:30

标签: javascript google-chrome cookies google-chrome-extension

我正在开发一个可以修改一些响应标头的扩展程序。我做了扩展,我认为它修改了标题值,但问题是修改后的标题值没有出现在检查元素窗口(网络选项卡)中。

我想要改变的标题是Set-Cookie标题,在我的代码更改后,我可以看到带有cookie查看器扩展名的新值(EditThisCookie),但内容的行为仍然像cookie一样修改(内容根据cookie值修改)。

我将附加脚本和清单文件的下面的代码。

的manifest.json

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "Getting started example",
  "version": "1.0",

  "browser_action": {
    "default_icon": "assets/logo.png",
    "default_popup": "index.html"
  },

  "background": {
    "scripts": ["main.js"]
  },

  "permissions": [
    "activeTab",
    "webRequest",
    "webRequestBlocking",
    "*://*.domain.com/"
  ]
}

main.js

var callback = function(details) {
  if (details.type == "main_frame") {
    process_headers(details);
    console.log("Finished processing headers for request with url: ", details.url);
  }
  var final_response_headers = details.responseHeaders;
  console.log(final_response_headers);
  return {responseHeaders: final_response_headers};
};

function process_headers(details) {
  var responseHeaders = details.responseHeaders;
  for (var i in responseHeaders) {
    var responseHeader = responseHeaders[i];
    if (responseHeader.name == "Set-Cookie") {
      try_changing_value(responseHeader);
    }
  }
}

function try_changing_value(header) {
  var value = header.value;
  var expected_value = "sub_cookie1";
  var viewed_count_index = value.indexOf(expected_value);
  if (viewed_count_index != -1) {
    var two_points_index = value.indexOf(":", viewed_count_index);
    var comma_index = value.indexOf(",", viewed_count_index);
    var result = value.substring(0, two_points_index + 1) + "5" + value.substring(comma_index);
    header.value = result;
  }
}

chrome.webRequest.onHeadersReceived.addListener(callback, {urls: ["<all_urls>"]}, ["blocking", "responseHeaders"]);

原始的Set-Cookie标头值如下所示:

cookie=sub_cookie:5,sub_sub_cookie:0;Version=1;Comment=;Domain=.domain.com;Path=/;Max-Age=3600

总结一下,为什么在检查响应头时,新值是否出现在检查元素网络选项卡上?它没有正确改变吗?

非常感谢

0 个答案:

没有答案