This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategorizer.js
81 lines (69 loc) · 2.11 KB
/
categorizer.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
var langList = document.getElementById('langList');
var orderedLang;
var firstLang;
// Start an html table for languages details
var text = '<table border=1><tr><th>Default<th>Language<th>Local<th>Name<th>URI</tr>';
function getLang(lng) {
let lngSplit = lng.split('-');
if (lngSplit[0] in orderedLang) {
let found = false;
for (let i=0; i<orderedLang[lngSplit[0]].length; i++) {
if (orderedLang[lngSplit[0]][i].lang == lng) {
found = true;
break;
}
}
if (found) {
return lng;
}
else {
return orderedLang[lngSplit[0]][0].lang;
}
}
else {
return '';
}
}
// Get voices; add to table markup
function loadVoices() {
let voices = speechSynthesis.getVoices();
voices.forEach(function(voice, i) {
// Add all details to table
text += `<tr><td>${voice.default}<td>${voice.lang}<td>${voice.localService}<td>${voice.name}<td>${voice.voiceURI}`;
});
}
function orderVoicesByLanguage() {
let voices = speechSynthesis.getVoices();
voices.forEach(function(voice, i) {
let lng = voice.lang.split('-');
let collection = { "lang": voice.lang, "isLocal": voice.localService, "name": voice.name, "URI": voice.voiceURI, "default": voice.default };
if (!orderedLang) {
let key = lng[0];
let obj = {
[key]: [collection]
};
orderedLang = obj;
}
else if (!(lng[0] in orderedLang)) {
let key = lng[0];
let obj = {
[key]: [collection]
};
orderedLang = { ...orderedLang, ...obj };
} else {
let key = lng[0];
orderedLang[key].push(collection);
}
});
}
if ('speechSynthesis' in window) {
loadVoices();
langList.innerHTML = text;
// Chrome loads voices asynchronously.
window.speechSynthesis.onvoiceschanged = function(e) {
loadVoices();
langList.innerHTML = text;
orderVoicesByLanguage();
getLang('en-US');
}
}