-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
331 lines (282 loc) · 10.2 KB
/
Copy pathcontent.js
File metadata and controls
331 lines (282 loc) · 10.2 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
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
console.log('LeetCode Spaced Repetition: content.js loaded!');
class LeetCodeMonitor {
constructor() {
this.isMonitoring = false;
this.startTime = null;
this.problemData = null;
this.pageEntryTime = this.getOrSetPageEntryTime();
this.hasSubmitted = false;
console.log('LeetCode Spaced Repetition: Page entry time:', this.pageEntryTime, new Date(this.pageEntryTime).toLocaleTimeString());
this.init();
}
init() {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => this.setupMonitoring());
} else {
this.setupMonitoring();
}
}
setupMonitoring() {
if (this.isProblemPage()) {
this.monitorSubmissionButton();
}
}
isProblemPage() {
return window.location.pathname.includes('/problems/') &&
!window.location.pathname.includes('/submissions/');
}
extractProblemData() {
try {
let title = '';
const urlMatch = window.location.href.match(/\/problems\/([^\/]+)/);
if (urlMatch) {
const urlTitle = urlMatch[1].replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
title = urlTitle;
}
if (!title) {
let titleElement = document.querySelector('[data-cy="question-title"]');
if (titleElement) {
title = titleElement.textContent.trim();
}
}
if (!title) {
const h1 = document.querySelector('h1');
if (h1) title = h1.textContent.trim();
}
const numberMatch = title.match(/^(\d+)\./);
const number = numberMatch ? parseInt(numberMatch[1]) : null;
let difficultyElement = document.querySelector('[diff]');
if (!difficultyElement) {
difficultyElement = document.querySelector('.text-difficulty-easy, .text-difficulty-medium, .text-difficulty-hard');
}
let difficulty = '';
if (difficultyElement) {
difficulty = difficultyElement.getAttribute('diff') ||
difficultyElement.textContent.trim() ||
(difficultyElement.className.includes('easy') ? 'Easy' :
difficultyElement.className.includes('medium') ? 'Medium' :
difficultyElement.className.includes('hard') ? 'Hard' : '');
}
const url = window.location.href;
let tags = [];
const tagElements = document.querySelectorAll('[data-cy="question-tags"] a, .tag__1PqS, .css-1kg1yv8 .css-1kg1yv8');
if (tagElements.length === 0) {
const altTagElements = document.querySelectorAll('.mt-2 .inline-block a, .mt-2 .inline-block span');
altTagElements.forEach(el => {
if (el.textContent) tags.push(el.textContent.trim());
});
} else {
tagElements.forEach(el => {
if (el.textContent) tags.push(el.textContent.trim());
});
}
tags = Array.from(new Set(tags));
this.problemData = {
id: number,
title: title,
difficulty: difficulty,
url: url,
tags: tags,
timestamp: Date.now()
};
console.log('LeetCode Spaced Repetition: Problem data extracted:', this.problemData);
} catch (error) {
console.error('LeetCode Spaced Repetition: Error extracting problem data:', error);
}
}
startSubmissionMonitoring() {
if (this.isMonitoring) return;
this.extractProblemData();
this.isMonitoring = true;
this.startTime = Date.now();
this.hasSubmitted = false;
console.log('LeetCode Spaced Repetition: Starting submission monitoring');
this.observeSubmissionResults();
this.monitorSuccessMessages();
}
observeSubmissionResults() {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
this.checkForSuccess(node);
}
});
}
if (mutation.type === 'attributes') {
this.checkForSuccess(mutation.target);
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['class', 'data-testid', 'data-cy']
});
}
monitorSubmissionButton() {
document.addEventListener('click', (event) => {
const target = event.target;
if (target.textContent.includes('Submit') ||
target.closest('[data-cy="submit-code-btn"]') ||
target.closest('button[type="submit"]')) {
console.log('LeetCode Spaced Repetition: Submit button clicked, starting monitoring');
this.startSubmissionMonitoring();
}
});
}
monitorSuccessMessages() {
const checkInterval = setInterval(() => {
if (this.isMonitoring && this.startTime) {
const timeSinceSubmission = Date.now() - this.startTime;
console.log('LeetCode Spaced Repetition: Checking for success, time since submission:', timeSinceSubmission + 'ms');
this.checkForSuccessInDOM();
if (timeSinceSubmission > 30000) {
console.log('LeetCode Spaced Repetition: Stopping success monitoring after 30 seconds');
clearInterval(checkInterval);
this.isMonitoring = false;
}
} else {
clearInterval(checkInterval);
}
}, 1000);
}
checkForSuccessInDOM() {
console.log('LeetCode Spaced Repetition: Checking DOM for success...');
if (window.location.href.includes('/submissions/')) {
console.log('On submission page, checking for success indicators...');
const pageText = document.body.textContent.toLowerCase();
if (pageText.includes('accepted')) {
console.log('LeetCode Spaced Repetition: Found success on submission page');
this.handleSuccessfulSubmission();
return;
}
}
const allElements = document.querySelectorAll('*');
for (const element of allElements) {
const text = element.textContent.toLowerCase();
const className = element.className.toLowerCase();
if (text.includes('accepted')) {
const timeSinceSubmission = Date.now() - this.startTime;
if (timeSinceSubmission < 30000) {
console.log('LeetCode Spaced Repetition: Found success text:', text.substring(0, 100));
this.handleSuccessfulSubmission();
return;
}
}
if (className.includes('success') ||
className.includes('accepted') ||
className.includes('green') ||
className.includes('passed')) {
console.log('LeetCode Spaced Repetition: Found success class:', className);
this.handleSuccessfulSubmission();
return;
}
}
const allText = document.body.textContent.toLowerCase();
const successPatterns = [
/accepted/i
];
for (const pattern of successPatterns) {
if (pattern.test(allText)) {
const timeSinceSubmission = Date.now() - this.startTime;
if (timeSinceSubmission < 30000) {
console.log('LeetCode Spaced Repetition: Found success pattern:', pattern);
this.handleSuccessfulSubmission();
return;
}
}
}
}
checkForSuccess(element) {
const successSelectors = [
'[data-cy="submission-success"]',
'.success',
'[class*="success"]',
'[class*="accepted"]',
'.text-success',
'.text-green-600',
'.text-green-500'
];
for (const selector of successSelectors) {
if (element.matches(selector) || element.querySelector(selector)) {
this.handleSuccessfulSubmission();
return;
}
}
if (element.textContent && element.textContent.includes('Accepted')) {
this.handleSuccessfulSubmission();
return;
}
const successIcons = element.querySelectorAll('svg, i, span');
for (const icon of successIcons) {
if (
(typeof icon.textContent === 'string' && (icon.textContent.includes('✓') || icon.textContent.includes('✅')))
||
(typeof icon.className === 'string' && (icon.className.includes('success') || icon.className.includes('accepted')))
) {
this.handleSuccessfulSubmission();
return;
}
}
}
handleSuccessfulSubmission() {
if (!this.problemData || !this.pageEntryTime || this.hasSubmitted) return;
this.hasSubmitted = true;
const timeSpent = Date.now() - this.pageEntryTime;
const submissionData = {
...this.problemData,
timeSpent: timeSpent,
submittedAt: Date.now(),
nextReview: this.calculateNextReview(timeSpent)
};
chrome.runtime.sendMessage({
type: 'PROBLEM_SOLVED',
data: submissionData
});
console.log('LeetCode Spaced Repetition: Problem solved!', submissionData);
this.isMonitoring = false;
this.startTime = null;
const key = 'leetcodePageEntryTime_' + window.location.pathname;
sessionStorage.removeItem(key);
}
calculateNextReview(timeSpent) {
const baseInterval = 24 * 60 * 60 * 1000;
const timeFactor = Math.max(0.5, Math.min(2, timeSpent / (5 * 60 * 1000)));
const difficultyMultiplier = {
'Easy': 1.5,
'Medium': 1.0,
'Hard': 0.7
};
const multiplier = difficultyMultiplier[this.problemData.difficulty] || 1.0;
const interval = baseInterval * timeFactor * multiplier;
return Date.now() + interval;
}
getOrSetPageEntryTime() {
const key = 'leetcodePageEntryTime_' + window.location.pathname;
let entryTime = sessionStorage.getItem(key);
if (!entryTime) {
entryTime = Date.now();
sessionStorage.setItem(key, entryTime);
}
return parseInt(entryTime, 10);
}
}
const monitor = new LeetCodeMonitor();
window.testLeetReps = function() {
console.log('=== LeetReps Manual Test ===');
console.log('Current URL:', window.location.href);
console.log('Is monitoring:', monitor.isMonitoring);
console.log('Start time:', monitor.startTime);
if (monitor.isMonitoring) {
console.log('Testing success detection...');
monitor.checkForSuccessInDOM();
} else {
console.log('Starting submission monitoring...');
monitor.startSubmissionMonitoring();
}
console.log('=== End Test ===');
};
console.log('Manual test available: call testLeetReps() in console');