-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_widget.js
More file actions
41 lines (35 loc) · 1.08 KB
/
custom_widget.js
File metadata and controls
41 lines (35 loc) · 1.08 KB
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
const ServiceURL = "";
const ServiceAPIKey = "";
const IncludeDisplayName = true; // "(DisplayName) Says: " will be prepended before the text
const PollRate = 5000
let ttsQueueArray = [];
let ttsPlaying = false;
let theQueue;
function checkQueue() {
if (ttsQueueArray.length == 0) {
clearInterval(theQueue);
return;
}
if (ttsPlaying == false) {
speak(ttsQueueArray.shift());
}
}
function speak(text) {
let ServiceURLSlash = (ServiceURL.slice(-1) == "/") ? ServiceURL : ServiceURL + "/";
const src = ServiceURLSlash + "say" + "?text=" + text + "&apikey=" + ServiceAPIKey;
const audio = new Audio(src);
ttsPlaying = true;
audio.play();
audio.addEventListener("ended", () => {
ttsPlaying = false;
})
}
window.addEventListener("onEventReceived", function ( obj ) {
let data = obj.detail.event.data
if( data.text.indexOf("!tts ") !== 0 ) {
return;
}
let text = (IncludeDisplayName) ? data.displayName + " says: " + data.text.substring(5) : data.text.substring(5);
ttsQueueArray.push(text);
theQueue = setInterval(checkQueue, PollRate);
});