我見過很多從Firefox附加組件創建xhr請求的例子,但我試圖使用新的WebExtensions東西(其中require
和Components
未定義),似乎無法看到爲什麼我無法從擴展中發送簡單的XmlHttpRequest?來自Firefox的XMLHttpRequest WebExtension
值得注意的是,ajax請求將轉到完全不同的URL,但主機已將CORs設置爲允許所有來源。
只要.send()
被激發我的錯誤:
[Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: resource://gre/modules/ExtensionContent.jsm -> moz-extension://9ca18411-9a95-4fda-8184-9dcd3448a41a/myapp.js :: GM_xmlhttpRequest :: line 162" data: no]"1 whatsapp.js:166:9
的代碼看起來是這樣的:
function GM_xmlhttpRequest(orders) {
try {
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function(a1, a2, a3) {
console.log('xhr.load: %s, %s, %s', a1, a2, a3);
});
// open synchronously
oReq.open(orders.method, orders.url, false);
// headers
for (var key in orders.headers) {
oReq.setRequestHeader(key, orders.headers[key]);
}
// send
var res = oReq.send(orders.data);
console.log('xhr result: %s', res);
} catch(e) {
debugger;
console.warn('could not send ajax request %s to %s, reason %s', orders.method, orders.url, e.toString());
}
}
我已經添加了WebRequest的權限,以我的manifest.json中,我認識到,這不是什麼意思,但我正在努力瞭解什麼是停止Ajax請求?有任何想法嗎?
{
"manifest_version": 2,
"name": "MyApp",
"version": "1.0",
"description": "TestXHR",
"icons": {
"48": "icons/myapp-48.png"
},
"applications": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "45.0"
}
},
"content_scripts": [
{
"matches": ["*://web.myapp.com/*"],
"js": ["myapp.js"]
}
],
"permissions": [
"https://thehost.all-xhr-sent-here.net/*",
"webRequest"
]
}
您可以嘗試[fetch](https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch)。 –
這很有趣。 XHR應該像從任何網頁一樣工作,只要您從background.js或popup.js中進行操作即可。 – Noitidart
也適用於網絡擴展,這與權限URL本身有關 – tommed