ray
ray

Reputation: 148

Check network connection via Firefox Extension

Is there a possibility to check the connection to the network in a Firefox extension? I just want to know if the user is Online or Offline.

I've already tried navigator.online, but it doesn't work.

Upvotes: 0

Views: 486

Answers (2)

ray
ray

Reputation: 148

As I'm learned, the Firefox doesn't know if I cut the network connection. So I've created a little XMLHttpRequest (XHR) to set a Boolean:

var netOnline; 

function createXMLHttpRequest() {
    return Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
       .createInstance(Components.interfaces.nsIXMLHttpRequest);    
}

function issueRequest() {
    var req = createXMLHttpRequest();
    req.open("GET", "http://google.com", true);
    req.timeout = 1000; // ms 
    // online
    req.addEventListener("load", function(event) {
        netOnline = true; 
    });
    // timeout -> offline
    req.addEventListener("timeout", function(event) {
        netOnline = false; 
    }); 
    // error -> offline
    req.addEventListener("error", function(event) {
        netOnline = false; 
    });
    req.send();
};

It works fine. Anyhow thanks for your help!

Upvotes: 1

Pike
Pike

Reputation: 166

https://developer.mozilla.org/en-US/docs/Online_and_offline_events is what you're looking for.

And now I'll just ramble to get beyond 30 chars, thanks stackoverflow.

Upvotes: 1

Related Questions