2014-04-15 38 views
0

我正在開發一個擴展,其中無論有什麼要求,我都想更改請求參數並向服務器發送一個虛擬請求以獲得響應。例如 。如果加載的原始請求是www.abc.com/eg.php?user="some code「,那麼我想將其更改爲www.abc.com/eg.php?user="ritubala」並獲得html頁面在我的擴展內進行進一步處理的響應.. 我使用了以下網址中給出的這些代碼: Return HTML content as a string, given URL. Javascript Function 但它導致遞歸... 因此總之我想從我的擴展中的url獲取html代碼... plz do幫助獲取html頁面作爲URL內擴展的響應

回答

1

使用nsITraceableChannel,請注意,您將獲得正在加載的內容的副本。

如果您中止請求,它會中止給您一個副本。

這裏是我使用nsITraceableChannel:https://github.com/Noitidart/demo-nsITraceableChannel

我想你想是這樣的:

const { Ci, Cu, Cc, Cr } = require('chrome'); //const {interfaces: Ci, utils: Cu, classes: Cc, results: Cr } = Components; 
Cu.import('resource://gre/modules/Services.jsm'); 
Cu.import('resource://gre/modules/devtools/Console.jsm'); 

var observers = { 
    'http-on-examine-response': { 
     observe: function (aSubject, aTopic, aData) { 
      console.info('http-on-modify-request: aSubject = ' + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData); 
      var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel); 
      var requestUrl = httpChannel.URI.spec 
      if (requestUrl.indexOf('blah')) { 
       //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this is how you abort but if use redirectTo you don't need to abort the request 
       httpChannel.redirectTo(Services.io.newURI('http://www.anotherwebsite/', null, null)); 
      } 
     }, 
     reg: function() { 
      Services.obs.addObserver(observers['http-on-modify-request'], 'http-on-modify-request', false); 
     }, 
     unreg: function() { 
      Services.obs.removeObserver(observers['http-on-modify-request'], 'http-on-modify-request'); 
     } 
    } 
}; 



//register all observers 
for (var o in observers) { 
    observers[o].reg(); 
} 

注意redirectTo功能是FF20 + https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIHttpChannel#redirectTo%28%29

所以與nsITraceableChannel演示相結合這個例子你會得到你想要的。

如果你不想重定向你可以中止請求,獲取請求的DOCUMENT,然後xmlhttprequest新的頁面,它給你的源FIRST並沒有加載它到選項卡,然後修改源然後加載它回到文檔。或者不是xmlhttprequesting它,只是中止請求,重定向到新頁面,然後修改選項卡。到這裏的通道請參閱解決方案的標籤:

Security Error when trying to load content from resource in a Firefox Addon (SDK)

也做從一個擴展的HTTP請求看到這個片段:https://gist.github.com/Noitidart/9088113

+0

@Noitidart ...謝謝... :) –