-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcontent-script.js
More file actions
1153 lines (1007 loc) · 39.2 KB
/
Copy pathcontent-script.js
File metadata and controls
1153 lines (1007 loc) · 39.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;(() => {
const BASE_URL = 'https://api.github.com/repos'
const FILE_EXTENSIONS = {
C: '.c',
'C++': '.cpp',
'C#': '.cs',
Dart: '.dart',
Elixir: '.ex',
Erlang: '.erl',
Go: '.go',
Java: '.java',
JavaScript: '.js',
Kotlin: '.kt',
PHP: '.php',
Python: '.py',
Python3: '.py',
Racket: '.rkt',
Ruby: '.rb',
Rust: '.rs',
Scala: '.scala',
Swift: '.swift',
TypeScript: '.ts',
MySQL: '.sql',
PostgreSQL: '.sql',
Oracle: '.sql',
'MS SQL Server': '.tsql',
Pandas: '.py',
}
const LOCAL_STORAGE_KEYS = {
C: 'c',
'C++': 'cpp',
'C#': 'csharp',
Dart: 'dart',
Elixir: 'elixir',
Erlang: 'erlang',
Go: 'golang',
Java: 'java',
JavaScript: 'javascript',
Kotlin: 'kotlin',
PHP: 'php',
Python: 'python',
Python3: 'python3',
Racket: 'racket',
Ruby: 'ruby',
Rust: 'rust',
Scala: 'scala',
Swift: 'swift',
TypeScript: 'typeScript',
MySQL: 'mysql',
Oracle: 'oraclesql',
PostgreSQL: 'postgresql',
'MS SQL Server': 'mssql',
Pandas: 'pythondata',
}
const DATABASE_LANGUAGES = ['MySQL', 'Oracle', 'PostgreSQL', 'MS SQL Server', 'Pandas']
const isMac = /Mac|iPod|iPhone|iPad/.test(navigator.platform)
const DEFAULT_SHORTCUT = isMac ? { key: 'p', modifier: 'meta' } : { key: 'p', modifier: 'ctrl' }
const getShortcutDisplayText = (shortcut) => {
const sym =
shortcut.modifier === 'meta'
? '⌘'
: shortcut.modifier === 'alt'
? '⌥'
: shortcut.modifier === 'shift'
? '⇧'
: shortcut.modifier === 'ctrl'
? 'Ctrl+'
: ''
return `${sym}${shortcut.key.toUpperCase()}`
}
const getKeyboardShortcut = () => {
const saved = localStorage.getItem('keyboard-shortcut')
if (saved) {
try {
return JSON.parse(saved)
} catch {
return DEFAULT_SHORTCUT
}
}
return DEFAULT_SHORTCUT
}
let KEYBOARD_SHORTCUT = getKeyboardShortcut()
let SHORTCUT_DISPLAY = getShortcutDisplayText(KEYBOARD_SHORTCUT)
// Fallback-only DOM selectors — only used when API/localStorage approach fails
const FALLBACK_SELECTORS = {
parentDiv:
'div.flex.justify-between.py-1.pl-3.pr-1 > div.relative.flex.overflow-hidden.rounded.bg-fill-tertiary.dark\\:bg-fill-tertiary.\\!bg-transparent > div.flex-none.flex > div:nth-child(2)',
parentDivCodeEditor: '#ide-top-btns > div > div',
performanceMetrics:
'div.flex.items-center.justify-between.gap-2 > div > div.rounded-sd.flex.min-w-\\[275px\\].flex-1.cursor-pointer.flex-col.px-4.py-3.text-xs > div:nth-child(2) > span.font-semibold',
}
// ─── Toast System ─────────────────────────────────────────────────────────
function injectToastStyles() {
if (document.getElementById('lp-toast-styles')) return
const style = document.createElement('style')
style.id = 'lp-toast-styles'
style.textContent = `
#lp-toaster {
position: fixed;
top: 1rem;
left: 50%;
transform: translateX(-50%);
z-index: 9999999;
display: flex;
flex-direction: column;
gap: 0.5rem;
pointer-events: none;
width: 356px;
max-width: calc(100vw - 2rem);
}
.lp-toast {
pointer-events: all;
display: flex;
align-items: flex-start;
gap: 0.625rem;
padding: 0.875rem 1rem;
border-radius: 0.625rem;
font-size: 13px;
font-weight: 500;
line-height: 1.45;
background: #18181b;
border: 1px solid #3f3f46;
color: #fafafa;
box-shadow: 0 4px 6px -1px rgba(0,0,0,0.4), 0 10px 20px -4px rgba(0,0,0,0.5);
animation: lp-toast-in 0.35s cubic-bezier(0.21,1.02,0.73,1) forwards;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
.lp-toast-icon {
flex-shrink: 0;
width: 16px;
height: 16px;
margin-top: 1px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
font-size: 10px;
font-weight: 700;
}
.lp-toast-error .lp-toast-icon { background: #ef4444; color: #fff; }
.lp-toast-success .lp-toast-icon { background: #22c55e; color: #fff; }
.lp-toast-info .lp-toast-icon { background: #3b82f6; color: #fff; }
.lp-toast-message { flex: 1; }
.lp-toast-close {
background: none;
border: none;
color: #71717a;
cursor: pointer;
font-size: 13px;
padding: 0;
flex-shrink: 0;
line-height: 1;
transition: color 0.15s;
margin-top: 1px;
}
.lp-toast-close:hover { color: #fafafa; }
.lp-toast-out {
animation: lp-toast-out 0.25s ease forwards !important;
}
@keyframes lp-toast-in {
from { opacity: 0; transform: translateY(-12px) scale(0.95); }
to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes lp-toast-out {
from { opacity: 1; transform: translateY(0) scale(1); max-height: 80px; margin-bottom: 0; }
to { opacity: 0; transform: translateY(-8px) scale(0.95); max-height: 0; margin-bottom: -0.5rem; }
}
`
document.head.appendChild(style)
}
function ensureToaster() {
let toaster = document.getElementById('lp-toaster')
if (!toaster) {
toaster = document.createElement('div')
toaster.id = 'lp-toaster'
document.body.appendChild(toaster)
}
return toaster
}
function showToast(message, type = 'error') {
injectToastStyles()
const toaster = ensureToaster()
const toast = document.createElement('div')
toast.className = `lp-toast lp-toast-${type}`
const iconText = type === 'error' ? '✕' : type === 'success' ? '✓' : 'i'
toast.innerHTML = `
<span class="lp-toast-icon">${iconText}</span>
<span class="lp-toast-message">${message}</span>
<button class="lp-toast-close" aria-label="Close">✕</button>
`
toast.querySelector('.lp-toast-close').addEventListener('click', () => dismissToast(toast))
toaster.appendChild(toast)
const timer = setTimeout(() => dismissToast(toast), 4500)
toast._lpTimer = timer
}
function dismissToast(toast) {
if (!toast.isConnected) return
clearTimeout(toast._lpTimer)
toast.classList.add('lp-toast-out')
toast.addEventListener('animationend', () => toast.remove(), { once: true })
}
// ─── Problem Resolution: URL + GraphQL + localStorage ─────────────────────
function getProblemSlugFromURL() {
const match = window.location.pathname.match(/\/problems\/([^/]+)\//)
return match?.[1] ?? null
}
async function getProblemMetaFromGraphQL(titleSlug) {
try {
const res = await fetch('https://leetcode.com/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `{ question(titleSlug: "${titleSlug}") { questionFrontendId title } }`,
}),
})
const data = await res.json()
return data?.data?.question ?? null
} catch {
return null
}
}
async function fetchFullProblemData(titleSlug) {
try {
const res = await fetch('https://leetcode.com/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `{
question(titleSlug: "${titleSlug}") {
questionFrontendId
title
difficulty
content
topicTags { name }
}
}`,
}),
})
const data = await res.json()
return data?.data?.question ?? null
} catch {
return null
}
}
function buildReadme(problemData, solutionFileName) {
const { questionFrontendId, title, difficulty, content, topicTags } = problemData
const slug = title.toLowerCase().replaceAll(' ', '-')
const url = `https://leetcode.com/problems/${slug}/`
const tags = topicTags?.map((t) => t.name).join(', ') || 'N/A'
const diffEmoji = difficulty === 'Easy' ? '🟢' : difficulty === 'Medium' ? '🟡' : '🔴'
return `# ${questionFrontendId}. ${title}
${diffEmoji} **${difficulty}** | [View on LeetCode](${url})
**Topics:** ${tags}
---
${content || ''}
---
**My Solution:** [${solutionFileName}](./${solutionFileName})
`
}
// Scan all buttons for a known language name — resilient to LeetCode DOM changes
function detectLanguage() {
const buttons = document.querySelectorAll('button')
for (const btn of buttons) {
const text = btn.textContent?.trim()
if (text && FILE_EXTENSIONS[text]) return text
}
return null
}
function getSolutionFromLocalStorage(probNum) {
const allKeys = []
for (let i = 0; i < localStorage.length; i++) {
allKeys.push(localStorage.key(i))
}
for (const [lang, langKey] of Object.entries(LOCAL_STORAGE_KEYS)) {
const match = allKeys.find((k) => {
const parts = k.split('_')
return parts[0] === probNum && parts[parts.length - 1] === langKey
})
if (match) {
const raw = localStorage.getItem(match)
if (raw) {
return {
lang,
solution: raw.replace(/\\n/g, '\n').replace(/ {2}/g, ' ').replace(/"/g, ''),
}
}
}
}
return null
}
function findPercentValues() {
const pctPattern = /^\d+\.?\d*%$/
const results = []
document.querySelectorAll('span').forEach((el) => {
const text = el.textContent?.trim() ?? ''
if (pctPattern.test(text)) results.push(text)
})
return results
}
function extractPerformanceMetrics(language) {
// Try specific selector first
const specific = document.querySelectorAll(FALLBACK_SELECTORS.performanceMetrics)
if (DATABASE_LANGUAGES.includes(language)) {
if (specific.length >= 2) {
return { runtime: specific[1]?.textContent?.trim() || 'N/A', memory: null }
}
const pct = findPercentValues()
return { runtime: pct[0] || 'N/A', memory: null }
}
if (specific.length >= 4) {
return {
runtime: specific[1]?.textContent?.trim() || 'N/A',
memory: specific[3]?.textContent?.trim() || 'N/A',
}
}
// Fallback: locate % values in the page
const pct = findPercentValues()
return { runtime: pct[0] || 'N/A', memory: pct[1] || 'N/A' }
}
async function extractProblemInfo() {
try {
const titleSlug = getProblemSlugFromURL()
if (!titleSlug) {
console.error('LeetPush: could not extract problem slug from URL')
return null
}
const meta = await getProblemMetaFromGraphQL(titleSlug)
if (!meta) {
console.error('LeetPush: GraphQL returned no data for slug:', titleSlug)
return null
}
const probNum = meta.questionFrontendId
const probName = meta.title.replaceAll(' ', '-')
const language = detectLanguage()
if (!language || !FILE_EXTENSIONS[language]) {
console.error('LeetPush: could not detect solution language')
return null
}
const fullFileName = `${probNum}-${probName}${FILE_EXTENSIONS[language]}`
// Prefer localStorage; fall back to DOM code block
let solution = null
const stored = getSolutionFromLocalStorage(probNum)
if (stored) {
solution = stored.solution
} else {
const codeEl = document.querySelector('div.px-4.py-3 > div > pre > code')
if (codeEl) {
solution = Array.from(codeEl.children).map((line) =>
Array.from(line.childNodes).slice(1).map((n) => n.textContent).join('')
).join('').replace(/ /g, ' ') || codeEl.textContent || ''
}
}
if (!solution) {
console.error('LeetPush: solution not found in localStorage or DOM')
return null
}
const metrics = extractPerformanceMetrics(language)
const commitMsg =
metrics.memory !== null
? `[${probNum}] [Time Beats: ${metrics.runtime}] [Memory Beats: ${metrics.memory}] - LeetPush`
: `[${probNum}] [Time Beats: ${metrics.runtime}] - LeetPush`
sessionStorage.setItem('fileName', fullFileName)
sessionStorage.setItem('solution', solution)
sessionStorage.setItem('commitMsg', commitMsg)
return { probNum, probName, fileName: fullFileName, solution, commitMsg, language }
} catch (error) {
console.error('LeetPush: error extracting problem info:', error)
return null
}
}
// ─── Submission Detection ─────────────────────────────────────────────────
function isSubmissionPage() {
return window.location.pathname.includes('/submissions/')
}
function hasAcceptedSolution() {
// Text-based scan — resilient to class name changes
const candidates = document.querySelectorAll('span, div, h4, p')
return Array.from(candidates).some(
(el) => el.childElementCount === 0 && el.textContent?.trim() === 'Accepted',
)
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
// ─── Keyboard Shortcut ────────────────────────────────────────────────────
function registerKeyboardShortcut() {
document.addEventListener('keydown', (event) => {
const modMatch =
(KEYBOARD_SHORTCUT.modifier === 'meta' && event.metaKey) ||
(KEYBOARD_SHORTCUT.modifier === 'alt' && event.altKey) ||
(KEYBOARD_SHORTCUT.modifier === 'shift' && event.shiftKey) ||
(KEYBOARD_SHORTCUT.modifier === 'ctrl' && event.ctrlKey)
if (modMatch && event.key.toLowerCase() === KEYBOARD_SHORTCUT.key.toLowerCase()) {
event.preventDefault()
handlePushClick()
}
})
}
// ─── Button Injection ─────────────────────────────────────────────────────
function findParent(selectors) {
for (const sel of selectors) {
const el = document.querySelector(sel)
if (el) return el
}
return null
}
function injectButtons() {
const parentDiv = findParent([FALLBACK_SELECTORS.parentDiv])
const parentDivCodeEditor = findParent([FALLBACK_SELECTORS.parentDivCodeEditor])
if (parentDiv) {
injectButtonsToParent(
parentDiv,
'leetpush-div-edit',
'leetpush-btn-edit',
'Edit',
'leetpush-div',
'leetpush-btn',
`Push (${SHORTCUT_DISPLAY})`,
false,
)
}
if (parentDivCodeEditor) {
injectButtonsToParent(
parentDivCodeEditor,
'leetpush-div-edit-CodeEditor',
'leetpush-btn-edit-CodeEditor',
'Edit',
'leetpush-div-CodeEditor',
'leetpush-btn-CodeEditor',
`Push (${SHORTCUT_DISPLAY})`,
true,
)
}
}
function injectButtonsToParent(
parent,
editContainerId,
editButtonId,
editText,
pushContainerId,
pushButtonId,
pushText,
isCodeEditor,
) {
if (document.getElementById(editContainerId) || document.getElementById(pushContainerId)) return
const editButton = createButton(editContainerId, editButtonId, editText, () => {
localStorage.removeItem('branch')
handlePushClick()
})
const pushButton = createButton(pushContainerId, pushButtonId, pushText, handlePushClick)
parent.appendChild(editButton)
parent.appendChild(pushButton)
}
function createButton(containerId, buttonId, text, clickHandler) {
const container = document.createElement('div')
container.id = containerId
const button = document.createElement('button')
button.id = buttonId
button.textContent = text
container.addEventListener('click', () => {
if (!button.disabled) clickHandler()
})
container.appendChild(button)
return container
}
function updateButtonLabels() {
const btns = [
document.querySelector('#leetpush-btn'),
document.querySelector('#leetpush-btn-CodeEditor'),
]
btns.forEach((btn) => {
if (btn) btn.textContent = `Push (${SHORTCUT_DISPLAY})`
})
}
// ─── Config Modal ─────────────────────────────────────────────────────────
function createConfigModal() {
const modal = document.createElement('div')
modal.id = 'lp-modal'
modal.innerHTML = `
<div id="lp-container">
<div id="lp-close-btn"><button aria-label="Close">✕</button></div>
<h3>Leet<span>Push</span></h3>
<form id="lp-form">
<div class="lp-div">
<label>Repository URL:</label>
<input type="text" id="repo-url" name="repo-url" placeholder="https://github.com/username/repository" required>
</div>
<div class="lp-div">
<label>Token: <a href="https://scribehow.com/shared/Generating_a_personal_access_token_on_GitHub__PUPxxuxIRQmlg1MUE-2zig" target="_blank">Generate Token?</a></label>
<input type="text" id="token" name="token" placeholder="github_pat_..." required>
</div>
<div class="lp-div">
<label>Target directory push:</label>
<input type="text" id="custom-dir" name="custom-dir" placeholder="Leave empty to push to the root.">
</div>
<div class="lp-push-mode">
<label>Push mode:</label>
<div id="lp-push-mode-radios">
<div class="radio-div">
<input type="radio" id="push-mode-single" name="push-mode" value="single" checked>
<label for="push-mode-single">Single file</label>
</div>
<div class="radio-div">
<input type="radio" id="push-mode-folder" name="push-mode" value="folder">
<label for="push-mode-folder">Folder per problem</label>
</div>
</div>
<p class="lp-hint">Folder mode pushes <code>{id}-{slug}/solution + README.md</code> in one commit.</p>
</div>
<div class="lp-keyboard-shortcut">
<label>Keyboard shortcut:</label>
<div class="shortcut-config">
<select id="shortcut-modifier">
<option value="meta">${isMac ? '⌘ Command' : '⊞ Windows'}</option>
<option value="ctrl">Ctrl</option>
<option value="alt">${isMac ? '⌥ Option' : 'Alt'}</option>
<option value="shift">⇧ Shift</option>
</select>
<span>+</span>
<input type="text" id="shortcut-key" maxlength="1" placeholder="Key">
</div>
</div>
<div class="lp-daily-challenge">
<label>Daily problems on a separate folder:</label>
<div id="lp-radios">
<div class="radio-div">
<input type="radio" id="separate-folder-yes" name="daily-challenge" value="yes">
<label for="separate-folder-yes">yes</label>
</div>
<div class="radio-div">
<input type="radio" id="separate-folder-no" name="daily-challenge" value="no" checked>
<label for="separate-folder-no">no</label>
</div>
</div>
</div>
<div>
<label>Repository branch:</label>
<div id="lp-radios">
<div class="radio-div">
<input type="radio" id="branch-master" name="branch-name" value="master" checked>
<label for="branch-master">master</label>
</div>
<div class="radio-div">
<input type="radio" id="branch-main" name="branch-name" value="main">
<label for="branch-main">main</label>
</div>
</div>
</div>
<button id="lp-submit-btn" type="submit">Submit</button>
</form>
</div>
`
modal.querySelector('#lp-close-btn button')?.addEventListener('click', () => {
document.body.removeChild(modal)
})
modal.addEventListener('click', (event) => {
if (event.target === modal) document.body.removeChild(modal)
})
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && document.body.contains(modal)) {
document.body.removeChild(modal)
}
})
modal.querySelector('#lp-form')?.addEventListener('submit', async (event) => {
event.preventDefault()
await saveConfig(modal)
})
return modal
}
async function saveConfig(modal) {
const repoUrlInput = modal.querySelector('#repo-url')
const tokenInput = modal.querySelector('#token')
const branchInput = modal.querySelector('input[name="branch-name"]:checked')
const separateFolderInput = modal.querySelector('input[name="daily-challenge"]:checked')
const customDirInput = modal.querySelector('#custom-dir')
const shortcutModifierInput = modal.querySelector('#shortcut-modifier')
const shortcutKeyInput = modal.querySelector('#shortcut-key')
if (!repoUrlInput || !tokenInput || !branchInput || !separateFolderInput) return
const githubUrlPattern = /^https:\/\/github\.com\/[\w-]+\/[\w.-]+$/
if (!githubUrlPattern.test(repoUrlInput.value)) {
showToast('Please enter a valid GitHub repository URL (https://github.com/username/repository).', 'error')
return
}
if (!tokenInput.value.startsWith('ghp_') && !tokenInput.value.startsWith('github_pat_')) {
showToast('Please enter a valid GitHub token. It should start with "ghp_" or "github_pat_".', 'error')
return
}
const repoUrl = repoUrlInput.value.endsWith('.git')
? repoUrlInput.value.slice(0, -4)
: repoUrlInput.value
if (shortcutModifierInput && shortcutKeyInput && shortcutKeyInput.value) {
KEYBOARD_SHORTCUT = {
key: shortcutKeyInput.value.toLowerCase(),
modifier: shortcutModifierInput.value,
}
SHORTCUT_DISPLAY = getShortcutDisplayText(KEYBOARD_SHORTCUT)
localStorage.setItem('keyboard-shortcut', JSON.stringify(KEYBOARD_SHORTCUT))
updateButtonLabels()
}
const pushModeInput = modal.querySelector('input[name="push-mode"]:checked')
localStorage.setItem('repo', repoUrl)
localStorage.setItem('token', tokenInput.value)
localStorage.setItem('branch', branchInput.value)
localStorage.setItem('separate-folder', separateFolderInput.value)
localStorage.setItem('custom-dir', customDirInput?.value || '')
localStorage.setItem('push-mode', pushModeInput?.value || 'single')
document.body.removeChild(modal)
try {
await updateRepoDescription(tokenInput.value, repoUrl, branchInput.value)
} catch (error) {
console.error('LeetPush: error setting up repository metadata:', error)
showToast('Initial repository setup failed. You may need to check your repository permissions.', 'error')
}
}
function showConfigModal() {
const modal = createConfigModal()
const token = localStorage.getItem('token')
const repo = localStorage.getItem('repo')
const branch = localStorage.getItem('branch')
const separateFolder = localStorage.getItem('separate-folder')
const customDir = localStorage.getItem('custom-dir')
const shortcutModifierInput = modal.querySelector('#shortcut-modifier')
const shortcutKeyInput = modal.querySelector('#shortcut-key')
if (shortcutModifierInput) shortcutModifierInput.value = KEYBOARD_SHORTCUT.modifier
if (shortcutKeyInput) shortcutKeyInput.value = KEYBOARD_SHORTCUT.key.toUpperCase()
const pushMode = localStorage.getItem('push-mode')
if (token) modal.querySelector('#token').value = token
if (repo) modal.querySelector('#repo-url').value = repo
if (branch) modal.querySelector(`#branch-${branch}`).checked = true
if (separateFolder) modal.querySelector(`#separate-folder-${separateFolder}`).checked = true
if (customDir) modal.querySelector('#custom-dir').value = customDir
if (pushMode) modal.querySelector(`#push-mode-${pushMode}`).checked = true
document.body.appendChild(modal)
}
// ─── GitHub API ───────────────────────────────────────────────────────────
function getGithubConfig() {
return {
token: localStorage.getItem('token') || '',
repo: localStorage.getItem('repo') || '',
branch: localStorage.getItem('branch') || '',
separateFolder: localStorage.getItem('separate-folder') || '',
customDir: localStorage.getItem('custom-dir') || '',
pushMode: localStorage.getItem('push-mode') || 'single',
}
}
function isConfigComplete(config) {
return !!(config.token && config.repo && config.branch && config.separateFolder !== null)
}
function getPushButton() {
return document.querySelector('#leetpush-btn') || document.querySelector('#leetpush-btn-CodeEditor')
}
async function handlePushClick() {
const config = getGithubConfig()
if (!isConfigComplete(config)) {
showConfigModal()
return
}
const pushBtn = getPushButton()
if (!pushBtn) {
showToast('Interface error: Push button not found. Please refresh the page and try again.', 'error')
return
}
const problemInfo = await extractProblemInfo()
if (!problemInfo) {
showToast(
'Failed to extract problem info. Make sure you are on an accepted submission page.',
'error',
)
return
}
const { fileName, solution, commitMsg } = problemInfo
if (!fileName || !solution || !commitMsg) {
if (!fileName) showToast('Failed to generate a valid file name for your solution.', 'error')
else if (!solution) showToast('Failed to extract your solution code. Please try again.', 'error')
else showToast('Failed to generate commit message. Please try again.', 'error')
return
}
const [userName, repoName] = config.repo.split('/').slice(3, 5)
if (!userName || !repoName) {
showToast(
'Invalid repository URL format. Please check your settings.',
'error',
)
showConfigModal()
return
}
pushBtn.disabled = true
pushBtn.textContent = 'Loading...'
pushBtn.classList.add('loading')
// Fetch full problem data only when folder mode is on
let fullProblemData = null
if (config.pushMode === 'folder') {
const titleSlug = getProblemSlugFromURL()
if (titleSlug) fullProblemData = await fetchFullProblemData(titleSlug)
if (!fullProblemData) {
showToast('Could not fetch problem description. Falling back to single-file mode.', 'info')
}
}
try {
await pushToGithub(
userName,
repoName,
config.branch,
fileName,
solution,
commitMsg,
config.token,
config.separateFolder,
config.customDir,
config.pushMode,
problemInfo,
fullProblemData,
)
pushBtn.classList.remove('loading')
pushBtn.classList.add('success')
pushBtn.textContent = 'Done'
showToast('Solution pushed to GitHub!', 'success')
const solutionsPushed = Number.parseInt(localStorage.getItem('solutions-pushed') || '0') + 1
localStorage.setItem('solutions-pushed', solutionsPushed.toString())
try {
const [, dailyProblemNum] = await getDailyChallenge()
if (dailyProblemNum === problemInfo.probNum) {
const dailyChallenges = Number.parseInt(localStorage.getItem('daily-challenges') || '0') + 1
localStorage.setItem('daily-challenges', dailyChallenges.toString())
}
} catch (error) {
console.error('LeetPush: error checking daily challenge:', error)
}
await sleep(2000)
pushBtn.disabled = false
pushBtn.classList.remove('success')
pushBtn.textContent = `Push (${SHORTCUT_DISPLAY})`
} catch (error) {
console.error('LeetPush: failed to push solution:', error)
const errorMessage = error.message || ''
const isAuthError =
errorMessage.includes('401') ||
errorMessage.includes('Authentication failed') ||
errorMessage.includes('invalid or expired')
if (isAuthError) {
localStorage.removeItem('token')
localStorage.removeItem('branch')
showToast('Authentication failed. Your GitHub token is invalid or expired. Please enter a new token.', 'error')
pushBtn.classList.remove('loading')
pushBtn.disabled = false
pushBtn.textContent = `Push (${SHORTCUT_DISPLAY})`
showConfigModal()
} else {
showToast(errorMessage || 'Unknown error occurred while pushing solution.', 'error')
pushBtn.classList.remove('loading')
pushBtn.classList.add('error')
pushBtn.textContent = 'Error'
await sleep(2000)
pushBtn.disabled = false
pushBtn.classList.remove('error')
pushBtn.textContent = `Push (${SHORTCUT_DISPLAY})`
}
}
}
async function resolveDailyPrefix(problemInfo, separateFolder) {
if (separateFolder !== 'yes') return ''
try {
const [date, dailyProblemNum] = await getDailyChallenge()
if (problemInfo && dailyProblemNum === problemInfo.probNum) {
const splitDate = date.split('-')
return `DCP-${splitDate[1]}-${splitDate[0].slice(2)}`
}
} catch (error) {
console.error('LeetPush: error checking daily challenge folder:', error)
}
return ''
}
async function pushToGithub(
userName,
repoName,
branch,
fileName,
content,
commitMsg,
token,
separateFolder,
customDir,
pushMode,
problemInfo,
fullProblemData,
) {
if (!fileName?.trim()) throw new Error('Invalid file name. Please try again.')
if (!content?.trim()) throw new Error('No solution content found. Please make sure your solution is visible on the page.')
const dailyPrefix = await resolveDailyPrefix(problemInfo, separateFolder)
if (pushMode === 'folder' && fullProblemData) {
// Build {id}-{slug} folder name
const folderName = `${problemInfo.probNum}-${problemInfo.probName.toLowerCase()}`
// Assemble path: [customDir/][dailyPrefix/]{id}-{slug}
const parts = [customDir, dailyPrefix, folderName].filter(Boolean)
const folderPath = parts.join('/')
const readme = buildReadme(fullProblemData, fileName)
return await pushFolderToRepo(userName, repoName, folderPath, branch, readme, content, fileName, commitMsg, token)
}
// Single-file mode
const parts = [customDir, dailyPrefix, fileName].filter(Boolean)
const filePath = parts.join('/')
if (!filePath?.trim()) throw new Error('Failed to generate a valid file path. Please check your target directory settings.')
return await pushFileToRepo(userName, repoName, filePath, branch, content, commitMsg, token)
}
async function pushFileToRepo(userName, repoName, filePath, branch, content, commitMsg, token) {
const apiUrl = `${BASE_URL}/${userName}/${repoName}/contents/${filePath}`
const repoCheckResponse = await fetch(`${BASE_URL}/${userName}/${repoName}`, {
headers: { Authorization: `Bearer ${token}` },
})
if (!repoCheckResponse.ok) {
const errorData = await repoCheckResponse.json()
if (repoCheckResponse.status === 404) {
throw new Error(`Repository not found: ${userName}/${repoName}. Please check if the repository exists and your token has access to it.`)
} else if (repoCheckResponse.status === 401) {
throw new Error('Authentication failed. Your GitHub token may be invalid or expired.')
} else if (repoCheckResponse.status === 403) {
throw new Error('Access forbidden. Your token may not have sufficient permissions.')
} else {
throw new Error(`Repository access error: ${errorData.message || 'Unknown error'}`)
}
}
const encodedContent = btoa(unescape(encodeURIComponent(content)))
const requestBody = { message: commitMsg, content: encodedContent, branch }
let fileExistsRes
try {
fileExistsRes = await fetch(`${apiUrl}?ref=${branch}`, {
headers: { Authorization: `Bearer ${token}` },
})
} catch {
throw new Error('Network error while checking if file already exists in repository.')
}
if (fileExistsRes.ok) {
const existingFileData = await fileExistsRes.json()
if (existingFileData?.sha) requestBody.sha = existingFileData.sha
} else if (fileExistsRes.status !== 404) {
const errorData = await fileExistsRes.json()
if (fileExistsRes.status === 403) {
throw new Error('Permission denied. Make sure your token has "contents: write" permission.')
} else if (fileExistsRes.status === 401) {
throw new Error('Authentication failed. Your GitHub token may be invalid or expired.')
} else {
throw new Error(`Error checking file: ${errorData.message || 'Unknown error'}`)
}
}
let response = await fetch(apiUrl, {
method: 'PUT',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
body: JSON.stringify(requestBody),
})
// Retry on SHA mismatch (409)
let retries = 0
while (response.status === 409 && retries < 3) {
retries++
try {
const latestRes = await fetch(`${apiUrl}?ref=${branch}`, {
headers: { Authorization: `Bearer ${token}` },
})
if (!latestRes.ok) break
const latestData = await latestRes.json()
if (!latestData?.sha) break
requestBody.sha = latestData.sha
response = await fetch(apiUrl, {
method: 'PUT',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
body: JSON.stringify(requestBody),
})
if (response.ok) break
} catch {
break
}
await sleep(500)
}