-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
339 lines (312 loc) · 10.2 KB
/
main.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
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
328
329
330
331
332
333
334
335
336
337
338
339
const DEFAULT_GRAPH = document.getElementById("defaultGraph");
const EASY_GRAPH = document.getElementById("easyGraph");
const MEDIUM_GRAPH = document.getElementById("mediumGraph");
const HARD_GRAPH = document.getElementById("hardGraph");
const TIME_SPENT_GRAPH = document.getElementById("timeSpent");
const DEFAULT_COLOR = "#3033f5";
const EASY_COLOR = "#46c6c2";
const MEDIUM_COLOR = "#fac31d";
const HARD_COLOR = "#f8615c";
const TIME_SPENT = "#640D5F";
const graph_types = [
DEFAULT_GRAPH,
EASY_GRAPH,
MEDIUM_GRAPH,
HARD_GRAPH,
TIME_SPENT_GRAPH,
];
let orgQuestionData = [];
let questionData = [];
let optionSelected = -1;
const prepareQuestionData = () => {
const key = "questionSolvedDetails"; // Replace with your key
chrome.storage.local.get([key], (result) => {
if (!chrome.runtime.lastError) {
orgQuestionData = result[key] || [];
orgQuestionData.forEach((ques) => {
ques.date = new Date(JSON.parse(ques.date));
});
orgQuestionData.sort((a, b) => a.date - b.date);
questionData = orgQuestionData;
openDefaultGraph();
}
});
};
prepareQuestionData();
const ctx = document.getElementById("canvas");
let ctxInstance;
const createGraph = (labels, data, quesNo, color = null) => {
if (ctxInstance) {
ctxInstance.destroy();
}
ctxInstance = new Chart(ctx, {
type: "line",
data: {
labels: labels,
datasets: [
{
label: "Total Questions Solved: " + quesNo,
data: data,
borderWidth: 1,
},
],
},
cubicInterpolationMode: "monotone",
options: {
elements: {
line: {
tension: 0.4,
borderWidth: 5,
backgroundColor: !color ? DEFAULT_COLOR : color,
borderColor: !color ? DEFAULT_COLOR : color,
},
point: {
backgroundColor: !color ? DEFAULT_COLOR : color,
color: !color ? DEFAULT_COLOR : color,
},
},
scales: {
x: {
beginAtZero: false, // Avoid cutting off at the start
offset: true, // Add padding to the start and end of the axis
ticks: {
callback: function (value, index, ticks_array) {
let characterLimit = 5;
let label = this.getLabelForValue(value);
if (label.length >= characterLimit) {
return label
.slice(0, label.length)
.substring(0, characterLimit - 1)
.trim();
}
return label;
},
},
},
y: {
title: {
display: true,
text: "Seconds",
font: {
size: 14,
},
},
},
},
plugins: {
tooltip: {
callbacks: {
label: function (tooltipItem) {
const seconds = tooltipItem.raw;
let time_label = "";
if (seconds >= 60) {
time_label += ` ${Math.floor(seconds / 60)} m`;
}
if (seconds % 60 != 0 && seconds != 0) {
time_label += ` ${seconds % 60} s`;
}
return `Time: ${time_label}`;
},
},
},
},
},
});
};
const createDefaultGraph = () => {
const labels = questionData.map(
(ques) => ques.questionId + " - " + ques.title
);
const data = questionData.map((ques) => ques.timeTaken);
createGraph(labels, data, data.length, DEFAULT_COLOR);
createStatsTable(questionData);
};
const createGraphBasedOnDifficulty = (id) => {
const parsedQues = questionData.filter(
(ques) =>
ques.difficulty === (id == 0 ? "Easy" : id == 1 ? "Medium" : "Hard")
);
const labels = parsedQues.map((ques) => ques.questionId + " - " + ques.title);
const data = parsedQues.map((ques) => ques.timeTaken);
createGraph(
labels,
data,
data.length,
id == 0 ? EASY_COLOR : id == 1 ? MEDIUM_COLOR : HARD_COLOR
);
createStatsTable(parsedQues);
};
const createGraphBasedOnTimeSpend = () => {
const parseDataByDate = (data) => {
const groupedData = data.reduce((acc, obj) => {
const date = obj.date;
const day = String(date.getDate()).padStart(2, "0");
const month = String(date.getMonth() + 1).padStart(2, "0");
const year = date.getFullYear();
const dateKey = `${day}-${month}-${year}`;
if (!acc[dateKey]) {
acc[dateKey] = { timeTaken: 0, frequency: 0 };
}
acc[dateKey].timeTaken += obj.timeTaken;
acc[dateKey].frequency += 1;
return acc;
}, {});
return Object.entries(groupedData).map(
([date, { timeTaken, frequency }]) => ({
date,
timeTaken,
frequency,
})
);
};
const result = parseDataByDate(questionData);
const labels = result.map(
(item) => item.date + " \nSolved: " + item.frequency
);
const data = result.map((item) => item.timeTaken);
const quesNo = result.reduce((sum, item) => sum + item.frequency, 0);
createGraph(labels, data, quesNo, TIME_SPENT);
createStatsTable(result);
};
const updateButtonClick = (id) => {
for (const g of graph_types) g.style.borderColor = "#000";
optionSelected = id;
if (id == 0) {
DEFAULT_GRAPH.style.borderColor = DEFAULT_COLOR;
createDefaultGraph();
} else if (id == 1) {
EASY_GRAPH.style.borderColor = EASY_COLOR;
createGraphBasedOnDifficulty(0);
} else if (id == 2) {
MEDIUM_GRAPH.style.borderColor = MEDIUM_COLOR;
createGraphBasedOnDifficulty(1);
} else if (id == 3) {
HARD_GRAPH.style.borderColor = HARD_COLOR;
createGraphBasedOnDifficulty(2);
} else if (id == 4) {
TIME_SPENT_GRAPH.style.borderColor = TIME_SPENT;
createGraphBasedOnTimeSpend();
}
};
DEFAULT_GRAPH.addEventListener("click", () => {
updateButtonClick(0);
});
EASY_GRAPH.addEventListener("click", () => {
updateButtonClick(1);
});
MEDIUM_GRAPH.addEventListener("click", () => {
updateButtonClick(2);
});
HARD_GRAPH.addEventListener("click", () => {
updateButtonClick(3);
});
TIME_SPENT_GRAPH.addEventListener("click", () => {
updateButtonClick(4);
});
// for the select option
document.getElementsByTagName("select")[0].onchange = function () {
var index = this.selectedIndex;
const currentTime = new Date();
if (index == 1) {
const sevenDaysAgo = new Date(currentTime);
sevenDaysAgo.setDate(currentTime.getDate() - 7);
questionData = orgQuestionData.filter((ques) => ques.date >= sevenDaysAgo);
} else if (index == 2) {
const thirtyDaysAgo = new Date(currentTime);
thirtyDaysAgo.setDate(currentTime.getDate() - 30);
questionData = orgQuestionData.filter((ques) => ques.date >= thirtyDaysAgo);
} else if (index == 3) {
const ninetyDaysAgo = new Date(currentTime);
ninetyDaysAgo.setDate(currentTime.getDate() - 90);
questionData = orgQuestionData.filter((ques) => ques.date >= ninetyDaysAgo);
} else {
questionData = orgQuestionData;
}
updateButtonClick(optionSelected);
};
const openDefaultGraph = () => {
updateButtonClick(0);
};
// to create a stats table
const statsTable = document.getElementById("statsTable");
const convertSecondsToLeetcodeFormat = (timeInSeconds) => {
const minutes = Math.floor(timeInSeconds / 60);
const seconds = Math.floor(timeInSeconds % 60);
let label = "";
if (minutes != 0) label = `${minutes} m `;
if (seconds != 0) label += `${seconds} s`;
return label;
};
const createStatsTable = (data) => {
const noOfQues = data.length;
statsTable.innerHTML = "";
if (noOfQues == 0) {
return;
}
const totalTimeTaken = data.reduce((sum, item) => sum + item.timeTaken, 0);
const avg = totalTimeTaken / noOfQues;
const lowest = data.reduce((low, item) => {
return Math.min(low, item.timeTaken);
}, 1e9);
const maximum = data.reduce(
(high, item) => Math.max(high, item.timeTaken),
0
);
const columnTitles = ["Stats", "Time"];
const rows = [
{
name: "Average Time",
value: convertSecondsToLeetcodeFormat(avg.toFixed(2)),
},
{ name: "Minimum Time", value: convertSecondsToLeetcodeFormat(lowest) },
{ name: "Maximum Time", value: convertSecondsToLeetcodeFormat(maximum) },
];
const table = document.createElement("table");
table.style.borderCollapse = "collapse";
table.style.margin = "20px auto";
table.style.width = "50%";
table.style.textAlign = "left";
const thead = document.createElement("thead");
const headerRow = document.createElement("tr");
columnTitles.forEach((title) => {
const th = document.createElement("th");
th.textContent = title;
th.style.border = "1px solid #ddd";
th.style.padding = "10px";
th.style.backgroundColor = "#f2f2f2";
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
const tbody = document.createElement("tbody");
rows.forEach((row) => {
const tr = document.createElement("tr");
const statsCell = document.createElement("td");
statsCell.textContent = row.name;
statsCell.style.border = "1px solid #ddd";
statsCell.style.padding = "10px";
tr.appendChild(statsCell);
const valueCell = document.createElement("td");
valueCell.textContent = row.value;
valueCell.style.border = "1px solid #ddd";
valueCell.style.padding = "10px";
tr.appendChild(valueCell);
tbody.appendChild(tr);
});
table.appendChild(tbody);
statsTable.appendChild(table);
};
// share the extnesion function
document.getElementById("shareBtn").addEventListener("click", () => {
const textToShare =
"I'm using this awesome Chrome extension to track how much time I spend solving LeetCode questions. It helps me analyze and improve my performance over time! 📈\n🔗 Get it here: https://github.com/somesh4545/leetcode-progress-tracker/\nHow it works:\n1️⃣ Tracks time spent on each question automatically.\n2️⃣ Provides stats and visualizations to help you improve.\n3️⃣ Simple to use—just solve problems and check your stats anytime!\n\nIt's been a game-changer for me—give it a try and share your progress! 🎯";
navigator.clipboard
.writeText(textToShare)
.then(() => {
alert("Text is copied to clipboard");
})
.catch((err) => {
console.error("Failed to copy text");
console.log(err);
});
});