-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
129 lines (104 loc) · 3.47 KB
/
main.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
(function(w) {
var speechnav = function(options) {
var self = this;
this._base = null;
self._registry = {};
self._options = {
debug: true
};
self._lang = (navigator.language) ? navigator.language : navigator.userLanguage;
self._lang = "en";
if (typeof(options) === 'object') {
for (var i in options) {
self._options[i] = options[i];
}
}
self.include = function(p, clbk, b) {
var h = document.getElementsByTagName('head')[0], s = document.createElement('script');
s.type = 'text/javascript';
s.src = (b !== undefined) ? b : self._base + p;
s.onreadystatechange = clbk;
s.onload = clbk;
h.appendChild(s);
};
self.registry = function(key, obj) {
if (key in self._registry) {
self.log('key [' + key + '] already present in registry: ', self._registry[key]);
return false;
}
self._registry[key] = obj;
};
self.register = function(key) {
if (!key in self._registry) {
self.log('key [' + key + '] not found in registry');
return false;
}
return self._registry[key];
};
self.log = function() {
if (self._options && self._options.debug) {
return w.console && console.log.call(console, arguments);
}
};
self.init = function() {
self.loadBase();
if (!self.isCapable())
return false;
if (self._options.includejQuery) {
self.include("lib/jquery-2.0.1.min.js", self.prepare);
} else {
self.prepare();
}
self.include("lib/webkitSpeechRecognition.js");
self.loadDictionary();
self.include("lib/speechManager.js", function() {
self.register("speech_manager").ready();
});
self.log(self);
};
self.loadDictionary = function() {
sn.include("dictionary/_def.js", function() {
sn.include("dictionary/" + self._lang + ".js");
});
};
self.prepare = function() {
self.$ = $;
$.noConflict();
self.$(function() {
self.ready();
});
};
self.isCapable = function() {
if (!('webkitSpeechRecognition' in w)) {
alert("Now only Chrome 21 and above support Web Speech API, consider to upgrade!");
return false;
}
return true;
};
self.loadBase = function() {
var scripts = document.getElementsByTagName('script');
var index = scripts.length - 1;
var myScript = scripts[index];
var tmp = myScript.src.split("/");
tmp.pop();
self._base = tmp.join("/") + "/";
};
self.ready = function() {
};
};
var sn = new speechnav({
includejQuery: !("jQuery" in w)
});
try {
if (w.addEventListener) {
w.addEventListener('load', sn.init, false);
} else if (w.attachEvent) {
w.attachEvent('onload', sn.init);
} else {
throw "Can't bind load event";
}
} catch (e) {
sn.log(e);
}
w.speechnav = sn;
})(window);