-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.js
488 lines (449 loc) · 15.1 KB
/
script.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
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
482
483
484
485
486
487
488
let position;
let manualPosition = false;
let map;
let locationData;
let cancelSpeaking;
let skipThisParagraph;
const state = {
playing: false,
loading: false,
paused: false,
};
const pollSeconds = 30;
const seen = {};
const interestingTypes = [
'point_of_interest',
'natural_feature',
'neighborhood',
'sublocality',
'locality',
];
const languageMap = {
'English (US)': {wikiTag: 'en', speechTag: 'en-US', welcomeMsg: 'Welcome to your road trip.'},
'English (UK)': {wikiTag: 'en', speechTag: 'en-GB', welcomeMsg: 'Welcome to your road trip.'},
'Français': {wikiTag: 'fr', speechTag: 'fr-FR', welcomeMsg: 'Bienvenue dans votre voyage.'},
'Cebuano': {wikiTag: 'ceb', speechTag: 'ceb', welcomeMsg: 'Welcome sa imong pagbiyahe sa dalan.'},
'Svenska': {wikiTag: 'sv', speechTag: 'sv-SE', welcomeMsg: 'Välkommen till din vägresa.'},
'Deutsch': {wikiTag: 'de', speechTag: 'de-DE', welcomeMsg: 'Willkommen zu Ihrem Roadtrip.'},
'Nederlands ': {wikiTag: 'nl', speechTag: 'nl-NL', welcomeMsg: 'Welkom bij je roadtrip.'},
'русский': {wikiTag: 'ru', speechTag: 'ru-RU', welcomeMsg: 'Добро пожаловать в свою поездку.'},
'Italiano': {wikiTag: 'it', speechTag: 'it-IT', welcomeMsg: 'Benvenuto nel tuo viaggio.'},
'Español': {wikiTag: 'es', speechTag: 'es', welcomeMsg: 'Bienvenido a tu viaje.'},
'Polski': {wikiTag: 'pl', speechTag: 'pl-PL', welcomeMsg: 'Witamy w podróży.'},
'Tiếng Việt': {wikiTag: 'vi', speechTag: 'vi-VN', welcomeMsg: 'Chào mừng bạn đến với chuyến đi của bạn.'},
'日本語': {wikiTag: 'ja', speechTag: 'ja-JP', welcomeMsg: 'あなたのロードトリップへようこそ。'},
'中文': {wikiTag: 'ch', speechTag: 'zh-CN', welcomeMsg: '欢迎来到您的公路旅行。'},
'Português (PT)': {wikiTag: 'pt', speechTag: 'pt-PT', welcomeMsg: 'Bem-vindo à sua viagem.'},
'Português (BR)': {wikiTag: 'pt', speechTag: 'pt-BR', welcomeMsg: 'Bem-vindo à sua viagem.'},
'українська': {wikiTag: 'uk', speechTag: 'uk-UA', welcomeMsg: 'Ласкаво просимо до дорожньої подорожі.'},
'فارسی': {wikiTag: 'fa', speechTag: 'fa-IR', welcomeMsg: 'به سفر جاده ای شما خوش آمدید'},
'српски': {wikiTag: 'sr', speechTag: 'sr-BA', welcomeMsg: 'Добродошли на путовање.'},
'Català': {wikiTag: 'ca', speechTag: 'ca-ES', welcomeMsg: 'Benvingut al vostre viatge.'},
'العربية': {wikiTag: 'ar', speechTag: 'ar', welcomeMsg: 'مرحبا بك في رحلتك'},
};
function wikiUrl(path, api, mobile) {
let url = 'https://' + state.lang.wikiTag;
if (mobile) url += '.m';
return url + '.wikipedia.org/' + (api ? 'w/api.php?' : 'wiki/') + path
}
function startWatchLocation() {
return new Promise(resolve => {
navigator.geolocation.watchPosition(pos => {
if (!manualPosition) {
console.info('Updated position', pos);
position = pos;
if (map) {
map.setCenter({lat: pos.coords.latitude, lng: pos.coords.longitude});
}
}
resolve();
}, error => {
console.error('WatchPosition error.', error.message, error);
alert('Failed to find your current location.')
}, {
enableHighAccuracy: false,
maximumAge: 15000,
});
});
}
async function geoCode(position) {
const response = await fetchWithTimeout('https://roadtrip-api.glitch.me/geocode?latlng='
+ position.coords.latitude + ',' + position.coords.longitude);
if (response.ok) {
const json = await response.json();
//console.info('Geo coding result: ' + JSON.stringify(json));
if (json.status == 'OK') {
return json.results;
} else {
throw new Error('Reverse geocoding failed: ' + JSON.stringify(json));
}
} else {
const text = await response.text();
throw new Error('Reverse geocoding failed: ' + text);
}
}
function processResult(results) {
return results.filter(result => {
let found = false ;
result.types.forEach(type => {
let index = interestingTypes.indexOf(type);
if (index == -1) {
return;
}
if (!result.interestingness || result.interestingness > index) {
result.interestingness = index;
console.info('Location candidate', result.formatted_address, result.types);
found = true;
}
});
return found;
}).sort((a, b) => a.interestingness - b.interestingness);
}
async function searchWikipedia(term) {
const response = await fetchWithTimeout(wikiUrl('action=opensearch&format=json&origin=*&limit=1&search=' + encodeURIComponent(term), true));
if (!response.ok) {
console.error('Wikipedia call failed', response)
throw new Error('Wikipedia search is down');
}
const json = await response.json();
console.info('Search response', term, json);
const title = json[1][0]
if (title) {
return title;
}
const parts = term.split(/\,\s+/);
parts.pop();
const newTerm = parts.join(', ');
if (newTerm) {
return searchWikipedia(newTerm);
}
return null;
}
async function getContent(title) {
console.info('Getting content');
const response = await fetchWithTimeout(wikiUrl('redirects=true&format=json&origin=*&action=query&prop=extracts|coordinates&titles=' + encodeURIComponent(title), true));
if (!response.ok) {
console.error('Wikipedia content call failed', response)
throw new Error('Wikipedia content is down');
}
const json = await response.json();
const page = Object.values(json.query.pages)[0];
console.info('Page', page)
seen[page.title] = true;
return {
url: wikiUrl(encodeURIComponent(page.title), false),
title: page.title,
label: page.title,
content: simpleHtmlToText(page.extract.trim()),
lang: state.lang.speechTag,
coordinates: page.coordinates[0] ? {
lat: page.coordinates[0].lat,
lng: page.coordinates[0].lon,
} : null,
};
}
async function getArticleForLocation() {
console.info('Geo coding');
const locationResults = processResult(await geoCode(position));
console.info('Location results retrieved', locationResults.length);
let result;
while (result = locationResults.find(r => !seen[r.formatted_address])) {
seen[result.formatted_address] = true;
console.info('Searching for', result.formatted_address);
const title = await searchWikipedia(result.formatted_address);
if (!title) {
continue;
}
console.info('Title', title);
return getContent(title);
};
return null;
}
async function getNearbyArticle() {
console.info('Finding nearby article');
const response = await fetchWithTimeout(wikiUrl('action=query&format=json&origin=*&generator=geosearch&ggsradius=10000&ggsnamespace=0&ggslimit=50&formatversion=2&ggscoord=' + encodeURIComponent(position.coords.latitude) + '%7C' + encodeURIComponent(position.coords.longitude), true, true));
if (!response.ok) {
console.error('Wikipedia nearby failed', response)
throw new Error('Wikipedia nearby is down');
}
const json = await response.json();
console.info('Nearby response', json);
const pages = json.query.pages;
for (let page of pages) {
const title = page.title;
if (seen[title]) {
continue;
}
seen[title] = true;
console.info('Title', title);
return getContent(title);
}
return null;
}
async function speak(text, language) {
// Mobile Chrome doesn't like long texts, so we just do one sentence at a time.
// Make a sentence end a paragraph end. \w\w to not match e.g.
const paras = text.split(/\n/).filter(p => p.trim());
for (let p of paras) {
p = p.replace(/(\w\w\.)/, '$1\n')
const sentences = p.split(/\.\n/).filter(e => e.trim());
for (let sentence of sentences) {
await speakSentence(sentence, language);
if (cancelSpeaking) {
cancelSpeaking = false;
console.info('Cancel speaking');
return;
}
if (skipThisParagraph) {
skipThisParagraph = false;
console.info('Skipping paragraph');
break; // Goes to next step in paras loop
}
}
}
}
function speakSentence(text, language) {
var utterance = new SpeechSynthesisUtterance();
utterance.text = text + '.';
utterance.lang = language;
console.info('Start speaking', utterance.text);
let interval;
return new Promise(resolve => {
let spoke = false;
interval = setInterval(() => {
if (window.speechSynthesis.speaking) {
spoke = true;
}
if (spoke && !window.speechSynthesis.speaking) {
if (state.paused) {
return;
}
console.info('Detected end of speaking without end event', utterance.text);
resolve();
}
}, 100);
utterance.onend = () => {
console.info('End speaking', utterance.text);
resolve();
};
utterance.onerror = e => {
console.error('Error speaking', e.error, e);
resolve();
};
utterance.onpause = () => console.info('Pause');
utterance.onresume = () => console.info('Resume');
window.speechSynthesis.speak(utterance);
}).then(() => clearInterval(interval))
}
async function talkAboutLocation(article) {
state.status = `Reading about <a href="${article.url}" target="_blank">${html(article.title)}</a>`;
render();
gtag('config', 'UA-121987888-1', {
'page_title' : 'Article ' + article.title,
'page_path': '/article/' + encodeURIComponent(article.title),
});
if (article.coordinates && map) {
article.marker = new google.maps.Marker({
position: article.coordinates,
map: map,
title: article.title,
//label: article.title,
});
}
return speak(article.content, article.lang);
}
async function start() {
state.playing = true;
state.loading = true;
state.lang = languageMap[$('language_inner_select').value];
state.status = 'Finding your location.'
render();
const s = startWatchLocation();
$('toast').MaterialSnackbar.showSnackbar({
message: 'Please make sure your volume is turned up!',
timeout: 10000,
});
await speak(state.lang.welcomeMsg + '\n\n', state.lang.speechTag);
await s;
next();
}
async function next() {
console.info('Starting session');
state.playing = true;
state.loading = true;
state.status = 'Finding something interesting to read. I\'ll keep checking as you move.'
render();
gtag('config', 'UA-121987888-1', {
'page_title' : 'Next',
'page_path': '/next/' + state.lang.speechTag,
});
try {
console.info('Finding article.');
let article = await getArticleForLocation();
if (!article) {
console.info('Did not find location article. Trying nearby.');
article = await getNearbyArticle();
}
if (!article) {
console.info('Did not find article');
setTimeout(next, pollSeconds * 1000);
return;
}
console.info('Retrieved article');
state.loading = false;
render();
await talkAboutLocation(article);
} catch (e) {
console.error('Error :' + e, e);
setTimeout(next, pollSeconds * 1000);
return;
}
next();
}
function pause() {
state.paused = true;
render()
window.speechSynthesis.pause();
}
function play() {
state.paused = false;
render()
window.speechSynthesis.resume();
}
function forward() {
cancelSpeaking = true;
window.speechSynthesis.cancel();
}
function skipParagraph() {
skipThisParagraph = true;
window.speechSynthesis.cancel();
}
function render() {
$('html_start').style.display = display(!state.playing);
$('language_select').style.display = display(!state.playing);
$('html_pause').style.display = display(!state.loading && state.playing && !state.paused);
$('html_play').style.display = display(!state.loading && state.playing && state.paused);
$('html_next').style.display = display(!state.loading && state.playing);
$('html_skip').style.display = display(!state.loading && state.playing);
$('html_spinner').style.display = display(state.loading);
$('html_title').innerHTML = state.status;
}
function simpleHtmlToText(html) {
const div = document.createElement('div');
div.innerHTML = html;
remove(div.querySelector('#References'));
remove(div.querySelector('#See_also'));
let text = div.textContent;
text = text.replace(/(.)\n/g, '$1.\n');
// Remove stuff in parantheses. Nobody wants to hear that stuff.
// This isn't how you pass the Google interview.
// But it is technically O(n)
for (let i = 0; i < 10; i++) {
text = text.replace(/\([^\)]+\)/g, '');
}
return text;
}
function remove(element) {
if (!element) {
return false;
}
return element.parentElement.removeChild(element);
}
function $(id) {
return document.getElementById(id);
}
function display(bool) {
return bool ? 'block' : 'none';
}
function html(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function initMap() {
const startLoc = {lat: -34.397, lng: 150.644};
map = new google.maps.Map($('map'), {
center: startLoc,
zoom: 11
});
const centerMarker = new google.maps.Marker({
position: startLoc,
map: map,
title: 'Current Location'
});
map.addListener('center_changed', e => {
position = {
coords: {
longitude: map.getCenter().lng(),
latitude: map.getCenter().lat(),
}
};
centerMarker.setPosition({
lat: map.getCenter().lat(),
lng: map.getCenter().lng()
});
console.info('Map position', position);
manualPosition = true;
});
}
function initLanguageSelect(selected) {
var fragment = document.createDocumentFragment();
for (let language of Object.keys(languageMap)) {
var opt = document.createElement('option');
opt.innerHTML = language;
opt.value = language;
fragment.appendChild(opt);
}
$('language_inner_select').appendChild(fragment);
$('language_inner_select').value = selected;
}
function selectLang(langTag) {
let selected = null;
for (var langValue in languageMap) {
if (languageMap[langValue].speechTag === langTag || languageMap[langValue].wikiTag === langTag) {
selected = langValue;
break;
}
}
if (!selected) {
selected = 'Browser Language (' + langTag + ')';
languageMap[selected] = {
speechTag: langTag,
wikiTag: langTag.split('-')[0],
welcomeMsg: ''
};
}
initLanguageSelect(selected);
}
function timeout(time, message) {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Timeout: ' + message)), time);
})
}
function fetchWithTimeout(url, paras) {
return Promise.race([fetch(url, paras),
timeout(15 * 1000, 'Fetch timed out for ' + url)]);
}
async function guessLang() {
const langTag = navigator.language;
const wikiTag = langTag.split('-')[0];
const response = await fetchWithTimeout('https://' + wikiTag + '.wikipedia.org/w/api.php?redirects=true&format=json&origin=*&action=query&prop=extracts&titles=Main_Page');
if (response.ok) {
console.info('Set language to browser language', langTag);
return langTag;
}
}
function init() {
onunload = function() {
window.speechSynthesis.cancel();
}
navigator.serviceWorker.register("/sw.js");
const l = new URLSearchParams(location.search).get('lang');
if (l) {
selectLang(encodeURIComponent(l));
}
else {
guessLang().then(selectLang);
}
}
init();