我见过很多从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