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.
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.
https://developer.mozilla.org/en-US/docs/Online_and_offline_events是您正在寻找的。
现在我会漫无目的地获得超过 30 个字符,感谢 stackoverflow。
据我所知,Firefox 不知道我是否切断了网络连接。所以我创建了一个小的 XMLHttpRequest (XHR) 来设置一个布尔值:
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();
};
它工作正常。无论如何感谢您的帮助!