简体   繁体   English

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

[英]How to use Chrome's declarativeWebRequest for changing http response headers

I'm trying to write a simple Chrome extension, which replaces the Content-Type header in http response using declarativeWebRequest (currently in beta; I'm using 25.0.1364.0). 我正在尝试编写一个简单的Chrome扩展程序,它使用declarativeWebRequest (当前为Beta;我使用的是25.0.1364.0)替换http响应中的Content-Type标头。

The code is based on Catifier example, where I changed the registerRules method: 该代码基于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);
}

It works ok in the sense, that the rules are registered, and I got the following feedback from the browser console: 从某种意义上说,规则已注册正常,并且从浏览器控制台获得了以下反馈:

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
  }
]

The problem is that the code does not actually produce any effect, that is http response headers remain unchanged. 问题在于该代码实际上不会产生任何效果,即http响应标头保持不变。 I'm not sure if this relates to a (still unfixed) bug in Chrome not displaying altered headers. 我不确定这是否与Chrome中的(仍未修复错误不显示更改后的标头有关。 But anyway, I have the following questions: 但是无论如何,我有以下问题:

1) Is the abovementioned RequestMatcher applied to contentType correctly or should I use a matcher for responseHeaders instead (with somehow pointed out Content-Type )? 1)是否将上述RequestMatcher正确地应用于contentType ,还是应该对responseHeaders使用匹配器(以某种方式指出Content-Type )?

2) If RequestMatcher should be applied to responseHeaders , what is the syntax for such a rule? 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) How can one debug a rule execution? 3)如何调试规则执行? I mean I'd like to trace and analyze how conditions are processed, and actions executed. 我的意思是我想跟踪和分析条件的处理方式和执行的动作。 Without this using declarativeWebRequest would be a puzzle, imho. 没有declarativeWebRequest使用, declarativeWebRequest将是一个难题,恕我直言。

Thanks in advance. 提前致谢。

1) I tried the following code (which is almost the same as yours, except that it modifies the content type from text/html to text/plain) 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'})
    ]
  }])

And it worked! 而且有效! The modification is not reflected in DevTools, though. 但是,该修改未反映在DevTools中。

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

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

But it didn't work (Did I make a mistake??). 但这没有用(我犯错了吗?)。 Use valueContains instead of valueEquals since Content-Type may contain encoding. 由于Content-Type可能包含编码,因此请使用valueContains而不是valueEquals。

3) It seems impossible to do that unless you debug Chrome itself. 3)除非您调试Chrome本身,否则似乎无法做到这一点。

I tested on Chromium 25.0.1363.0 (Build 173419) 我在Chromium 25.0.1363.0(内部版本173419)上进行了测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM