Extension Developers - Better Background XMLHttpRequests

XMLHttpRequests (XHR) are used for lots of things these days. When running a background XHR, however, you might experience some common issues like authentication or bad SSL certificate dialogs popping up. Another problem was starting an XHR, then closing the window - the XHR is closed. To help get around these inconveniences, Manish Singh (of Flock) adding support for a background mode (bug 383430). The details:

  • Do not attach to a window’s load group, so requests aren’t cancelled if the window closes (often the window isn’t really associated with the request anyway)
  • Do not get an nsIAuthPrompt by default, since we don’t want authentication dialogs to pop up randomly.
  • Provide an nsIBadCertListener implementation, again to prevent bad certificate dialogs to pop up randomly.
var req = new XMLHttpRequest();
req.mozBackgroundRequest = true;
req.open("GET", someURI);
req.send(null);

The magic is handled by mozBackgroundRequest and it’s only available to chrome, not for web content.