/
background.js
197 lines (145 loc) · 5.96 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
/*
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 setup = () => new Promise(
(resolve, reject) => {
log("Start", "Setup");
const synthesizer = window.speechSynthesis || window.webkitSpeechSynthesis || window.mozSpeechSynthesis || window.oSpeechSynthesis || window.msSpeechSynthesis;
const handleVoicesChanged = (event) => {
synthesizer.onerror = null;
synthesizer.onvoiceschanged = null;
document.onbeforeunload = (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");
};
const voices = synthesizer.getVoices();
log("Variable", "synthesizer", synthesizer);
log("Variable", "voices", voices);
log("Done", "Setup");
resolve(synthesizer);
};
const handleError = (event) => {
synthesizer.onerror = null;
synthesizer.onvoiceschanged = null;
reject();
};
synthesizer.onerror = reject;
synthesizer.onvoiceschanged = handleVoicesChanged;
log("Variable", "synthesizer", synthesizer);
}
);
const speak = (synthesizer = null, text = "", language = null) => 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) => {
utterance.onend = null;
utterance.onerror = null;
log("End", `Speak text (length ${text.length}) spoken in ${event.elapsedTime} milliseconds.`);
resolve();
};
const handleError = (event) => {
utterance.onend = null;
utterance.onerror = null;
log("End", `Speak text (length ${text.length}) spoken in ${event.elapsedTime} milliseconds.`);
resolve();
};
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})`);
}
);
log("Done", "Loading");
let rootChain = Promise.resolve();
const rootChainCatcher = (e) => {
log("Error", e);
};
const chain = (promise) => {
rootChain = rootChain
.then(promise)
.catch(rootChainCatcher);
}
chain(
setup()
.then((synthesizer) => {
// const text = $$(".entry-content p").map(e => e.textContent).join("\n\n");
const text = "Testing testing testing!";
return speak(synthesizer, text)
.then(() => synthesizer);
})
.then((synthesizer) => {
const speakSelection = () => {
try {
log("Start", "Speaking selection");
const speakAllSelections = (selections) => {
log("Start", "Speaking all selections");
log("Variable", `selections (length ${selections && selections.length || 0}) ${selections}`);
if (selections) {
selections.forEach((selection) => {
log("Text", "Speaking selection:", selection);
chain(speak(synthesizer, selection.text, selection.language));
});
}
log("Done", "Speaking all selections");
};
chrome.tabs.executeScript(
{
allFrames: true,
matchAboutBlank: true,
code: 'var t = { text: document.getSelection().toString(), language: document.getElementsByTagName("html")[0].getAttribute("lang") }; t'
},
speakAllSelections
);
log("Done", "Speaking selection");
} catch(error) {
log("Error", "speakSelection", error);
debugger;
}
};
const handleIconClick = (tab) => {
// Clear all old text.
synthesizer.cancel();
if (!synthesizer.speaking) {
speakSelection();
}
};
chrome.browserAction.onClicked.addListener(handleIconClick);
})
);