2014-02-28 24 views

回答

4

複製粘貼,將任何谷歌的網站時,這將增加自定義標題:

const {Cu, Ci} = require('chrome'); //im not sure about this line plz verify, im not an sdk guy but know a bit about it 
Cu.import('resource://gre/modules/Services.jsm'); 

var httpRequestObserver = 
{ 
    observe: function(subject, topic, data) 
    { 
     var httpChannel, requestURL; 

     if (topic == "http-on-modify-request") { 
      httpChannel = subject.QueryInterface(Ci.nsIHttpChannel); 
      requestURL = httpChannel.URI.spec; 

      if (requestURL.indexOf('google.com') > -1) { 
       httpChannel.setRequestHeader('MyCustomRequestHeader', 'hiiii', false); 
      } 

      return; 
     } 
    } 
}; 

Services.obs.addObserver(httpRequestObserver, "http-on-modify-request", false); 
//Services.obs.removeObserver(httpRequestObserver, "http-on-modify-request", false); //run this on shudown of your addon otherwise the observer stags registerd 

也是一個音符。因爲您想更改用戶請求,請確保第三個參數設置爲false,否則它將合併預先存在的用戶代理和您提供的新用戶代理。

+0

完美,謝謝!我以前嘗試導入'chrome'包失敗,但似乎在某處發生了無關的錯誤,而不是該包無法用於SDK附加組件。乾杯! –