我正在開發一個擴展,其中無論有什麼要求,我都想更改請求參數並向服務器發送一個虛擬請求以獲得響應。例如 。如果加載的原始請求是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內擴展的響應
0
A
回答
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
相關問題
- 1. 作爲AJAX響應的HTML頁面?
- 2. 獲取響應從background.js的鉻擴展
- 3. 響應頁面高度作爲div展開html css
- 4. 獲取Firefox的擴展頁面大小
- 5. 在Chrome擴展中獲取當前頁面的url
- 6. Chrome擴展加載內容頁面中的HTML頁面
- 7. 從Chrome擴展中獲取當前頁面的源HTML
- 8. 獲取頁面源代碼擴展
- 9. ios擴展和獲取url標題和頁面截圖
- 10. 在HTML頁面中捕獲POST響應
- 11. 如何修改html擴展頁面的url字符串
- 12. 從html頁面獲取URL - objective
- 13. 在jQuery中獲取整個頁面作爲響應aJax
- 14. 什麼是HTML頁面的擴展
- 15. 獲取一些HTML代碼對ASPX的響應頁面
- 16. 在Ruby中獲取完整的HTML頁面(包括AJAX響應)
- 17. 刪除面servlet的URL模式和頁面擴展從URL
- 18. 獲取整個HTML頁面作爲迴應與JavaScript
- 19. 從URL獲取頁面內容?
- 20. 從URL獲取頁面內容
- 21. 從背景頁面獲取iframe擴展名爲Chrome123 popup
- 22. 響應式HTML頁面
- 23. 獲取html響應的HTTP響應
- 24. Chrome擴展從事件頁面到異常響應的異步響應
- 25. 在Firefox擴展中更改Html頁面的內容
- 26. 獲取404錯誤頁面響應的內容ruby
- 27. 使用Chrome擴展編輯頁面html
- 28. 從GitHub頁面刪除html擴展
- 29. 從Firefox的側邊欄擴展中獲取當前頁面的URL
- 30. 獲取html頁面
@Noitidart ...謝謝... :) –