Search code examples
javascriptnode.jsgoogle-chrome-extensionxmlhttprequestfirefox-addon

XMLHttpRequest not sending in Firefox (addon)


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...


Solution

  • 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.