-
Notifications
You must be signed in to change notification settings - Fork 0
/
SilverNeedle.js
202 lines (201 loc) · 6.47 KB
/
SilverNeedle.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var SilverNeedle = function(config){
var win = window,
doc = document,
nav = win.navigator,
ret = {},
mediaFormats = {
video : {
'mpeg4' : 'video/mp4; codecs="mp4v.20.8"',
'h264' : 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"',
'ogg' : 'video/ogg; codecs="theora"',
'webm' : 'video/webm; codecs="vp8"'
},
audio : {
'wav' : 'audio/wav; codecs="1"',
'mp3' : 'audio/mpeg',
'AAC' : 'audio/mp4; codecs="mp4a.40.2"',
'ogg' : 'audio/ogg; codecs="vorbis"',
'webM' : 'audio/webm; codecs="vorbis"'
}
},
_testMedia = function(type){
var ret = false,
newElement = doc.createElement(type),
isSupport = !!newElement.canPlayType,
formatObject = mediaFormats[type],
testFormat;
if(isSupport){
ret = {};
testFormat = function(format){
return newElement.canPlayType(format) == 'probably';
}
for(var f in formatObject){
ret[f] = testFormat(formatObject[f]);
}
}
return ret;
},
formTests = {
inputTypes : function(){
var ret = {},
types = ['search','number','range','color','tel','url','email','date','month','week','time','datetime','datetime-local'],
l = types.length,
input = doc.createElement('input'),
type;
while(l--){
type = types[l];
input.setAttribute('type',type);
if(input.type !== type){
ret[type] = false;
}
else{
ret[type] = true;
}
}
return ret;
},
inputAttributes : function(){
var ret = {},
newInput = doc.createElement('input'),
atttibutes = ['autocomplete','autofocus','list','placeholder','max',
'min','multiple','pattern','required','step',
'form','formAction','formEnctype','formMethod','formNoValidate',
'formTarget'],
l = atttibutes.length,
atttibute;
while(l--){
atttibute = atttibutes[l];
if(atttibute in newInput){
ret[atttibute] = true;
}
else{
ret[atttibute] = false;
}
}
return ret;
},
datalist : function(){
var element = doc.createElement('datalist');
return !!((typeof HTMLDataListElement != 'undefined' && element instanceof HTMLDataListElement) || element.childNodes.length);
},
keygen : function(){
var element = doc.createElement('keygen');
return !!((typeof HTMLKeygenElement != 'undefined' && element instanceof HTMLKeygenElement) || element.childNodes.length);
},
output : function(){
var element = doc.createElement('output');
return typeof HTMLOutputElement != 'undefined' && element instanceof HTMLOutputElement;
},
progress : function(){
var element = doc.createElement('progress');
return typeof HTMLProgressElement != 'undefined' && element instanceof HTMLProgressElement;
},
meter : function(){
var element = doc.createElement('meter');
return typeof HTMLMeterElement != 'undefined' && element instanceof HTMLMeterElement;
}
},
otherTests = {
canvas : function(){
var newCanvasElement = doc.createElement('canvas'),
isSupportCanvas = !!newCanvasElement.getContext,
context2D,
result = false;
if(isSupportCanvas){
context2D = newCanvasElement.getContext('2d');
result = {
support2D : typeof CanvasRenderingContext2D != 'undefined' && context2D instanceof CanvasRenderingContext2D,
text : typeof context2D.fillText == 'function'
}
}
return result;
},
video : function(){
return _testMedia('video');
},
audio : function(){
return _testMedia('audio');
},
//本地存储
localStorage : function(){
try {
return ('localStorage' in win) && win.localStorage !== null;
} catch(e) {
return false;
}
},
sessionstorage : function(){
try {
return ('sessionStorage' in win) && win.sessionStorage !== null;
} catch(e){
return false;
}
},
webSQLDatabase : function(){
return !!win.openDatabase;
},
indexedDB : function(){
return 'indexedDB' in win || 'webkitIndexedDB' in win || 'mozIndexedDB' in win || 'moz_indexedDB' in win;
},
webWorkers : function(){
return !!win.Worker;
},
//离线web应用
applicationCache : function(){
return !!win.applicationCache;
},
//地理位置
geolocation : function(){
return !!nav.geolocation;
}
},
_aoe = function(a,fns){
var l = a.length,
target;
if(l>0){
for(var i=0;i<l;i++){
try{
target = a[i];
if(!(target in ret)){
ret[target] = fns[target].call(null);
}
}catch(e){
}finally{
continue;
}
}
}
},
_stab = function(o){
var formTestsArray = o.form,
otherTestsArray = o.other;
_aoe(formTestsArray, formTests);
_aoe(otherTestsArray, otherTests);
},
_stabAll = function(){
for(var f in formTests){
ret[f] = formTests[f].call(null);
}
for(var o in otherTests){
ret[o] = otherTests[o].call(null);
}
},
_stabForm = function(){
for(var f in formTests){
ret[f] = formTests[f].call(null);
}
},
init = function(){
if(!config){
_stabAll();
}
else if(config === 'form'){
_stabForm();
}
else{
_stab(config);
}
ret['stab'] = _stab;
}();
return ret;
};