-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
327 lines (286 loc) · 10.2 KB
/
Copy pathscript.js
File metadata and controls
327 lines (286 loc) · 10.2 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
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
// ---------- SPEED READING ENGINE ----------
let wordsArray = []; // array of words
let currentIndex = 0; // current word position
let isPlaying = false;
let timer = null;
let currentWPM = 300; // default
let splitLength = 16; // words of this length are split
let speedChange = 10; // increment/decrement wpm
let posChange = 100; // increment/decrement position
let maxWPM = 800;
let longLength = 10; // words of this length are displayed twice
// DOM elements
document.addEventListener('DOMContentLoaded', function() {
const wordDisplay = document.getElementById('wordDisplay');
const resetBtn = document.getElementById('resetBtn');
const wpmDisplay = document.getElementById('wpmDisplay');
const textInput = document.getElementById('textInput');
const loadTextBtn = document.getElementById('loadTextBtn');
const sampleTextBtn = document.getElementById('sampleTextBtn');
const statusMsg = document.getElementById('statusMsg');
const themeToggle = document.getElementById('themeToggle');
const fontToggle = document.getElementById('fontToggle');
const playToggle = document.getElementById('playToggle');
const incrementWPMBtn = document.getElementById('incrementWPMBtn');
const decrementWPMBtn = document.getElementById('decrementWPMBtn');
const incrementPosBtn = document.getElementById('incrementPosBtn');
const decrementPosBtn = document.getElementById('decrementPosBtn');
});
function incrementWPM() {
let newWPM = currentWPM + speedChange;
if (newWPM > maxWPM) newWPM = maxWPM;
updateWPM(newWPM);
}
function decrementWPM() {
let newWPM = currentWPM - speedChange;
if (newWPM < speedChange) newWPM = speedChange;
updateWPM(newWPM);
}
function incrementPos() {
let newPos = currentIndex + posChange;
if (newPos > wordsArray.length - 1) newPos = wordsArray.length;
currentIndex = newPos;
}
function decrementPos() {
let newPos = currentIndex - posChange;
if (newPos < 0) newPos = - 1;
currentIndex = newPos;
}
// ---------- THEME MANAGEMENT ----------
function setTheme(theme) {
if (theme === 'light') {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
themeToggle.textContent = '⏾'; // Moon icon for switching to dark
} else {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
themeToggle.textContent = '☀︎'; // Sun icon for switching to light
}
}
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
if (currentTheme === 'light') {
setTheme('dark');
} else {
setTheme('light');
}
}
function loadSavedTheme() {
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
setTheme('light');
} else {
setTheme('dark'); // default to dark
}
}
/// ---------- FONT MANAGEMENT ----------
function setFont(fontType) {
if (fontType === 'serif') {
document.documentElement.setAttribute('data-font', 'serif');
localStorage.setItem('font', 'serif');
} else {
document.documentElement.setAttribute('data-font', 'sans');
localStorage.setItem('font', 'sans');
}
}
function toggleFont() {
const currentFont = document.documentElement.getAttribute('data-font');
if (currentFont === 'serif') {
setFont('sans');
} else {
setFont('serif');
}
}
function loadSavedFont() {
const savedFont = localStorage.getItem('font');
if (savedFont === 'sans') {
setFont('sans');
} else {
setFont('serif'); // default to serif
}
}
// ---------- SPEED READING ENGINE ----------
function getDelayMs() {
if (currentWPM <= 0) return 200;
return 60000 / currentWPM;
}
function stopTimer() {
if (timer) {
clearInterval(timer);
timer = null;
}
isPlaying = false;
}
function displayCurrentWord() {
if (wordsArray.length === 0) {
wordDisplay.innerText = '📖';
return;
}
if (currentIndex >= wordsArray.length) {
wordDisplay.innerText = '🏁 THE END';
stopTimer();
statusMsg.innerText = '✅ Finished! Load new text or reset.';
return;
}
let word = wordsArray[currentIndex];
wordDisplay.innerText = word;
}
function nextWord() {
if (!isPlaying) return;
if (wordsArray.length === 0) {
stopTimer();
statusMsg.innerText = '⚠️ No text loaded. Paste something first.';
wordDisplay.innerText = '📭';
return;
}
if (currentIndex < wordsArray.length) {
currentIndex++;
displayCurrentWord();
if (currentIndex >= wordsArray.length) {
stopTimer();
statusMsg.innerText = '🎉 Complete! Great job.';
wordDisplay.innerText = '✨ DONE ✨';
}
} else {
stopTimer();
}
}
function togglePlay() {
isPlaying = !(isPlaying);
if (isPlaying) {
startReading();
} else {
pauseReading();
}
}
function startReading() {
if (wordsArray.length === 0) {
statusMsg.innerText = '❌ No text loaded. Paste or load sample text.';
return;
}
if (currentIndex >= wordsArray.length) {
statusMsg.innerText = '🔄 End reached. Press RESET to start over.';
return;
}
stopTimer();
isPlaying = true;
const delay = getDelayMs();
timer = setInterval(() => {
nextWord();
}, delay);
statusMsg.innerText = `▶️ Reading at ${currentWPM} WPM | word ${currentIndex+1}/${wordsArray.length}`;
}
function pauseReading() {
stopTimer();
statusMsg.innerText = `⏸ Paused at word ${currentIndex+1}/${wordsArray.length}`;
}
function resetReading() {
stopTimer();
currentIndex = 0;
displayCurrentWord();
if (wordsArray.length > 0) {
statusMsg.innerText = `⟳ Reset to start. ${wordsArray.length} words loaded.`;
} else {
statusMsg.innerText = `⟳ Reset, but no text loaded.`;
}
}
function duplicateLongOrEndingWithPeriod(arr) {
let result = [];
for (let item of arr) {
if (item.length > longLength || /[.!?]$/.test(item)) {
result.push(item, item);
} else {
result.push(item);
}
}
return result;
}
function loadTextFromInput() {
const rawText = textInput.value;
if (!rawText.trim()) {
statusMsg.innerText = '⚠️ Please paste some text first.';
return false;
}
let words = rawText.trim().split(/\s+/);
words = words.filter(w => w.length > 0);
// Split long words into halves
let processedWords = [];
for (let word of words) {
if (word.length > splitLength && word.includes('-')) {
// Split along hyphen
let hyphenParts = word.split('-');
for (let i = 0; i < hyphenParts.length; i++) {
let part = hyphenParts[i];
if (part.length > 0) {
// processedWords.push(part);
processedWords.push(i === hyphenParts.length - 1 ? part : part + '-');
}
}
} else if (word.length > splitLength) {
const midpoint = Math.ceil(word.length / 2);
const firstHalf = word.slice(0, midpoint);
const secondHalf = word.slice(midpoint);
processedWords.push(firstHalf, secondHalf);
} else {
processedWords.push(word);
}
}
if (processedWords.length === 0) {
statusMsg.innerText = '❌ No valid words found.';
return false;
}
wordsArray = duplicateLongOrEndingWithPeriod(processedWords);
currentIndex = 0;
stopTimer();
displayCurrentWord();
statusMsg.innerText = `📚 Loaded ${wordsArray.length} words. Press PLAY.`;
return true;
}
function loadSampleText() {
const sample = `The art of reading rapidly is not about skipping meaning but about reducing subvocalization and expanding peripheral vision. When you see one word at a time, your brain can process faster without regression. This method is called RSVP - Rapid Serial Visual Presentation. Studies show that with practice, comprehension remains high even at 500 to 700 words per minute. Trust your eyes and let the words flow. Speed reading is a superpower hiding in plain sight. Enjoy the journey!`;
textInput.value = sample;
loadTextFromInput();
}
function updateWPM(value) {
currentWPM = value;
if (isPlaying && wordsArray.length > 0 && currentIndex < wordsArray.length) {
stopTimer();
isPlaying = true;
const newDelay = getDelayMs();
timer = setInterval(() => {
nextWord();
}, newDelay);
statusMsg.innerText = `⚡ Speed changed to ${currentWPM} WPM (continuing)`;
} else if (!isPlaying) {
statusMsg.innerText = `⚙️ Speed set to ${currentWPM} WPM`;
}
}
// ---------- EVENT LISTENERS ----------
playToggle.addEventListener('click', function() {
togglePlay();
this.textContent = isPlaying ? '⏸' : '▶';
// if (this.textContent === '▶') {
// this.textContent = '■'; // or use '⏹' for a more standard stop icon
// } else {
// this.textContent = '▶';
// }
}
);
resetBtn.addEventListener('click', resetReading);
themeToggle.addEventListener('click', toggleTheme);
fontToggle.addEventListener('click', toggleFont);
incrementWPMBtn.addEventListener('click', incrementWPM);
decrementWPMBtn.addEventListener('click', decrementWPM);
incrementPosBtn.addEventListener('click', incrementPos);
decrementPosBtn.addEventListener('click', decrementPos);
loadTextBtn.addEventListener('click', loadTextFromInput);
sampleTextBtn.addEventListener('click', loadSampleText);
// ---------- INITIALIZATION ----------
(function init() {
loadSavedTheme(); // Load user's theme preference
const introText = "Welcome to Speed Reader! Paste any article, book excerpt, or story. The words will flash one at a time. Click ▶ to start. Adjust speed with 🐢/🐇. This method boosts reading speed dramatically. Have fun and train your brain.";
textInput.value = introText;
loadTextFromInput();
currentWPM = 300;
})();