-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlearn.js
More file actions
149 lines (119 loc) · 3.55 KB
/
Copy pathlearn.js
File metadata and controls
149 lines (119 loc) · 3.55 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
/*
* Learn page logic.
* Uses shared verb data from data/irregular-verbs.js.
* Shows one verb at a time and handles Next and Speak controls.
*/
const irregularVerbs = Array.isArray(window.IRREGULAR_VERBS) ? window.IRREGULAR_VERBS : [];
// Difficulty
const storedDifficulty = safeGet(localStorage, "selectedDifficulty");
const allowedDifficulties = ["easy", "medium", "hard"];
const difficulty = allowedDifficulties.includes(storedDifficulty) ? storedDifficulty : "easy";
const filteredVerbs = irregularVerbs.filter(
v => v.difficulty === difficulty
);
// DOM
const speakButton = document.getElementById("speakButton");
const verbPrompt = document.getElementById("verbPrompt");
const answerText = document.getElementById("answerText");
const nextButton = document.getElementById("nextButton");
const progBar = document.getElementById("progBar");
// State
let currentVerb = null;
let countdown = -1;
let intervalId = null;
// Init
document.addEventListener("DOMContentLoaded", () => {
if (filteredVerbs.length === 0) {
verbPrompt.textContent = "No verbs available.";
nextButton.disabled = true;
speakButton.disabled = true;
return;
}
showNewVerb();
nextButton.addEventListener("click", () => {
showNewVerb();
});
setupSpeech();
});
// Show Verb
function showNewVerb() {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
countdown = 2;
answerText.textContent = "";
const randomIndex = Math.floor(
Math.random() * filteredVerbs.length
);
currentVerb = filteredVerbs[randomIndex];
window.FlashcardProgress?.recordView(currentVerb);
verbPrompt.textContent = currentVerb.base;
restartProgressAnimation((countdown + 1) * 1000);
intervalId = setInterval(() => {
countdown--;
if (countdown === -1) {
clearInterval(intervalId);
intervalId = null;
answerText.textContent =
`${currentVerb.base} ${currentVerb.past} ${currentVerb.pp}`;
}
}, 1000);
}
// Progress animation
function restartProgressAnimation(durationMs) {
if (!progBar) return;
progBar.getAnimations().forEach(anim => anim.cancel());
progBar.style.width = "0%";
progBar.offsetWidth;
progBar.animate(
[
{ width: "0%" },
{ width: "100%" }
],
{
duration: durationMs,
fill: "forwards"
}
);
}
// Speak button
function setupSpeech() {
if (!("speechSynthesis" in window) || typeof SpeechSynthesisUtterance === "undefined") {
speakButton.disabled = true;
speakButton.title = "Text-to-speech not supported";
return;
}
speakButton.addEventListener("click", () => {
if (!currentVerb) return;
window.speechSynthesis.cancel();
const utterance = new SpeechSynthesisUtterance(
`${currentVerb.base}, ${currentVerb.past}, ${currentVerb.pp}`
);
utterance.rate = 0.9;
utterance.volume = 1;
const voices = window.speechSynthesis.getVoices();
const englishVoice =
voices.find(v => v.lang === "en-US") ||
voices.find(v => v.lang === "en-GB") ||
voices.find(v => v.lang.startsWith("en"));
if (englishVoice) {
utterance.voice = englishVoice;
utterance.lang = englishVoice.lang;
} else {
utterance.lang = "en-US";
}
window.speechSynthesis.speak(utterance);
});
}
// Instagram detection
function isInstagramWebView() {
const ua = navigator.userAgent || "";
return ua.includes("Instagram");
}
document.addEventListener("DOMContentLoaded", () => {
const speakButton = document.getElementById("speakButton");
if (isInstagramWebView()) {
speakButton.style.display = "none";
}
});