2012-12-18 20 views
2

我在尝试编写一个简单的Chrome扩展插件,它使用declarativeWebRequest(目前处于测试阶段;我使用25.0.1364.0)替换了http响应中的Content-Type标头。如何使用Chrome的声明式WebRequest来更改http响应标头

的代码是基于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)适用于contentType上述RequestMatcher正确或我应该使用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将是一个难题,恕我直言。

在此先感谢。

回答

4

1)我尝试下面的代码(这几乎是一样的你,除了它从文本修改内容类型/ HTML文本/纯)

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)上测试过