-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathautoread.js
127 lines (108 loc) · 3.56 KB
/
autoread.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
// functions to read alound the speedrun.
var currentSection = -1;
// current line from this section
var sectionLines;
var currentLineIndex = 0;
var currentLineText = "";
function goToNext(){
if(sectionLines && sectionLines[currentLineIndex]){
sectionLines[currentLineIndex].style.backgroundColor="";
}
currentLineIndex +=1;
if(currentLineIndex < sectionLines?.length){
let currentLine = sectionLines[currentLineIndex];
currentLine.style.backgroundColor="lightyellow";
handleCheckbox(currentLine);
currentLineText = handleSublist(currentLine);
}
else
{
currentSection +=1;
currentLineIndex = 0;
//update section
var sections = document.getElementsByClassName("section");
var thisSection = sections[currentSection];
sectionLines = thisSection.querySelectorAll("li")
let currentLine = sectionLines[currentLineIndex];
currentLine.style.backgroundColor="lightyellow";
handleCheckbox(currentLine);
currentLineText = handleSublist(currentLine);
}
}
function play(){
window.speechSynthesis.cancel();
speech.text = currentLineText;
window.speechSynthesis.speak(speech);
}
function playHotKey(event){
if (event.key == " " || event.type == "touchstart") {
event.preventDefault();
goToNext();
play();
}
if(event.key == " " && sectionLines && sectionLines[currentLineIndex]){
sectionLines[currentLineIndex].scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"});
}
}
function initSpeak(){
speech = new SpeechSynthesisUtterance();
addSpeakBox();
// Disable the "Enable Speech" button
let enableSpeechBtn = document.getElementById("enable_speech_btn");
enableSpeechBtn.disabled = true;
enableSpeechBtn.blur(); //take focus off of button, so we can press space right away
}
function addSpeakBox(){
/*
var speechBox = document.createElement("div");
speechBox.id = "speechbox"
speechBox.style.float="right";
speechBox.style.backgroundColor="#FBEFD5";
speechBox.style.border="1px solid black";
speechBox.style.marginRight="2em";
var sbTitle = document.createElement("div");
sbTitle.innerText = "Speech settings";
speechBox.appendChild(sbTitle);
var sbPlay = document.createElement("button");
sbPlay.innerText = "play";
sbPlay.addEventListener('click',play);
speechBox.appendChild(sbPlay);
var sbNext = document.createElement("button");
sbNext.innerText = "next";
sbNext.addEventListener('click',goToNext);
speechBox.appendChild(sbNext);
var outerSpeechBox = document.getElementById("sidebar");
if(outerSpeechBox){
speechBox.style.position="sticky";
speechBox.style.top="1em";
}
else{
var outerSpeechBox = document.createElement("div");
outerSpeechBox.style.position="fixed";
outerSpeechBox.style.width = "100%";
outerSpeechBox.style.margin="1em";
document.body.prepend(outerSpeechBox);
}
outerSpeechBox.appendChild(speechBox);
*/
var tBar = document.getElementById("topbar");
var speechBox = document.createElement("div");
speechBox.className = "topbarSection";
speechBox.style.backgroundColor = "lightgreen";
speechBox.innerText = "Speech Enabled.";
tBar.append(speechBox);
window.addEventListener('keydown', playHotKey, true);
window.addEventListener('touchstart', playHotKey, true);
}
function handleCheckbox(currentLine){
let inputElems = currentLine.getElementsByTagName("input");
for(let i = 0; i < inputElems.length; i++){
if(inputElems[i].type == "checkbox" &&
inputElems[i].checked == false){
inputElems[i].click();
}
}
}
function handleSublist(currentLine){
return Array.of(...currentLine.childNodes).filter(x=>x.nodeName != "OL" && x.nodeName != "UL").map(x=>x.textContent).join('');
}