Skip to content

Commit cc39a4b

Browse files
committed
feat: added vocab mountain stats
1 parent f72b895 commit cc39a4b

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

.changeset/fresh-lobsters-brake.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gregmat-buddy": patch
3+
---
4+
5+
added stats for vocab mountain

src/entrypoints/gregmat.content/vocab-mountain.ts

+95
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export async function main() {
22
registerKeybinds();
3+
addStats();
34
}
45

56
function eventGenerator(key: KeyboardEvent["key"]) {
@@ -59,3 +60,97 @@ function registerKeybinds() {
5960
document.body.dispatchEvent(keys.down);
6061
});
6162
}
63+
64+
async function addStats() {
65+
await waitForElement("button[tabindex='-1']");
66+
67+
const parent = document.querySelector(
68+
".w-full.flex.flex-col.justify-start.items-center.space-y-2"
69+
);
70+
71+
if (!parent) return;
72+
73+
let summary = document.getElementById("buddy-stats") as HTMLDivElement;
74+
75+
if (!summary) {
76+
summary = document.createElement("div");
77+
summary.textContent = "loading...";
78+
summary.classList.add("text-lg", "dark:text-gray-50", "text-center");
79+
summary.setAttribute("id", "buddy-stats");
80+
parent.prepend(summary);
81+
}
82+
83+
const day = document.querySelector(
84+
"h3[class='font-semibold text-lg dark:text-gray-50']"
85+
);
86+
87+
if (!day) return;
88+
89+
const buttonObserver = new MutationObserver((mutations) => {
90+
if (mutations.length > 1) return;
91+
92+
calculate_stats(summary);
93+
});
94+
95+
const dayObserver = new MutationObserver(() => {
96+
calculate_stats(summary);
97+
buttonObserver.disconnect();
98+
registerChanges();
99+
});
100+
101+
dayObserver.observe(day, {
102+
childList: true,
103+
});
104+
105+
function registerChanges() {
106+
document.querySelectorAll("button[tabindex='-1']").forEach((button) => {
107+
buttonObserver.observe(button, {
108+
attributes: true,
109+
attributeOldValue: true,
110+
});
111+
});
112+
}
113+
114+
calculate_stats(summary);
115+
registerChanges();
116+
}
117+
118+
function calculate_stats(summary: HTMLDivElement) {
119+
const buttons = document.querySelectorAll("button[tabindex='-1']");
120+
121+
if (buttons.length === 0) return;
122+
123+
let total = 0,
124+
correct = 0;
125+
126+
buttons.forEach((button) => {
127+
if (button.classList.contains("bg-green-200")) {
128+
correct++;
129+
total++;
130+
} else if (button.classList.contains("bg-red-200")) {
131+
total++;
132+
}
133+
});
134+
135+
if (total != buttons.length) total = -1;
136+
137+
summary.innerHTML = getVocabStatsText(correct, total);
138+
}
139+
140+
function getVocabStatsText(correct: number, total: number) {
141+
if (total == -1) {
142+
return `Stats: DNF`;
143+
}
144+
145+
const percentage = (correct / total) * 100;
146+
147+
let color = "#f57c00"; // text-orange-500
148+
149+
if (percentage >= 90) {
150+
color = "#4caf50"; // text-green-500
151+
}
152+
153+
return `Stats: <div class="flex">Accuracy: ${correct}/${total} | <p class="font-semibold ml-1" style="color: ${color};"> ${percentage.toFixed(
154+
2
155+
)}% </p> </div>`;
156+
}

0 commit comments

Comments
 (0)