|
1 | 1 | const mainElement = document.querySelector(".main-element"); |
2 | 2 | const test = document.querySelector(".test"); |
3 | | -console.log(test); |
4 | | -test.addEventListener("click", () => { |
5 | | - console.log("test"); |
6 | | -}); |
7 | 3 | const backgroundImg = document.querySelector("#background-img"); |
8 | 4 | const gptResponseElement = document.querySelector(".gpt-response"); |
9 | 5 | const headline = document.querySelector(".headline"); |
@@ -33,6 +29,7 @@ let textContent; |
33 | 29 | let imageUrl; |
34 | 30 | let isLactoseIntolerant; |
35 | 31 | let dishOriginCountry; |
| 32 | +let currentChar |
36 | 33 | const defaultRecipe = ` |
37 | 34 | Apologies, but our AI Recipe-Making expert is unavailable. Please try again later. In the meantime, please find one of our favourite recipes below. |
38 | 35 |
|
@@ -265,13 +262,71 @@ recipeButtons.forEach((button) => { |
265 | 262 | .then(() => { |
266 | 263 | console.log("image loaded:", Promise.all.status); |
267 | 264 | textContent = data.text.choices[0].message.content; |
268 | | - gptResponseElement.innerHTML = `${textContent}`; |
| 265 | + gptResponseElement.innerHTML = ` |
| 266 | + <div class="recording"> |
| 267 | + <i class="fa-solid fa-microphone"></i> |
| 268 | + <i class="fa-solid fa-pause"></i> |
| 269 | + <i class="fa-solid fa-stop"></i> |
| 270 | + <div class="speed-wrapper"> |
| 271 | + <label for="speed">Speed</label> |
| 272 | + <input type="number" name="speed" id="speed" min="0.25" max="2" step="0.25" value="1"> |
| 273 | + </div> |
| 274 | + </div> |
| 275 | + ${textContent}`; |
269 | 276 | removeElements([headline, allergies, ...recipeButtons]); |
270 | 277 | displayElements([ |
271 | 278 | userWantAnotherRecipe, |
272 | 279 | gptResponseElement, |
273 | 280 | sendRecipeToUserInboxBtn, |
274 | 281 | ]); |
| 282 | + |
| 283 | + const utterance = new SpeechSynthesisUtterance(); |
| 284 | + |
| 285 | + function readRecipe(recipe) { |
| 286 | + if(speechSynthesis.paused && speechSynthesis.speaking) { |
| 287 | + return speechSynthesis.resume(); |
| 288 | + } |
| 289 | + if(speechSynthesis.speaking) return |
| 290 | + utterance.text = recipe; |
| 291 | + utterance.rate = speedBtn.value || 1; |
| 292 | + speechSynthesis.speak(utterance); |
| 293 | + } |
| 294 | + function pauseReading() { |
| 295 | + if (speechSynthesis.speaking) speechSynthesis.pause(); |
| 296 | + } |
| 297 | + |
| 298 | + function stopREeading() { |
| 299 | + speechSynthesis.resume(); |
| 300 | + speechSynthesis.cancel(); |
| 301 | + } |
| 302 | + |
| 303 | + const microphoneBtn = document.querySelector(".fa-microphone"); |
| 304 | + const pauseBtn = document.querySelector(".fa-pause"); |
| 305 | + const stopBtn = document.querySelector(".fa-stop"); |
| 306 | + const speedBtn = document.querySelector("#speed"); |
| 307 | + |
| 308 | + console.log(speedBtn); |
| 309 | + |
| 310 | + // speedBtn.addEventListener("input", () => { |
| 311 | + // stopREeading(); |
| 312 | + // readRecipe(utterance.text.substring(currentChar)); |
| 313 | + |
| 314 | + // console.log("speed has been incremented") |
| 315 | + // }) |
| 316 | + |
| 317 | + // utterance.addEventListener("boundary", (e) => { |
| 318 | + // currentChar = e.charIndex; |
| 319 | + // }) |
| 320 | + |
| 321 | + stopBtn.addEventListener("click", stopREeading); |
| 322 | + |
| 323 | + pauseBtn.addEventListener("click", pauseReading); |
| 324 | + |
| 325 | + |
| 326 | + |
| 327 | + microphoneBtn.addEventListener("click", () => { |
| 328 | + readRecipe(`${textContent}`); |
| 329 | + }); |
275 | 330 | }) |
276 | 331 | .catch((error) => { |
277 | 332 | console.error("Error:", error); |
|
0 commit comments