This repository was archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
481 lines (440 loc) · 14.2 KB
/
index.html
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags always come first -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.min.css"></link>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css"></link>
<style>
/* Space out content a bit */
body {
padding-top: 1.5rem;
padding-bottom: 1.5rem;
}
/* Everything but the jumbotron gets side spacing for mobile first views */
.header,
.marketing,
.footer {
padding-right: 1rem;
padding-left: 1rem;
}
/* Custom page header */
.header {
padding-bottom: 1rem;
border-bottom: .05rem solid #e5e5e5;
}
/* Make the masthead heading the same height as the navigation */
.header h3 {
margin-top: 0;
margin-bottom: 0;
line-height: 3rem;
}
/* Custom page footer */
.footer {
padding-top: 1.5rem;
border-top: .05rem solid #e5e5e5;
text-align: center;
}
/* Customize container */
@media (min-width: 48em) {
.container {
max-width: 46rem;
}
}
.container-narrow > hr {
margin: 2rem 0;
}
/* Main marketing message and sign up button */
.jumbotron {
text-align: center;
border-bottom: .05rem solid #e5e5e5;
}
/* Supporting marketing content */
.marketing {
margin: 3rem 0;
}
.marketing p + h4 {
margin-top: 1.5rem;
}
.results {
min-height: 38px;
}
/* emoji nodes */
.emoji {
font-weight: normal;
font-family: AppleColorEmoji;
letter-spacing: 0.1em;
}
#needs_upgrade {
display: none;
}
/* Responsive: Portrait tablets and up */
@media screen and (min-width: 48em) {
/* Remove the padding we set earlier */
.header,
.marketing,
.footer {
padding-right: 0;
padding-left: 0;
}
/* Space out the masthead */
.header {
margin-bottom: 2rem;
}
/* Remove the bottom border on the jumbotron for visual effect */
.jumbotron {
border-bottom: 0;
}
}
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h3>
#saymoji
<small class="text-muted"> - say something, see something!</small>
</h3>
<div id="needs_upgrade">
Web Speech API is not supported by this browser. Upgrade to <a href="//www.google.com/chrome">Chrome</a> version 25 or later.
</div>
<div id="is_supported">
<form id="translate_form">
<fieldset class="form-group">
<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-success" id="start_button" onclick="startButton(event)">
<i id="start_icon" class="fa fa-microphone"></i>
<i id="stop_icon" style="display: none;" class="fa fa-microphone-slash"></i>
</button>
</span>
<input type="text" class="form-control" id="final_text" disabled placeholder="Click on the microphone..."></input>
</div>
</fieldset>
<fieldset class="form-group">
<div class="input-group">
<span id="final_emoji" class="results form-control"></span>
<span class="input-group-btn">
<button class="btn btn-default" onclick="copyButton(event)">
<i class="fa fa-clipboard"></i>
</button>
</span>
</div>
</fieldset>
</form>
<form class="form-inline">
<div class="form-group">
<label for="select_language">Select language:</label>
<select class="form-control" id="select_language" onchange="updateCountry()">
</select>
</div>
<div class="form-group">
<label for="select_dialect">Select dialect:</label>
<select class="form-control" id="select_dialect">
</select>
</div>
</form>
</div>
<div id="info" style="display:none">
<p id="info_start">
Click on the microphone icon and begin speaking for as long as you like.
</p>
<p id="info_speak_now" style="display:none">
Speak now.
</p>
<p id="info_no_speech" style="display:none">
No speech was detected. You may need to adjust your <a href="//support.google.com/chrome/bin/answer.py?hl=en&answer=1407892">microphone
settings</a>.
</p>
<p id="info_no_microphone" style="display:none">
No microphone was found. Ensure that a microphone is installed and that
<a href="//support.google.com/chrome/bin/answer.py?hl=en&answer=1407892">
microphone settings</a> are configured correctly.
</p>
<p id="info_allow" style="display:none">
Click the "Allow" button above to enable your microphone.
</p>
<p id="info_denied" style="display:none">
Permission to use microphone was denied.
</p>
<p id="info_blocked" style="display:none">
Permission to use microphone is blocked. To change, go to chrome://settings/contentExceptions#media-stream
</p>
<p id="info_upgrade" style="display:none">
Web Speech API is not supported by this browser. Upgrade to <a href="//www.google.com/chrome">Chrome</a> version 25 or later.
</p>
</div>
</div>
<div class="footer">
<p>made with <span class="emoji">😄</span> by <a href="https://twitter.com/luke_duke">luke</a>. find this on <a href="https://github.com/lpetre/saymoji">github</a></p>
</div>
</div>
<script>
var langs = [
['English', ['en-AU', 'Australia'],
['en-CA', 'Canada'],
['en-IN', 'India'],
['en-NZ', 'New Zealand'],
['en-ZA', 'South Africa'],
['en-GB', 'United Kingdom'],
['en-US', 'United States']
]
];
for (var i = 0; i < langs.length; i++) {
select_language.options[i] = new Option(langs[i][0], i);
}
select_language.selectedIndex = 0;
updateCountry();
select_dialect.selectedIndex = 6;
showInfo('info_start');
translate_form.onsubmit = function(event) {
event.preventDefault();
return false;
};
function updateCountry() {
for (var i = select_dialect.options.length - 1; i >= 0; i--) {
select_dialect.remove(i);
}
var list = langs[select_language.selectedIndex];
for (var i = 1; i < list.length; i++) {
select_dialect.options.add(new Option(list[i][1], list[i][0]));
}
select_dialect.style.visibility = list[1].length == 1 ? 'hidden' : 'visible';
}
var final_transcript = '';
var recognizing = false;
var ignore_onend;
var start_timestamp;
if (!('webkitSpeechRecognition' in window)) {
upgrade();
} else {
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
recognizing = true;
showInfo('info_speak_now');
//start_img.src = '/intl/en/chrome/assets/common/images/content/mic-animate.gif';
};
recognition.onerror = function(event) {
if (event.error == 'no-speech') {
showMic(false);
showInfo('info_no_speech');
ignore_onend = true;
}
if (event.error == 'audio-capture') {
showMic(false);
showInfo('info_no_microphone');
ignore_onend = true;
}
if (event.error == 'not-allowed') {
if (event.timeStamp - start_timestamp < 100) {
showInfo('info_blocked');
} else {
showInfo('info_denied');
}
ignore_onend = true;
}
};
recognition.onend = function() {
recognizing = false;
if (ignore_onend) {
return;
}
showMic(false);
if (!final_transcript) {
showInfo('info_start');
return;
}
showInfo('');
};
recognition.onresult = function(event) {
var interim_transcript = '';
if (typeof(event.results) == 'undefined') {
recognition.onend = null;
recognition.stop();
upgrade();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
var text = capitalize(final_transcript + interim_transcript);
final_text.value = linebreak(text);
final_emoji.innerHTML = '';
final_emoji.appendChild(translateInputChunk(text))
};
}
start_button.disabled = true;
document.addEventListener('emoji-ready', function() {
start_button.disabled = false;
});
function translateInputChunk(text) {
var container = document.createElement('span');
var allLines = text.split('\n');
for (var line = 0; line < allLines.length; line++) {
if (allLines[line] == '')
continue
var words = allLines[line].split(' ');
// Re-add the translated words.
for (var i = 0; i < words.length; i++) {
container.appendChild(translateWord(words[i]));
}
}
var tag = document.createElement('span');
tag.innerHTML = ' #saymoji';
tag.style.position = "absolute";
tag.style.left = "-1000px";
tag.style.top = "-1000px";
container.appendChild(tag);
return container;
}
function upgrade() {
needs_upgrade.style.display = 'inline';
is_supported.style.display = 'none';
}
var two_line = /\n\n/g;
var one_line = /\n/g;
function linebreak(s) {
return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
var first_char = /\S/;
function capitalize(s) {
return s.replace(first_char, function(m) {
return m.toUpperCase();
});
}
function startButton(event) {
if (recognizing) {
recognition.stop();
return;
}
final_transcript = '';
recognition.lang = select_dialect.value;
recognition.start();
ignore_onend = false;
final_text.innerHTML = '';
showMic(true);
showInfo('info_allow');
start_timestamp = event.timeStamp;
}
function copyButton(event) {
if (window.getSelection) {
window.getSelection().removeAllRanges();
var range = document.createRange();
range.selectNode(document.getElementById('final_emoji'));
window.getSelection().addRange(range);
}
try {
document.execCommand('copy');
} catch (err) {}
// Remove the selections - NOTE: Should use
// removeRange(range) when it is supported
window.getSelection().removeAllRanges();
}
function showInfo(s) {
if (s) {
for (var child = info.firstChild; child; child = child.nextSibling) {
if (child.style) {
child.style.display = child.id == s ? 'inline' : 'none';
}
}
info.style.visibility = 'visible';
} else {
info.style.visibility = 'hidden';
}
}
function showMic(r) {
start_icon.style.display = r ? 'none' : 'inline';
stop_icon.style.display = r ? 'inline' : 'none';
start_button.className = "btn " + (r ? "btn-danger" : "btn-success");
}
</script>
<script>
var allEmojis;
var SYMBOLS = '!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~';
/**
* Fires an emoji:ready event when the list of emojis has been loaded.
*
* @event emoji-ready
*/
(function() {
var request = new XMLHttpRequest();
request.open('GET', 'https://cdn.rawgit.com/muan/emojilib/1.1.0/emojis.json', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
allEmojis = JSON.parse(request.response);
var event = document.createEvent('HTMLEvents');
event.initEvent('emoji-ready', true, false);
document.dispatchEvent(event);
}
};
request.send();
})();
/**
* Returns a possibly translated english word to emoji, ready for display.
* @param {String} word The word to be translated
* @returns {HTMLElement} A <span> element containing the translated word.
*/
function translateWord(word) {
var node = document.createElement('span');
// Punctuation blows. Get all the punctuation at the start and end of the word.
var firstSymbol = '';
var lastSymbol = '';
while (SYMBOLS.indexOf(word[0]) != -1) {
firstSymbol += word[0];
word = word.slice(1, word.length);
}
while (SYMBOLS.indexOf(word[word.length - 1]) != -1) {
lastSymbol += word[word.length - 1];
word = word.slice(0, word.length - 1);
}
var emoji = getMeAnEmoji(word);
if (emoji != '') {
node.title = word;
node.className = "emoji"
node.innerHTML = emoji;
} else {
node.innerHTML = word;
}
// Reapply the punctuation.
node.innerHTML = firstSymbol + node.innerHTML + lastSymbol + ' ';
return node;
}
/**
* Returns the emoji equivalent of an english word.
* @param {String} word The word to be translated
* @returns {String} The emoji character representing this word, or '' if one doesn't exist.
*/
function getMeAnEmoji(word) {
word = word.trim().toLowerCase();
if (!word || word === '' || word === 'it' || word.length < 3)
return '';
// Maybe this is a plural word but the word is the singular?
// Don't do it for two letter words since "as" would become "a" etc.
var maybeSingular = '';
if (word.length > 2 && word[word.length - 1] == 's')
maybeSingular = word.slice(0, word.length - 1);
// Maybe this is a singular word but the word is the plural?
// Don't do this for single letter since that will pluralize crazy things.
var maybePlural = (word.length == 1) ? '' : word + 's';
// Go through all the things and find the first one that matches.
for (var emoji in allEmojis) {
var words = allEmojis[emoji].keywords;
if (emoji == word || emoji == maybeSingular || emoji == maybePlural ||
(words && words.indexOf(word) >= 0) ||
(words && words.indexOf(maybeSingular) >= 0) ||
(words && words.indexOf(maybePlural) >= 0))
return allEmojis[emoji].char;
}
return '';
};
</script>
</body>
</html>