forked from MAX-ALCHEMIST/LeetTune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
53 lines (49 loc) · 1.42 KB
/
content.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
const successAudioElement = new Audio(chrome.runtime.getURL("assets/accepted.mp3"));
const wrongAnswerAudioElement = new Audio(
chrome.runtime.getURL("assets/wrong.mp3")
);
const playAudio = (audioElement) => {
audioElement
.play()
.then(() => {
console.log("Audio played successfully.");
})
.catch((error) => {
console.error("Error playing audio:", error);
});
};
let wrongAnswerPlayed = false;
const checkSuccessAndPlayAudio = () => {
const successTag = document.querySelector(".success__3Ai7");
if (successTag && successTag.innerText.trim() === "Success") {
playAudio(successAudioElement);
console.log("Correct answer!");
return true;
} else {
const wrongAns = document.getElementsByClassName("error__2Ft1");
if (wrongAns && wrongAns.length > 0 && !wrongAnswerPlayed) {
playAudio(wrongAnswerAudioElement);
console.log("Wrong answer!");
wrongAnswerPlayed = true;
}
return false;
}
};
window.onload = () => {
const successPlayed = checkSuccessAndPlayAudio();
if (successPlayed || wrongAnswerPlayed) {
setTimeout(() => {
window.location.reload();
}, 10000);
} else {
setInterval(() => {
const successPlayed = checkSuccessAndPlayAudio();
if (successPlayed || wrongAnswerPlayed) {
clearInterval(this);
setTimeout(() => {
window.location.reload();
}, 10000);
}
}, 2000);
}
};