0

I'm developing an addon that needs to do a POST request to my server.

I'm using XMLHttpRequest for this. It's working great on Chrome, but the POST request is never sent on Firefox. Nothing is showing in the dev tools (console / network / addon inspect console), and nothing is received on my server. I've disabled any other addons and any kind of security on my whole PC (yes, I'm this desperate).

manifest.json

{
    ...
    "manifest_version": 2,
    "content_scripts": [{
        "matches": ["*://myurl.com/*"],
        "js": ["content.js"],
        "run_at": "document_end"
    }],
    "permissions": ["activeTab", "webRequest"]
}

The post request in my content script

const post = () => {
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://myserver.com/test', false);
    xhr.setRequestHeader('Content-Type', 'application/json');
    console.log('Before sending request');
    xhr.send(JSON.stringify({id: myId}));
    console.log('Request sent');
    return xhr.responseText;
};

Only the "Before sending request" is logged. Anything after the xhr.send() is never executed (this includes code after the post() call in the rest of my code).

My server accepts all origins. It's also correctly configured to received and process POST requests

const cors = require('cors')
app.use(cors());

What's wrong with this ? It's perfectly working on Chrome and I have no idea why it's never being sent without leaving any kind of error...

Alex Mougenet
  • 213
  • 3
  • 20
  • Firefox has a separate browser console (Ctrl-Shift-J). Have you checked that for any errors? – Andy Jan 08 '23 at 20:30
  • I didn't know about this. Just checked, there is nothing related to my addon, and nothing that could impact it – Alex Mougenet Jan 08 '23 at 20:39
  • Does the behaviour change if you break the manifest file (e.g. remove webrequest permission). Trying to see if it might be be coming from explicit permissions somehow not set correctly and how those types of errors show up. – Cadmium Jan 10 '23 at 23:27
  • It makes no difference – Alex Mougenet Jan 11 '23 at 00:20
  • 1
    Some other items in case they trigger something: a note on where the [code is being triggered from having an effect](https://stackoverflow.com/q/66384281/7632432) on the behaviour . Also, does a GET http method behave differently? (still thinking about some extra permissons/sandboxing but no specific theory) – Cadmium Jan 11 '23 at 01:03
  • I would really prefer the request being made in my content script... I've also tried GET requests, and they behave the same (working on Chrome, not working on Firefox) – Alex Mougenet Jan 11 '23 at 17:29
  • @Cadmium You can post your comment as an answer if you want to claim the bounty, as it's a working solution that I'm going with ! – Alex Mougenet Jan 12 '23 at 04:25

3 Answers3

1

Per the post on firefox XHR working differently based on calling context consider moving the call to a background script to see if you get different behaviour.

Cadmium
  • 623
  • 3
  • 9
0

As @Cadmium suggested in the question comments (and even though I didn't want to handle this request there), I've moved my post() function to a background script.

It's now working on both Chrome and Firefox. I still don't understand why it's behaving this way on Firefox, but it's a good enough workaround for me for now !

Alex Mougenet
  • 213
  • 3
  • 20
-1

You should consider adding your URL (e.g. "*://myurl.com/*") as a host permission to the permission array to have privilege for XMLHttpRequest. Please see official doc here in MDN and Chrome docs.

You can see this issue was also covered in this SO answer as well

BTW, I would consider migrating to manifest V3 following this Chrome suggestion:

The Chrome Web Store no longer accepts Manifest V2 extensions. Please use Manifest V3 when building new extensions.

bentz123
  • 1,081
  • 7
  • 16
  • Unfortunately it makes no difference to add my URL as a host permission. I have a v3 manifest for Chrome, but Firefox doesn't like it : Protocol error (Error): Unsupported manifest version: 3 from: server0.conn0.webExtensionDescriptor48 (resource://gre/modules/Schemas.jsm:303:11) – Alex Mougenet Jan 11 '23 at 17:25