-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.js
60 lines (59 loc) · 1.61 KB
/
connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* connectionJs
* Analysis browser's navigator.connection type
*
* Author: Jone Casper
* Email: [email protected]
*/
(function(root, name, factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
define(function(){
return factory();
});
}else if (typeof module !== 'undefined' && module.exports){
module.exports = factory();
}else{
var namespaces = name.split("."),
scope = root || this;
for (var i=0; i<namespaces.length; i++) {
var p = namespaces[i],
ex = scope[p];
scope = scope[p] = (i === namespaces.length - 1) ?
factory():
(ex || {});
}
}
}(this, "connectionJs.getConnectType",function(){
/*
* navigator.connection now work on Android, Firefox Mobile (Gecko) and Firefox OS
*/
var connection = navigator.connection||navigator.mozConnection||navigator.webkitConnection;
var type_text = ['unknown','ethernet','wifi','2g','3g','4g','none'];
function getConnectType(){
if (!connection){
return "unknown";
}
var conn_type = 'unknown';
if (typeof connection.type === "number" && connection.type < type_text.length){
conn_type = type_text[connection.type];
}else{
conn_type = connection.type;
}
if (conn_type === "unknown" || typeof conn_type === "undefined" || typeof connection.bandwidth === "number"){
if(connection.bandwidth > 10){
conn_type = 'wifi';
}else if(connection.bandwidth > 2){
conn_type = '3g';
}else if(connection.bandwidth > 0){
conn_type = '2g';
}else if(connection.bandwidth == 0){
conn_type = 'none';
}else{
conn_type = 'unknown';
}
}
return conn_type;
}
return getConnectType;
}));