Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
156 lines (131 loc) · 5.07 KB
/
Copy pathpopup.js
File metadata and controls
156 lines (131 loc) · 5.07 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
150
151
152
153
154
155
156
let currentProblem = null;
let currentDifficulty = "Unknown";
chrome.runtime.onMessage.addListener((message)=>{
if(message.type === "PROBLEM_DATA"){
currentProblem = message.name;
currentDifficulty = message.difficulty || "Unknown";
enableControls();
const difficultyClass = currentDifficulty.toLowerCase();
document.getElementById("status").innerHTML =
`Detected: ${currentProblem} <span class="difficulty ${difficultyClass}">[${currentDifficulty}]</span>`;
}
});
function enableControls(){
document.getElementById('saveBtn').disabled = false;
document.getElementById('confusingchk').disabled = false;
document.getElementById('revisionBtn').disabled = false;
}
function disableControls(){
document.getElementById('saveBtn').disabled = true;
document.getElementById('confusingchk').disabled = true;
document.getElementById('revisionBtn').disabled = true;
}
function disableSaveOnly() {
document.getElementById('saveBtn').disabled = true;
document.getElementById("confusingchk").disabled = true;
}
document.getElementById("revisionBtn").addEventListener("click", () => {
if (!currentProblem) return;
chrome.storage.local.get({ problems: [] }, (data) => {
const problem = data.problems.find(
p => p.name === currentProblem
);
if (!problem) {
document.getElementById("status").textContent =
"Save the problem before adding to revision";
return;
}
problem.revision = true;
chrome.storage.local.set({ problems: data.problems }, () => {
renderRevisionList(data.problems);
renderStats(data.problems);
document.getElementById("status").textContent =
"Added to revision";
disableControls();
});
});
});
function renderRevisionList(problems){
const ul = document.getElementById("revisionList");
ul.innerHTML = "";
problems
.filter(p => p.revision)
.forEach(p => {
const li = document.createElement("li");
const difficulty = p.difficulty || "Unknown";
const difficultyClass = difficulty.toLowerCase();
li.innerHTML = `${p.name} <span class="difficulty ${difficultyClass}">[${difficulty}]</span> (${p.pattern})`;
ul.appendChild(li);
});
}
document.getElementById("saveBtn").addEventListener("click", ()=> {
if(!currentProblem) return;
const pattern = document.getElementById("pattern").value;
const confusing = document.getElementById("confusingchk").checked;
const entry = {
name: currentProblem,
pattern,
difficulty: currentDifficulty,
confusing,
revision: false,
date: Date.now()
};
chrome.storage.local.get({problems: []}, (data) => {
const exists = data.problems.some(p => p.name === currentProblem);
if(exists) return;
data.problems.push(entry);
chrome.storage.local.set({problems: data.problems}, ()=>{
renderRevisionList(data.problems);
renderStats(data.problems);
document.getElementById("status").textContent = "Problem saved successfully";
disableSaveOnly();
})
})
})
function renderStats(problems){
const total = problems.length;
const confusingProblems = problems.filter(p => p.confusing);
// Difficulty breakdown
const difficultyMap = { Easy: 0, Medium: 0, Hard: 0 };
problems.forEach(p => {
const diff = p.difficulty || "Unknown";
if(difficultyMap.hasOwnProperty(diff)){
difficultyMap[diff]++;
}
});
// Topic breakdown for confusing problems
const topicMap = {};
confusingProblems.forEach(p=>{
if(!topicMap[p.pattern]){
topicMap[p.pattern] = 0;
}
topicMap[p.pattern]++;
});
let topicHTML = "";
for(const topic in topicMap){
topicHTML+= `<p> ${topic}: ${topicMap[topic]}</p>`
}
document.getElementById("stats").innerHTML =
`<p><strong>Total Solved: </strong> ${total}</p>
<p><strong>By Difficulty: </strong></p>
<p class="stat-indent"><span class="difficulty easy">Easy:</span> ${difficultyMap.Easy} | <span class="difficulty medium">Medium:</span> ${difficultyMap.Medium} | <span class="difficulty hard">Hard:</span> ${difficultyMap.Hard}</p>
<p><strong>Total Confusing: </strong> ${confusingProblems.length}</p>
<hr/>
<p><strong>Confusing by topic: </strong></p>
${topicHTML || "<p>None</p>"}`;
}
document.addEventListener("DOMContentLoaded", () => {
disableControls();
requestProblemFromTab();
document.getElementById("status").textContent = "No problem detected on this page";
chrome.storage.local.get({problems: []}, (data) => {
renderRevisionList(data.problems);
renderStats(data.problems);
})
});
function requestProblemFromTab(){
chrome.tabs.query({active: true, currentWindow: true}, (tabs)=>{
if(!tabs.length) return;
chrome.tabs.sendMessage(tabs[0].id, {type: "CHECK_PROBLEM"});
})
}