如何使用Chrome的declarativeWebRequest来更改http响应标头

时间:2012-12-18 18:47:48

标签: javascript google-chrome-extension httpresponse

我正在尝试编写一个简单的Chrome扩展程序,它使用declarativeWebRequest取代了http响应中的Content-Type标头(目前处于测试阶段;我使用的是25.0.1364.0)。

代码基于Catifier示例,我在其中更改了registerRules方法:

var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher;
var RemoveResponseHeader = chrome.declarativeWebRequest.RemoveResponseHeader;
var AddResponseHeader = chrome.declarativeWebRequest.AddResponseHeader;

function registerRules() {
  var changeRule = {
    priority: 100,
    conditions: [
      // If any of these conditions is fulfilled, the actions are executed.
      new RequestMatcher({
        contentType: ['audio/mpeg']
      }),
    ],
    actions: [
      new RemoveResponseHeader({name: 'Content-Type'}),
      new AddResponseHeader({name: 'Content-Type', value: 'application/octet-stream'}),
      new AddResponseHeader({name: 'X-ChromeExt-Content-Type', value: 'trap'})
    ]
  };

  var callback = function() {
    if (chrome.extension.lastError) {
      console.error('Error adding rules: ' + chrome.extension.lastError);
    } else {
      console.info('Rules successfully installed');
      chrome.declarativeWebRequest.onRequest.getRules(null,
          function(rules) {
            console.info('Now the following rules are registered: ' +
                         JSON.stringify(rules, null, 2));
          });
    }
  };

  chrome.declarativeWebRequest.onRequest.addRules(
      [changeRule], callback);
}

从某种意义上说,规则已经注册,我从浏览器控制台获得了以下反馈:

Now the following rules are registered: [
  {
    "actions": [
      {
        "instanceType": "declarativeWebRequest.RemoveResponseHeader",
        "name": "Content-Type"
      },
      {
        "instanceType": "declarativeWebRequest.AddResponseHeader",
        "name": "Content-Type",
        "value": "application/octet-stream"
      },
      {
        "instanceType": "declarativeWebRequest.AddResponseHeader",
        "name": "X-ChromeExt-Content-Type",
        "value": "trap"
      }
    ],
    "conditions": [
      {
        "contentType": [
          "audio/mpeg"
        ],
        "instanceType": "declarativeWebRequest.RequestMatcher"
      }
    ],
    "id": "_0_",
    "priority": 100
  }
]

问题是代码实际上没有产生任何影响,即http响应头保持不变。我不确定这是否与一个(仍然未固定的)bug in Chrome没有显示更改的标题有关。但无论如何,我有以下问题:

1)上述RequestMatcher是否正确应用于contentType,还是应该使用responseHeaders的匹配器(以某种方式指出Content-Type)?

2)如果应将RequestMatcher应用于responseHeaders,此规则的语法是什么?

new RequestMatcher({
  responseHeaders: // what to place here to match a value in a specific header line?
  // or possibly responseHeaders['Content-Type']: ...?
})

3)如何调试规则执行?我的意思是我想跟踪和分析条件的处理方式和执行的操作。没有这个使用declarativeWebRequest将是一个难题,imho。

提前致谢。

1 个答案:

答案 0 :(得分:4)

1)我尝试了以下代码(与您的代码几乎相同,只是它将内容类型从text / html修改为text / plain)

chrome.declarativeWebRequest.onRequest.addRules([{
    priority: 100,
    conditions: [
      // If any of these conditions is fulfilled, the actions are executed.
      new chrome.declarativeWebRequest.RequestMatcher({
        contentType: ['text/html']
      }),
    ],
    actions: [
      new chrome.declarativeWebRequest.RemoveResponseHeader({name: 'Content-Type'}),
      new chrome.declarativeWebRequest.AddResponseHeader({name: 'Content-Type', value: 'text/plain'}),
      new chrome.declarativeWebRequest.AddResponseHeader({name: 'X-ChromeExt-Content-Type', value: 'trap'})
    ]
  }])

它有效!但是,修改并未反映在DevTools中。

2)根据https://developer.chrome.com/trunk/extensions/declarativeWebRequest.html#type-HeaderFilter,您应该使用

new RequestMatcher({
  responseHeaders: [{nameEquals: "Content-Type", valueContains: "audio/mpeg"}]
})

但它不起作用(我犯了错误吗?)。 使用valueContains而不是valueEquals,因为Content-Type可能包含编码。

3)除非你自己调试Chrome,否则似乎无法做到这一点。

我在Chromium 25.0.1363.0(Build 173419)上进行了测试