最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XMLHttpRequest from Firefox WebExtension - Stack Overflow

programmeradmin0浏览0评论

I've seen loads of examples of creating xhr requests from Firefox Add-ons, but I am trying to use the new WebExtensions stuff (where require and Components are undefined) and can't seem to see why I can't send a simple XmlHttpRequest from within the extension?

It's worth noting that the ajax request is going to a pletely different URL, but the host has CORs set to allow all origins.

As soon as .send() is fired I get the error:

[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

The code looks like this:

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());
  }
}

I've added webRequest permissions to my manifest.json, I realise that is not what it means, but am struggling to understand what is stopping the ajax request? Any ideas?

{
  "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/*"],
      "js": ["myapp.js"]
    }
  ],  
  "permissions": [
    "/*",
    "webRequest"
    ]
  }

I've seen loads of examples of creating xhr requests from Firefox Add-ons, but I am trying to use the new WebExtensions stuff (where require and Components are undefined) and can't seem to see why I can't send a simple XmlHttpRequest from within the extension?

It's worth noting that the ajax request is going to a pletely different URL, but the host has CORs set to allow all origins.

As soon as .send() is fired I get the error:

[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

The code looks like this:

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());
  }
}

I've added webRequest permissions to my manifest.json, I realise that is not what it means, but am struggling to understand what is stopping the ajax request? Any ideas?

{
  "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./*"],
      "js": ["myapp.js"]
    }
  ],  
  "permissions": [
    "https://thehost.all-xhr-sent-here/*",
    "webRequest"
    ]
  }
Share Improve this question edited Jun 23, 2016 at 5:55 tommed asked Jun 21, 2016 at 12:10 tommedtommed 1,5592 gold badges19 silver badges33 bronze badges 3
  • 3 You could try fetch. – Daniel Herr Commented Jun 21, 2016 at 16:50
  • 1 Thats interesting. XHR should work as it does from any webpage, as long as you do it from background.js or popup.js I would think. – Noitidart Commented Jun 23, 2016 at 4:38
  • Works from inside the web extension too, it was something to do with the permissions URL itself – tommed Commented Jun 23, 2016 at 5:57
Add a ment  | 

1 Answer 1

Reset to default 5

The problem was the permissions URL specified. I changed the sub domain to an asterisk and the protocol to an asterisk and it seemed to work after that.

发布评论

评论列表(0)

  1. 暂无评论