/
background.js
333 lines (248 loc) · 9.52 KB
/
background.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
/*
This file is part of Talkie -- text-to-speech browser extension button.
<https://github.com/joelpurra/talkie>
Copyright (c) 2016 Joel Purra <https://joelpurra.com/>
Talkie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Talkie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Talkie. If not, see <https://www.gnu.org/licenses/>.
*/
// https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html#tts-section
// https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html#examples-synthesis
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API/Using_the_Web_Speech_API#Speech_synthesis
const extensionName = "Talkie";
const log = (...args) => {
const now = new Date().toISOString();
console.log(now, extensionName, ...args);
};
const logError = (...args) => {
const now = new Date().toISOString();
console.error(now, extensionName, ...args);
};
log("Start", "Loading");
const promiseTry = (fn) => new Promise(
(resolve, reject) => {
try {
const result = fn();
resolve(result);
} catch (error) {
reject(error);
}
}
);
const shallowCopy = (...objs) => Object.assign({}, ...objs);
const isUndefinedOrNullOrEmptyOrWhitespace = (str) => !(str && typeof str === "string" && str.length > 0 && str.trim().length > 0);
const noTextSelectedMessage = {
text: "Please select desired text on the website first!",
language: "en",
};
const setup = () => new Promise(
(resolve, reject) => {
log("Start", "Setup");
const synthesizer = window.speechSynthesis;
const handleVoicesChanged = (event) => {
delete synthesizer.onerror;
delete synthesizer.onvoiceschanged;
log("Variable", "synthesizer", synthesizer);
const unload = (e) => {
log("Start", "Unloading");
if(synthesizer.speaking) {
// Clear all text.
// TODO: check if the text was added by this extension, or something else.
synthesizer.cancel();
// Reset the system to resume playback, just to be nice to the world.
synthesizer.resume();
}
log("Done", "Unloading");
};
document.onbeforeunload = unload;
const voices = synthesizer.getVoices();
log("Variable", "voices[]", voices.length, voices.map(voice => {
return {
name: voice.name,
lang: voice.lang,
};
}));
log("Done", "Setup");
return resolve(synthesizer);
};
const handleError = (event) => {
delete synthesizer.onerror;
delete synthesizer.onvoiceschanged;
return reject(null);
};
synthesizer.onerror = handleError;
synthesizer.onvoiceschanged = handleVoicesChanged;
}
);
const speak = (synthesizer, text = "", language) => new Promise(
(resolve, reject) => {
log("Start", `Speak text (length ${text.length}): "${text}"`);
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = language;
// utterance.pitch = [0,2];
// utterance.rate = [0,2];
// utterance.voice = synthesizer.getVoices()[4];
log("Variable", "utterance", utterance);
const handleEnd = (event) => {
delete utterance.onend;
delete utterance.onerror;
log("End", `Speak text (length ${text.length}) spoken in ${event.elapsedTime} milliseconds.`);
return resolve();
};
const handleError = (event) => {
delete utterance.onend;
delete utterance.onerror;
log("Error", `Speak text (length ${text.length})`, event);
return reject();
};
utterance.onend = handleEnd;
utterance.onerror = handleError;
synthesizer.speak(utterance);
if (synthesizer.paused) {
synthesizer.resume();
}
log("Variable", "synthesizer", synthesizer);
log("Done", `Speak text (length ${text.length})`);
}
);
const getFramesSelectionTextAndLanguage = () => new Promise(
(resolve, reject) => {
const getTabVariablesCode = `var t = { text: document.getSelection().toString(), language: document.getElementsByTagName("html")[0].getAttribute("lang") }; t`;
chrome.tabs.executeScript(
{
allFrames: true,
matchAboutBlank: true,
code: getTabVariablesCode,
},
(results) => resolve(results)
);
}
);
const detectPageLanguage = () => new Promise(
(resolve, reject) => {
chrome.tabs.detectLanguage((language) => {
// https://developer.chrome.com/extensions/tabs#method-detectLanguage
log("detectLanguage", "Browser detected primary page language", language);
// The language fallback value is "und", so treat it as no language.
if (!language || typeof language !== "string" || language === "und") {
return resolve(null);
}
return resolve(language);
});
}
);
const cleanupSelections = (detectedPageLanguage, selections) => {
const hasValidText = (selection) => !isUndefinedOrNullOrEmptyOrWhitespace(selection.text);
const trimText = (selection) => {
var copy = shallowCopy(selection);
copy.text = copy.text.trim();
return copy;
};
const setEffectiveLanguage = (selection) => {
var copy = shallowCopy(selection);
copy.language = copy.language || detectedPageLanguage || null;
return copy;
};
const cleanedupSelections = selections
.filter(hasValidText)
.map(trimText)
.filter(hasValidText)
.map(setEffectiveLanguage);
if (cleanedupSelections.length === 0) {
log("Empty filtered selections");
cleanedupSelections.push(noTextSelectedMessage);
}
return cleanedupSelections;
};
const speakAllSelections = (synthesizer, selections, detectedPageLanguage) => {
log("Start", "Speaking all selections");
log("Variable", `selections (length ${selections && selections.length || 0})`, selections);
const cleanedupSelections = cleanupSelections(detectedPageLanguage, selections);
const speakPromises = cleanedupSelections.map((selection) => {
log("Text", `Speaking selection (length ${selection.text.length}, language ${selection.language})`, selection);
return speak(synthesizer, selection.text, selection.language);
});
log("Done", "Speaking all selections");
return Promise.all(speakPromises);
};
const speakUserSelection = (synthesizer) => promiseTry(() => {
log("Start", "Speaking selection");
return Promise.all(
[
getFramesSelectionTextAndLanguage(),
detectPageLanguage(),
]
)
.then(([framesSelectionTextAndLanguage, detectedPageLanguage]) => {
return speakAllSelections(synthesizer, framesSelectionTextAndLanguage, detectedPageLanguage);
})
.then(() => log("Done", "Speaking selection"));
});
const getIconModePaths = (name) => {
return {
"16": `src/resource/icon/icon-${name}/icon-16x16.png`,
"32": `src/resource/icon/icon-${name}/icon-32x32.png`,
"48": `src/resource/icon/icon-${name}/icon-48x48.png`,
"64": `src/resource/icon/icon-${name}/icon-64x64.png`,
"128": `src/resource/icon/icon-${name}/icon-128x128.png`,
"256": `src/resource/icon/icon-${name}/icon-256x256.png`,
"512": `src/resource/icon/icon-${name}/icon-512x512.png`,
"1024": `src/resource/icon/icon-${name}/icon-1024x1024.png`
};
}
const setIconMode = (name) => new Promise(
(resolve, reject) => {
log("Start", "Changing icon to", name);
const paths = getIconModePaths(name);
const details = {
path: paths,
};
chrome.browserAction.setIcon(
details,
() => {
log("Done", "Changing icon to", name);
resolve();
}
);
}
);
const setIconModePlaying = () => setIconMode("pause");
const setIconModePaused = () => setIconMode("play");
let rootChain = Promise.resolve();
const rootChainCatcher = (error) => {
logError(error);
};
const chain = (promise) => {
rootChain = rootChain
.then(promise)
.catch(rootChainCatcher);
}
chain(
() => setup()
.then((synthesizer) => {
const handleIconClick = (tab) => chain(() => promiseTry(() => {
const wasSpeaking = synthesizer.speaking;
// Clear all old text.
synthesizer.cancel();
if (!wasSpeaking) {
return Promise.all(
[
setIconModePlaying(),
speakUserSelection(synthesizer),
]
)
.then(() => setIconModePaused());
}
return setIconModePaused();
}));
chrome.browserAction.onClicked.addListener(handleIconClick);
})
);
log("Done", "Loading");