-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
621 lines (533 loc) · 20.8 KB
/
Copy pathcontentScript.js
File metadata and controls
621 lines (533 loc) · 20.8 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// YeetCode Content Script
// Handles UI modification, Monaco editor extraction, submission tracking, notes/timer scraping, and messaging
let isSubmitting = false;
const SAVER_SVG_URL = chrome.runtime.getURL('catcodeSmall.svg');
const YEET_BUTTON_HTML = `<img src="${SAVER_SVG_URL}" style="height:14px;width:16px;margin-right:4px" alt=""> Yeet`;
// Expose functions for testing
window.LeetCodeCodeSaver = {
getProblemInfoFromUrl: function(urlString) {
const url = new URL(urlString);
const pathParts = url.pathname.split('/');
let problemTitle = 'unknown-problem';
if (pathParts.length >= 3 && pathParts[1] === 'problems') {
problemTitle = pathParts[2];
}
const timestamp = new Date().toISOString();
return {
title: problemTitle,
timestamp: timestamp,
url: url.href
};
},
getCodeFromEditor: function() {
// Try to get code from Monaco editor
if (window.monaco && window.monaco.editor) {
const editors = window.monaco.editor.getEditors();
if (editors.length > 0) {
return editors[0].getValue();
}
}
// Fallback: try to get from textarea
const textarea = document.querySelector('textarea');
if (textarea && textarea.value) {
return textarea.value;
}
// Last resort: get from div with contenteditable
const contentEditable = document.querySelector('[contenteditable="true"]');
if (contentEditable) {
return contentEditable.innerText;
}
throw new Error('Could not find code editor');
}
};
// Listen for keyboard shortcuts to submit code (Ctrl+Enter or Cmd+Enter)
document.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
isSubmitting = true;
}
}, true);
// Wait for the DOM to be fully loaded and observe for the editor and submit button
const observer = new MutationObserver(() => {
scanPageForSaverHooks();
});
function scanPageForSaverHooks() {
try {
// 1. Inject our custom manual button if it doesn't exist
if (!document.getElementById('leetcode-saver-button')) {
const submitButton = findSubmitButton();
const editorContainer = findEditorContainer();
if (submitButton && editorContainer) {
createCustomButton(submitButton);
}
}
// 2. Attach submit button listener if not already done
const submitButton = findSubmitButton();
if (submitButton && !submitButton.dataset.hasSaveListener) {
submitButton.dataset.hasSaveListener = 'true';
submitButton.addEventListener('click', () => {
isSubmitting = true;
});
}
// 3. Monitor for submission success
if (isSubmitting) {
const isAccepted = checkSubmissionSuccess();
if (isAccepted) {
isSubmitting = false;
triggerAutoSave();
}
}
} catch (e) {
// Ignore errors during DOM observation (e.g. during test teardown)
}
}
// Start observer
if (document.body) {
observer.observe(document.body, { childList: true, subtree: true });
scanPageForSaverHooks();
} else {
document.addEventListener('DOMContentLoaded', () => {
observer.observe(document.body, { childList: true, subtree: true });
scanPageForSaverHooks();
});
}
function findSubmitButton() {
// 1. Selector for new LeetCode layout
let btn = document.querySelector('button[data-testid="judge-button"]');
if (btn) return btn;
// 2. Fallback: Selector for type="submit"
btn = document.querySelector('button[type="submit"]');
if (btn) return btn;
// 3. Fallback: Search all buttons for text content "Submit"
const buttons = document.querySelectorAll('button');
for (const button of buttons) {
if (button.textContent.trim() === 'Submit') {
return button;
}
}
return null;
}
function findEditorContainer() {
return document.querySelector('.monaco-editor') ||
document.querySelector('#editor-container') ||
document.querySelector('.editor-container-placeholder') ||
document.querySelector('[contenteditable="true"]');
}
function checkSubmissionSuccess() {
const successSelectors = [
'[data-key="submission-title"]',
'[data-e2e-locator="console-result"]',
'.success__3Ai7',
'.text-success',
'.text-green-s',
'[class*="result-success"]',
'[class*="success"]'
];
// 1. Check known selectors first
for (const selector of successSelectors) {
const el = document.querySelector(selector);
if (el && el.textContent.includes('Accepted')) {
return true;
}
}
// 2. Fallback: Search all relevant text elements for the exact word "Accepted"
const elements = document.querySelectorAll('span, div, p');
for (const el of elements) {
if (el.textContent.trim() === 'Accepted') {
return true;
}
}
return false;
}
function createCustomButton(submitButton) {
// Create our custom button
const customButton = document.createElement('button');
customButton.id = 'leetcode-saver-button';
customButton.innerHTML = YEET_BUTTON_HTML;
customButton.style.marginLeft = '8px';
customButton.style.padding = '6px 12px';
customButton.style.backgroundColor = '#d3d3d3';
customButton.style.color = '#333333';
customButton.style.border = 'none';
customButton.style.borderRadius = '4px';
customButton.style.cursor = 'pointer';
customButton.style.fontSize = '14px';
customButton.style.fontWeight = 'bold';
customButton.style.transition = 'background-color 0.2s';
customButton.style.display = 'inline-flex';
customButton.style.alignItems = 'center';
customButton.style.justifyContent = 'center';
customButton.style.alignSelf = 'center';
// Add hover effect
customButton.addEventListener('mouseover', function() {
customButton.style.backgroundColor = '#b0b0b0';
});
customButton.addEventListener('mouseout', function() {
customButton.style.backgroundColor = '#d3d3d3';
});
// Insert the button inside the outer flex container, after the submit button's wrapper div
const submitWrapper = submitButton.parentNode.parentNode;
const flexContainer = submitWrapper.parentNode;
flexContainer.insertBefore(customButton, submitWrapper.nextSibling);
// Add click event listener
customButton.addEventListener('click', async function() {
customButton.textContent = 'Saving...';
customButton.disabled = true;
try {
// Get code and language from the main page world
const { code, languageId } = await getCodeAndLanguageFromPage();
if (!code) {
throw new Error('Retrieved solution code is empty.');
}
const problemInfo = getProblemInfo();
const notesText = getNotesValue();
const timerValue = getTimerValue();
// Send save message to background script
chrome.runtime.sendMessage({
action: 'saveToGitHub',
code: code,
problemInfo: problemInfo,
languageId: languageId,
notesText: notesText,
timerValue: timerValue
}, function(response) {
customButton.disabled = false;
customButton.innerHTML = YEET_BUTTON_HTML;
if (chrome.runtime.lastError) {
console.error('Runtime error:', chrome.runtime.lastError);
showStatusMessage(`Error: ${chrome.runtime.lastError.message}`, 'error');
return;
}
if (response && response.success) {
customButton.textContent = 'Saved!';
setTimeout(() => {
customButton.innerHTML = YEET_BUTTON_HTML;
}, 2000);
showStatusMessage('Code saved to GitHub successfully!', 'success');
} else {
const errorMsg = (response && response.error) ? response.error : 'Unknown background error';
showStatusMessage(`Error: ${errorMsg}`, 'error');
}
});
} catch (error) {
console.error('Error saving to GitHub:', error);
customButton.innerHTML = YEET_BUTTON_HTML;
customButton.disabled = false;
showStatusMessage(`Error: ${error.message}`, 'error');
}
});
}
async function triggerAutoSave() {
showStatusMessage('Auto-saving solution to GitHub...', 'success');
try {
const { code, languageId } = await getCodeAndLanguageFromPage();
if (!code) {
throw new Error('Solution code is empty.');
}
const problemInfo = getProblemInfo();
const notesText = getNotesValue();
const timerValue = getTimerValue();
chrome.runtime.sendMessage({
action: 'saveToGitHub',
code: code,
problemInfo: problemInfo,
languageId: languageId,
notesText: notesText,
timerValue: timerValue
}, function(response) {
if (chrome.runtime.lastError) {
console.error('Auto-save Runtime error:', chrome.runtime.lastError);
showStatusMessage(`Auto-save Error: ${chrome.runtime.lastError.message}`, 'error');
return;
}
if (response && response.success) {
showStatusMessage('Auto-saved to GitHub successfully!', 'success');
const manualButton = document.getElementById('leetcode-saver-button');
if (manualButton) {
manualButton.textContent = 'Auto-Saved!';
setTimeout(() => {
manualButton.innerHTML = YEET_BUTTON_HTML;
}, 2000);
}
} else {
const errorMsg = (response && response.error) ? response.error : 'Unknown background error';
showStatusMessage(`Auto-save Error: ${errorMsg}`, 'error');
}
});
} catch (error) {
console.error('Auto-save Error:', error);
showStatusMessage(`Auto-save Error: ${error.message}`, 'error');
}
}
// Injects script to retrieve Monaco Editor value from page's MAIN world context
async function getCodeAndLanguageFromPage() {
return new Promise((resolve, reject) => {
let settled = false;
let script = null;
const cleanup = () => {
document.removeEventListener('LeetCodeCodeSaver_CodeExtracted', handleEvent);
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
};
const resolveFromDomFallback = (errorMessage) => {
try {
const code = window.LeetCodeCodeSaver.getCodeFromEditor();
resolve({ code, languageId: 'txt' });
} catch (e) {
reject(new Error(errorMessage));
}
};
// Set timeout in case injection or event does not respond
const timeoutId = setTimeout(() => {
if (settled) return;
settled = true;
cleanup();
resolveFromDomFallback('Retrieval timed out: Unable to read editor.');
}, 1000);
const handleEvent = (event) => {
if (settled) return;
settled = true;
clearTimeout(timeoutId);
cleanup();
if (event.detail && event.detail.code !== null) {
resolve({
code: event.detail.code,
languageId: event.detail.languageId || 'txt'
});
} else {
resolveFromDomFallback('Editor is empty or could not be read.');
}
};
document.addEventListener('LeetCodeCodeSaver_CodeExtracted', handleEvent);
// Load an extension-hosted bridge script into the page context.
// Inline script injection is blocked by LeetCode's CSP.
script = document.createElement('script');
script.src = chrome.runtime.getURL('pageBridge.js');
script.onload = () => {
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
};
script.onerror = () => {
if (settled) return;
settled = true;
clearTimeout(timeoutId);
cleanup();
resolveFromDomFallback('Unable to load editor bridge script.');
};
(document.head || document.documentElement).appendChild(script);
});
}
function getProblemInfo() {
const url = new URL(window.location.href);
const pathParts = url.pathname.split('/');
let problemTitle = 'unknown-problem';
if (pathParts.length >= 3 && pathParts[1] === 'problems') {
problemTitle = pathParts[2];
}
const titleElement = document.querySelector('.title') ||
document.querySelector('h1') ||
document.querySelector('[data-cy="problem-title"]');
if (titleElement && titleElement.textContent.trim()) {
problemTitle = titleElement.textContent.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
const timestamp = new Date().toISOString();
return {
title: problemTitle,
timestamp: timestamp,
url: url.href
};
}
function getNotesValue() {
const noteSelectors = [
'[aria-label*="note" i]',
'[aria-labelledby*="note" i]',
'[role="textbox"][aria-label*="note" i]',
'[contenteditable="true"][aria-label*="note" i]',
'textarea[placeholder*="note" i]',
'textarea[placeholder*="Write a note" i]',
'input[placeholder*="note" i]',
'div[data-placeholder*="note" i]',
'div[data-placeholder*="Write a note" i]',
'.EasyMDEContainer .CodeMirror-code',
'.EasyMDEContainer .CodeMirror',
'.CodeMirror-code',
'.ProseMirror',
'.ql-editor',
'.note-editor textarea',
'#note-editor textarea',
'[class*="note-editor"] textarea',
'[class*="note-content"]',
'[class*="note-editor"] [contenteditable="true"]',
'[class*="notes"] [contenteditable="true"]',
'[class*="note"] [role="textbox"]',
'.qd-notes-textarea',
'[data-testid="notes-editor"] textarea',
'[data-testid="notes-editor"] [contenteditable="true"]'
];
for (const selector of noteSelectors) {
const elements = document.querySelectorAll(selector);
for (const el of elements) {
const val = getElementTextValue(el);
if (isLikelyUserNote(val)) return val;
}
}
const noteContainers = document.querySelectorAll('[class*="note" i], [id*="note" i], [data-testid*="note" i]');
for (const container of noteContainers) {
const noteFields = container.querySelectorAll('[contenteditable="true"], [role="textbox"], textarea, input, .ProseMirror, .ql-editor');
for (const field of noteFields) {
const val = getElementTextValue(field);
if (isLikelyUserNote(val)) return val;
}
const containerText = getElementTextValue(container);
if (isLikelyUserNote(containerText)) return containerText;
}
return getNotesValueFromStorage();
}
function getElementTextValue(el) {
if (!el) return '';
const tagName = el.tagName ? el.tagName.toUpperCase() : '';
if (tagName === 'TEXTAREA' || tagName === 'INPUT') {
return (el.value || '').trim();
}
if (el.classList && (el.classList.contains('CodeMirror') || el.classList.contains('CodeMirror-code'))) {
return getCodeMirrorTextValue(el);
}
const text = (el.innerText || el.textContent || '').trim();
return text.replace(/\n{3,}/g, '\n\n');
}
function getCodeMirrorTextValue(el) {
const root = el.classList.contains('CodeMirror-code') ? el : el.querySelector('.CodeMirror-code');
if (!root) return '';
const lines = Array.from(root.querySelectorAll('.CodeMirror-line'))
.map(line => (line.innerText || line.textContent || '').replace(/\u00a0/g, ' ').trim());
return lines.join('\n').trim();
}
function isLikelyUserNote(value) {
if (!value) return false;
const normalized = value.trim();
if (!normalized) return false;
const ignoredValues = [
'notes',
'note',
'write a note',
'write notes',
'add note',
'add notes'
];
return !ignoredValues.includes(normalized.toLowerCase());
}
function getNotesValueFromStorage() {
try {
const candidates = [];
for (let i = 0; i < window.localStorage.length; i += 1) {
const key = window.localStorage.key(i);
if (!key || !/notes?|notepad|notebook/i.test(key)) continue;
const value = window.localStorage.getItem(key);
collectNoteCandidates(value, candidates);
}
candidates.sort((a, b) => b.length - a.length);
return candidates[0] || null;
} catch (e) {
return null;
}
}
function collectNoteCandidates(value, candidates) {
if (value === null || value === undefined) return;
if (typeof value === 'string') {
const trimmed = value.trim();
if (!trimmed) return;
try {
collectNoteCandidates(JSON.parse(trimmed), candidates);
return;
} catch (e) {
if (isLikelyUserNote(trimmed)) {
candidates.push(trimmed);
}
return;
}
}
if (Array.isArray(value)) {
value.forEach(item => collectNoteCandidates(item, candidates));
return;
}
if (typeof value === 'object') {
Object.values(value).forEach(item => collectNoteCandidates(item, candidates));
}
}
function getTimerValue() {
const timerSelectors = [
'[class*="timer" i]',
'[id*="timer" i]',
'[class*="clock" i]',
'[id*="clock" i]',
'[data-testid*="timer" i]'
];
for (const selector of timerSelectors) {
const elements = document.querySelectorAll(selector);
for (const el of elements) {
const text = el.textContent.trim();
if (/^\d{2}:\d{2}(:\d{2})?$/.test(text)) {
return text;
}
}
}
const allTextElements = document.querySelectorAll('span, div, p, button');
for (const el of allTextElements) {
const text = el.textContent.trim();
if (/^\d{2}:\d{2}(:\d{2})?$/.test(text)) {
return text;
}
}
return null;
}
function showStatusMessage(message, type) {
const existingStatus = document.querySelector('#leetcode-saver-status');
if (existingStatus) {
existingStatus.remove();
}
const statusDiv = document.createElement('div');
statusDiv.id = 'leetcode-saver-status';
statusDiv.textContent = message;
statusDiv.style.position = 'fixed';
statusDiv.style.top = '20px';
statusDiv.style.right = '20px';
statusDiv.style.padding = '12px 24px';
statusDiv.style.borderRadius = '6px';
statusDiv.style.fontSize = '14px';
statusDiv.style.fontWeight = '500';
statusDiv.style.zIndex = '99999';
statusDiv.style.boxShadow = '0 4px 12px rgba(0,0,0,0.15)';
statusDiv.style.fontFamily = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif';
statusDiv.style.transition = 'all 0.3s ease';
if (type === 'success') {
statusDiv.style.backgroundColor = '#ecfdf5';
statusDiv.style.color = '#065f46';
statusDiv.style.border = '1px solid #a7f3d0';
} else {
statusDiv.style.backgroundColor = '#fef2f2';
statusDiv.style.color = '#991b1b';
statusDiv.style.border = '1px solid #fca5a5';
}
document.body.appendChild(statusDiv);
setTimeout(() => {
statusDiv.style.opacity = '0';
statusDiv.style.transform = 'translateY(-10px)';
setTimeout(() => {
if (statusDiv.parentNode) {
statusDiv.parentNode.removeChild(statusDiv);
}
}, 300);
}, 4000);
}
Object.assign(window.LeetCodeCodeSaver, {
findSubmitButton,
checkSubmissionSuccess,
getCodeAndLanguageFromPage,
getNotesValue,
getTimerValue,
scanPageForSaverHooks
});