-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
2304 lines (2033 loc) · 97.3 KB
/
content.js
File metadata and controls
2304 lines (2033 loc) · 97.3 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
(async function () {
if (document.getElementById("cfpm-compact")) return;
const CATEGORIES = ["Div1", "Div2", "Div3", "Div4", "Other"];
const SETTINGS_KEY = "cfpm_defaults";
function loadSettings() {
try { const raw = localStorage.getItem(SETTINGS_KEY); if (raw) return JSON.parse(raw); } catch(e) {}
return {};
}
function saveSettings(obj) {
try { localStorage.setItem(SETTINGS_KEY, JSON.stringify(obj)); } catch(e) {}
}
const _saved = loadSettings();
let DEFAULT_CATEGORY = _saved.category || "Div4";
let DEFAULT_MODE = _saved.mode || "total";
let DEFAULT_TIMELINE = _saved.timeline || "all";
let DEFAULT_SORT_MODE = _saved.sortMode || "errors";
let DEFAULT_HIDE_AC = _saved.hideAC !== undefined ? _saved.hideAC : false;
let DEFAULT_HIDE_TAGS = _saved.hideTags !== undefined ? _saved.hideTags : false;
let DEFAULT_HIDE_RATINGS = _saved.hideRatings !== undefined ? _saved.hideRatings : false;
let DEFAULT_SOLVED_ONLY = _saved.solvedOnly !== undefined ? _saved.solvedOnly : false;
let DEFAULT_MIN_ATTEMPTS = _saved.minAttempts !== undefined ? Math.max(1, _saved.minAttempts) : 1;
let DEFAULT_RATING_MIN = _saved.ratingMin !== undefined ? _saved.ratingMin : "";
let DEFAULT_RATING_MAX = _saved.ratingMax !== undefined ? _saved.ratingMax : "";
let DEFAULT_CUSTOM_START = _saved.customStart || "";
let DEFAULT_CUSTOM_END = _saved.customEnd || "";
let DEFAULT_TAG_FILTERS = Array.isArray(_saved.tagFilters) ? _saved.tagFilters : [];
let DEFAULT_TABLE_VISIBLE = _saved.tableVisible !== undefined ? _saved.tableVisible : true;
const TOGGLE_KEY = "cfpm_enabled";
function loadToggle() {
try { const v = localStorage.getItem(TOGGLE_KEY); return v === null ? true : v === "true"; } catch(e) { return true; }
}
function saveToggle(val) {
try { localStorage.setItem(TOGGLE_KEY, String(val)); } catch(e) {}
}
let isEnabled = loadToggle();
const contestMap = {};
let rawSubmissions = [];
let ratedContestSet = new Set();
let userRatingHistory = [];
const DEFAULT_INDICES = ["A", "B", "C", "D", "E", "F", "G", "H"];
// ── ALL CODEFORCES TAGS (shown always, independent of current timeline/div) ──
const ALL_CF_TAGS = [
"*special", "2-sat", "binary search", "bitmasks", "brute force",
"chinese remainder theorem", "combinatorics", "constructive algorithms",
"data structures", "dfs and similar", "divide and conquer", "dp", "dsu",
"expression parsing", "fft", "flows", "games", "geometry",
"graph matchings", "graphs", "greedy", "hashing", "implementation",
"interactive", "math", "matrices", "meet-in-the-middle", "number theory",
"probabilities", "schedules", "shortest paths", "sortings", "special",
"string suffix structures", "strings", "ternary search", "trees",
"two pointers"
];
let defaultSortMode = DEFAULT_SORT_MODE;
if (defaultSortMode !== "errors" && defaultSortMode !== "rating") defaultSortMode = "errors";
let hideAC = DEFAULT_HIDE_AC;
let hideTagsGlobal = DEFAULT_HIDE_TAGS;
let hideRatingsGlobal = DEFAULT_HIDE_RATINGS;
let solvedOnlyGlobal = DEFAULT_SOLVED_ONLY;
let ratingMinGlobal = DEFAULT_RATING_MIN;
let ratingMaxGlobal = DEFAULT_RATING_MAX;
let minAttemptsGlobal = DEFAULT_MIN_ATTEMPTS;
let tableVisible = DEFAULT_TABLE_VISIBLE;
let frictionActiveTab = "category";
let savedTimeline = DEFAULT_TIMELINE;
let lastAppliedCustomStart = DEFAULT_CUSTOM_START;
let lastAppliedCustomEnd = DEFAULT_CUSTOM_END;
let activeTagFilters = new Set(DEFAULT_TAG_FILTERS);
let lastDeltaInfo = null;
function detectDarkMode() {
const hasDarkClass =
document.documentElement.classList.contains('dark') ||
document.documentElement.classList.contains('dark-mode') ||
document.body.classList.contains('dark') ||
document.body.classList.contains('dark-mode');
if (hasDarkClass) return true;
const containers = [
document.querySelector('.info'),
document.querySelector('.datatable'),
document.querySelector('.roundbox'),
document.querySelector('#pageContent'),
document.querySelector('.second-level-menu-list'),
document.body
];
for (const container of containers) {
if (container) {
const bg = window.getComputedStyle(container).backgroundColor;
if (bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent') {
const rgb = bg.match(/\d+/g);
if (rgb && rgb.length >= 3) {
const brightness = (parseInt(rgb[0]) * 299 + parseInt(rgb[1]) * 587 + parseInt(rgb[2]) * 114) / 1000;
if (brightness < 128) return true;
}
}
}
}
return false;
}
function getBoxBackground() {
for (const sel of ['.info', '.roundbox', '#pageContent']) {
const el = document.querySelector(sel);
if (el) {
const bg = window.getComputedStyle(el).backgroundColor;
if (bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent') return bg;
}
}
const bodyBg = window.getComputedStyle(document.body).backgroundColor;
if (bodyBg && bodyBg !== 'rgba(0, 0, 0, 0)' && bodyBg !== 'transparent') return bodyBg;
return detectDarkMode() ? '#1a1a1a' : '#ffffff';
}
function getBoxBorderColor() {
for (const sel of ['.roundbox', '.info', '.datatable']) {
const el = document.querySelector(sel);
if (el) {
const bc = window.getComputedStyle(el).borderColor;
if (bc && bc !== 'rgba(0, 0, 0, 0)' && bc !== 'transparent') return bc;
}
}
return detectDarkMode() ? '#444' : '#d4d4d4';
}
function createTheme() {
const isDark = detectDarkMode();
return {
bg: getBoxBackground(),
text: isDark ? '#e8e8e8' : '#0b1220',
border: getBoxBorderColor(),
borderLight: isDark ? '#3a3a3a' : '#e8e8e8',
borderLighter: isDark ? '#2e2e2e' : '#f2f2f2',
muted: isDark ? '#999' : '#777',
mutedStrong: isDark ? '#bbb' : '#555',
headingText: isDark ? '#ddd' : '#222',
btnBg: isDark ? '#2e2e2e' : '#f4f4f4',
btnText: isDark ? '#ccc' : '#444',
btnBorder: isDark ? '#484848' : '#d0d0d0',
btnActiveBg: '#1652d6',
btnActiveText: '#ffffff',
btnActiveBorder: '#1652d6',
tableHeaderText: isDark ? '#bbb' : '#666',
tableCellText: isDark ? '#ccc' : '#555',
emptyText: isDark ? '#666' : '#aaa',
selectBg: isDark ? '#242424' : '#fff',
selectText: isDark ? '#ddd' : '#333',
selectBorder: isDark ? '#484848' : '#d0d0d0',
inputBg: isDark ? '#242424' : '#fff',
inputText: isDark ? '#ddd' : '#333',
inputBorder: isDark ? '#484848' : '#d0d0d0',
dropdownBg: isDark ? '#202020' : '#ffffff',
dropdownBorder: isDark ? '#383838' : '#e0e0e0',
dropdownSection: isDark ? '#1a1a1a' : '#f9f9f9',
problemLink: isDark ? '#7aabff' : '#1652d6',
solvedBadge: isDark ? '#1a3320' : '#e6f4ea',
solvedBadgeText: isDark ? '#4caf50' : '#276221',
waBadge: isDark ? '#331a1a' : '#fdecea',
waBadgeText: isDark ? '#f48080' : '#b71c1c',
tleBg: isDark ? '#2a2000' : '#fff8e1',
tleFg: isDark ? '#ffd54f' : '#b45309',
rteBg: isDark ? '#1a1a2e' : '#ede7f6',
rteFg: isDark ? '#9fa8da' : '#4527a0',
mleBg: isDark ? '#002828' : '#e0f2f1',
mleFg: isDark ? '#4db6ac' : '#00695c',
errBg: isDark ? '#2a2a2a' : '#f0f0f0',
errFg: isDark ? '#999' : '#555',
};
}
let theme = createTheme();
// ── GLOBAL STYLES ──
if (!document.getElementById("cfpm-toggle-style")) {
const s = document.createElement("style");
s.id = "cfpm-toggle-style";
s.textContent = `
#cfpm-compact { box-sizing: border-box; }
#cfpm-body {
overflow: hidden;
transition: max-height 0.32s cubic-bezier(0.4,0,0.2,1), opacity 0.28s ease;
max-height: 2000px; opacity: 1;
}
#cfpm-body.cfpm-collapsed { max-height: 0 !important; opacity: 0; pointer-events: none; }
#cfpm-chevron-btn {
display: flex; align-items: center; justify-content: center;
width: 22px; height: 22px; border-radius: 4px;
border: none; background: transparent; cursor: pointer;
flex-shrink: 0; outline: none !important;
-webkit-appearance: none; appearance: none; padding: 0;
}
#cfpm-chevron-btn:hover { background: rgba(128,128,128,0.1) !important; }
#cfpm-chevron-btn:focus, #cfpm-chevron-btn:active,
#cfpm-chevron-btn:focus-visible { outline: none !important; box-shadow: none !important; }
#cfpm-chevron-btn svg { transition: transform 0.22s cubic-bezier(0.4,0,0.2,1); }
#cfpm-chevron-btn.collapsed svg { transform: rotate(-90deg); }
#cfpm-compact button {
box-sizing: border-box; -webkit-appearance: none; appearance: none;
transition: none !important;
}
#cfpm-compact button:hover, #cfpm-compact button:focus,
#cfpm-compact button:active, #cfpm-compact button:focus-visible {
outline: none !important; box-shadow: none !important;
filter: none !important; -webkit-filter: none !important;
}
.cfpm-cat-btn {
display: inline-flex; align-items: center; justify-content: center;
height: 28px; padding: 0 14px; border-radius: 14px;
font-size: 12px; font-weight: 700; cursor: pointer; white-space: nowrap;
flex-shrink: 0; outline: none !important;
}
.cfpm-icon-btn {
width: 30px; height: 30px; border-radius: 5px;
display: flex; align-items: center; justify-content: center;
cursor: pointer; flex-shrink: 0; outline: none !important; padding: 0;
}
.cfpm-pill-btn {
display: inline-flex; align-items: center; justify-content: center;
height: 30px; padding: 0 12px; border-radius: 5px;
font-size: 12px; font-weight: 600; cursor: pointer;
white-space: nowrap; outline: none !important;
}
.cfpm-step-btn {
width: 24px; height: 24px; border-radius: 4px;
display: flex; align-items: center; justify-content: center;
cursor: pointer; flex-shrink: 0; outline: none !important;
-webkit-appearance: none; appearance: none;
}
.cfpm-tag-pill {
display: inline-flex; align-items: center; gap: 4px;
height: 22px; padding: 0 8px; border-radius: 11px; font-size: 11px;
font-weight: 600; cursor: pointer; white-space: nowrap; user-select: none;
}
.cfpm-tag-opt {
display: flex; align-items: center; gap: 8px;
padding: 6px 12px; font-size: 12px; cursor: pointer; user-select: none;
word-break: break-word;
}
.cfpm-tag-opt:hover { filter: brightness(0.95); }
.cfpm-tag-check {
width: 14px; height: 14px; border-radius: 3px; flex-shrink: 0;
display: flex; align-items: center; justify-content: center;
font-size: 9px; color: #fff; box-sizing: border-box;
}
.cfpm-sort-opt {
display: flex; align-items: center; gap: 10px;
padding: 9px 14px; font-size: 13px; cursor: pointer; user-select: none;
}
.cfpm-view-opt {
display: flex; align-items: center; gap: 10px;
padding: 9px 14px; font-size: 13px; cursor: pointer; user-select: none;
}
#cfpm-filter-dd {
position: absolute; top: calc(100% + 6px); right: 0; z-index: 10000;
border-radius: 6px; flex-direction: column;
min-width: 260px; max-width: 300px;
box-shadow: 0 8px 32px rgba(0,0,0,0.16), 0 2px 6px rgba(0,0,0,0.08);
}
#cfpm-sort-dd {
position: absolute; top: calc(100% + 6px); right: 0; z-index: 10000;
border-radius: 7px; overflow: hidden; min-width: 190px;
box-shadow: 0 6px 24px rgba(0,0,0,0.13), 0 1.5px 4px rgba(0,0,0,0.07);
}
#cfpm-view-dd {
position: absolute; top: calc(100% + 6px); right: 0; z-index: 10000;
border-radius: 7px; overflow: hidden; min-width: 210px;
box-shadow: 0 6px 24px rgba(0,0,0,0.13), 0 1.5px 4px rgba(0,0,0,0.07);
}
#cfpm-tag-list { overflow-y: auto; max-height: 110px; }
#cfpm-tag-list::-webkit-scrollbar { width: 4px; }
#cfpm-tag-list::-webkit-scrollbar-track { background: transparent; }
#cfpm-tag-list::-webkit-scrollbar-thumb { border-radius: 2px; background: rgba(128,128,128,0.25); }
.cfpm-rating-input::-webkit-outer-spin-button,
.cfpm-rating-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.cfpm-rating-input[type=number] { -moz-appearance: textfield; appearance: textfield; }
.cfpm-tag-search {
width: 100%; box-sizing: border-box;
height: 30px; padding: 0 10px 0 30px;
font-size: 12px; font-family: inherit;
outline: none; border: none;
background: transparent;
}
.cfpm-tag-search-wrap {
position: relative; display: flex; align-items: center;
}
.cfpm-tag-search-wrap svg {
position: absolute; left: 9px; pointer-events: none; flex-shrink: 0;
}
.cfpm-add-topic-btn {
display: inline-flex; align-items: center; gap: 4px;
height: 22px; padding: 0 8px; border-radius: 3px;
font-size: 11px; font-weight: 600; cursor: pointer;
white-space: nowrap; outline: none !important;
-webkit-appearance: none; appearance: none;
transition: none !important;
}
.cfpm-filter-tag-pills {
display: flex; flex-wrap: wrap; gap: 4px; padding: 0 12px 8px 12px;
}
.cfpm-filter-tag-pill {
display: inline-flex; align-items: center; gap: 3px;
height: 20px; padding: 0 7px; border-radius: 10px;
font-size: 11px; font-weight: 600; cursor: pointer;
user-select: none; white-space: nowrap;
}
.cfpm-filter-tag-pill-x {
font-size: 9px; opacity: 0.6; margin-left: 1px;
}
`;
document.head.appendChild(s);
}
const card = document.createElement("div");
card.id = "cfpm-compact";
card.style.cssText = [
"box-sizing:border-box", "font-family:Arial,sans-serif", "font-size:14px",
`color:${theme.text}`, `background:${theme.bg}`, `border:1px solid ${theme.border}`,
"border-radius:5px", "padding:0", "margin-top:10px", "max-width:920px"
].join(";");
// ── HEADER BAR ──
const headerBar = document.createElement("div");
headerBar.style.cssText = [
"display:flex", "align-items:center", "justify-content:space-between",
"padding:5px 12px", "gap:10px", "min-height:32px", "box-sizing:border-box"
].join(";");
const headerTitle = document.createElement("span");
headerTitle.style.cssText = `font-size:11px;font-weight:700;color:${theme.muted};letter-spacing:0.1em;cursor:default;user-select:none;font-family:monospace;opacity:0.8;`;
headerTitle.textContent = "cfpm";
const toggleBtn = document.createElement("button");
toggleBtn.id = "cfpm-chevron-btn";
toggleBtn.innerHTML = `<svg width="13" height="13" viewBox="0 0 13 13" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M2.5 4.5l4 4 4-4"/></svg>`;
function applyToggleVisuals() {
if (isEnabled) {
toggleBtn.classList.remove("collapsed");
toggleBtn.title = "Collapse";
} else {
toggleBtn.classList.add("collapsed");
toggleBtn.title = "Expand";
}
toggleBtn.style.color = theme.muted;
headerTitle.style.opacity = isEnabled ? "0.8" : "0.4";
}
applyToggleVisuals();
const body = document.createElement("div");
body.id = "cfpm-body";
body.style.cssText = "padding:0 14px 14px 14px;box-sizing:border-box;";
if (!isEnabled) body.classList.add("cfpm-collapsed");
toggleBtn.addEventListener("click", () => {
isEnabled = !isEnabled;
saveToggle(isEnabled);
applyToggleVisuals();
body.classList.toggle("cfpm-collapsed", !isEnabled);
});
headerBar.appendChild(headerTitle);
headerBar.appendChild(toggleBtn);
card.appendChild(headerBar);
const headerDivider = document.createElement("div");
headerDivider.id = "cfpm-header-divider";
headerDivider.style.cssText = `height:1px;background:${theme.borderLight};margin:0;`;
card.appendChild(headerDivider);
card.appendChild(body);
// ── CONTROLS ROW ──
const controlsRow = document.createElement("div");
controlsRow.style.cssText = "display:flex;align-items:center;justify-content:space-between;gap:10px;margin-bottom:12px;min-height:36px;margin-top:12px;";
const leftControls = document.createElement("div");
leftControls.style.cssText = "display:flex;gap:6px;flex-wrap:wrap;align-items:center;";
const categoryButtons = {};
CATEGORIES.forEach(cat => {
const b = document.createElement("button");
b.className = "cfpm-cat-btn";
b.textContent = cat;
b.dataset.cat = cat;
b.style.cssText = [`background:${theme.btnBg}`, `color:${theme.btnText}`, `border:1px solid ${theme.btnBorder}`, "cursor:pointer"].join(";");
b.addEventListener("click", () => { renderCategory(cat); autoSave(); });
categoryButtons[cat] = b;
leftControls.appendChild(b);
});
const rightControls = document.createElement("div");
rightControls.style.cssText = "display:flex;align-items:center;gap:7px;flex-shrink:0;";
function selectStyle() {
return `height:30px;padding:0 8px;border-radius:5px;border:1px solid ${theme.selectBorder};background:${theme.selectBg};color:${theme.selectText};font-size:12px;font-family:Arial,sans-serif;white-space:nowrap;outline:none;cursor:pointer;`;
}
// ── TABLE TOGGLE BUTTON ──
const tableToggleBtn = document.createElement("button");
tableToggleBtn.className = "cfpm-icon-btn";
tableToggleBtn.innerHTML = `<svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><rect x="1" y="2" width="14" height="12" rx="1.5"/><path d="M1 6h14"/><path d="M1 10h14"/><path d="M5.5 6v8"/></svg>`;
function applyTableToggleBtnStyle() {
tableToggleBtn.title = tableVisible ? "Hide stats table" : "Show stats table";
tableToggleBtn.style.cssText = [
`background:${tableVisible ? theme.btnActiveBg : theme.btnBg}`,
`color:${tableVisible ? theme.btnActiveText : theme.muted}`,
`border:1px solid ${tableVisible ? theme.btnActiveBorder : theme.btnBorder}`,
"cursor:pointer"
].join(";");
}
applyTableToggleBtnStyle();
tableToggleBtn.addEventListener("click", () => {
tableVisible = !tableVisible;
tableWrap.style.display = tableVisible ? "" : "none";
applyTableToggleBtnStyle();
autoSave();
});
const timelineSelect = document.createElement("select");
[
{ value: "all", label: "All time" },
{ value: "1", label: "Last month" },
{ value: "3", label: "Last 3 months" },
{ value: "6", label: "Last 6 months" },
{ value: "12", label: "Last year" },
{ value: "24", label: "Last 2 years" },
{ value: "custom", label: "Custom range" }
].forEach(opt => {
const o = document.createElement("option");
o.value = opt.value; o.textContent = opt.label;
timelineSelect.appendChild(o);
});
timelineSelect.value = DEFAULT_TIMELINE;
timelineSelect.style.cssText = selectStyle() + "min-width:130px;";
let timelineValueOnFocus = timelineSelect.value;
timelineSelect.addEventListener("mousedown", () => { timelineValueOnFocus = timelineSelect.value; });
timelineSelect.addEventListener("change", () => {
if (timelineSelect.value === "custom") {
customDateRow.style.display = "flex";
} else {
savedTimeline = timelineSelect.value;
customDateRow.style.display = "none";
renderCategory(currentCategory || DEFAULT_CATEGORY);
autoSave();
}
});
timelineSelect.addEventListener("click", () => {
if (timelineSelect.value === "custom" && timelineValueOnFocus === "custom") {
customDateRow.style.display = "flex";
}
});
const modeSelect = document.createElement("select");
[
{ value: "total", label: "Total" },
{ value: "rated", label: "Rated" },
{ value: "unrated", label: "Unrated" }
].forEach(opt => {
const o = document.createElement("option");
o.value = opt.value; o.textContent = opt.label;
modeSelect.appendChild(o);
});
modeSelect.value = DEFAULT_MODE;
modeSelect.style.cssText = selectStyle() + "min-width:90px;";
modeSelect.addEventListener("change", () => { renderCategory(currentCategory || DEFAULT_CATEGORY); autoSave(); });
rightControls.appendChild(tableToggleBtn);
rightControls.appendChild(timelineSelect);
rightControls.appendChild(modeSelect);
controlsRow.appendChild(leftControls);
controlsRow.appendChild(rightControls);
body.appendChild(controlsRow);
// ── CUSTOM DATE ROW ──
const customDateRow = document.createElement("div");
customDateRow.style.cssText = `display:none;align-items:center;gap:8px;margin-bottom:10px;padding:10px 12px;background:${theme.dropdownSection};border:1px solid ${theme.borderLight};border-radius:5px;flex-wrap:wrap;`;
const dateFromLabel = document.createElement("span");
dateFromLabel.textContent = "From";
dateFromLabel.style.cssText = `color:${theme.muted};font-size:12px;font-weight:600;white-space:nowrap;`;
const startDateInput = document.createElement("input");
startDateInput.type = "date";
startDateInput.value = DEFAULT_CUSTOM_START;
startDateInput.style.cssText = `height:30px;padding:0 8px;border-radius:5px;border:1px solid ${theme.inputBorder};background:${theme.inputBg};color:${theme.inputText};font-size:12px;flex:1 1 130px;min-width:130px;max-width:170px;color-scheme:${detectDarkMode()?"dark":"light"};outline:none;`;
const dateToLabel = document.createElement("span");
dateToLabel.textContent = "To";
dateToLabel.style.cssText = `color:${theme.muted};font-size:12px;font-weight:600;white-space:nowrap;`;
const endDateInput = document.createElement("input");
endDateInput.type = "date";
endDateInput.value = DEFAULT_CUSTOM_END;
endDateInput.style.cssText = `height:30px;padding:0 8px;border-radius:5px;border:1px solid ${theme.inputBorder};background:${theme.inputBg};color:${theme.inputText};font-size:12px;flex:1 1 130px;min-width:130px;max-width:170px;color-scheme:${detectDarkMode()?"dark":"light"};outline:none;`;
const dateButtonGroup = document.createElement("div");
dateButtonGroup.style.cssText = "display:flex;gap:6px;margin-left:auto;align-items:center;";
const applyDateBtn = document.createElement("button");
applyDateBtn.className = "cfpm-pill-btn";
applyDateBtn.textContent = "Apply";
applyDateBtn.style.cssText = `background:${theme.btnActiveBg};color:${theme.btnActiveText};border:1px solid ${theme.btnActiveBorder};cursor:pointer;`;
const cancelDateBtn = document.createElement("button");
cancelDateBtn.className = "cfpm-pill-btn";
cancelDateBtn.textContent = "Cancel";
cancelDateBtn.style.cssText = `background:${theme.btnBg};color:${theme.btnText};border:1px solid ${theme.btnBorder};cursor:pointer;`;
const dateValidationMsg = document.createElement("span");
dateValidationMsg.style.cssText = "color:#e74c3c;font-size:11px;font-weight:600;display:none;white-space:nowrap;";
dateValidationMsg.textContent = "Select both dates.";
dateButtonGroup.appendChild(dateValidationMsg);
dateButtonGroup.appendChild(applyDateBtn);
dateButtonGroup.appendChild(cancelDateBtn);
customDateRow.appendChild(dateFromLabel);
customDateRow.appendChild(startDateInput);
customDateRow.appendChild(dateToLabel);
customDateRow.appendChild(endDateInput);
customDateRow.appendChild(dateButtonGroup);
body.appendChild(customDateRow);
applyDateBtn.addEventListener("click", () => {
if (startDateInput.value && endDateInput.value) {
dateValidationMsg.style.display = "none";
customDateRow.style.display = "none";
savedTimeline = "custom";
lastAppliedCustomStart = startDateInput.value;
lastAppliedCustomEnd = endDateInput.value;
autoSave();
renderCategory(currentCategory || DEFAULT_CATEGORY);
} else {
dateValidationMsg.style.display = "inline";
}
});
cancelDateBtn.addEventListener("click", () => {
customDateRow.style.display = "none";
dateValidationMsg.style.display = "none";
startDateInput.value = lastAppliedCustomStart;
endDateInput.value = lastAppliedCustomEnd;
timelineSelect.value = savedTimeline;
renderCategory(currentCategory || DEFAULT_CATEGORY);
});
const info = document.createElement("div");
info.style.cssText = `color:${theme.muted};font-size:12px;margin-top:2px;margin-bottom:10px;`;
info.textContent = "Loading…";
body.appendChild(info);
const tableWrap = document.createElement("div");
tableWrap.style.cssText = "overflow-x:auto;margin-bottom:12px;";
tableWrap.style.display = tableVisible ? "" : "none";
const table = document.createElement("table");
table.style.cssText = "border-collapse:collapse;font-size:13px;width:100%;";
tableWrap.appendChild(table);
body.appendChild(tableWrap);
const frictionSection = document.createElement("div");
frictionSection.style.cssText = `margin-top:10px;border-top:1px solid ${theme.borderLight};padding-top:10px;`;
const frictionScrollBox = document.createElement("div");
frictionScrollBox.style.cssText = [
"height:260px", "overflow:visible",
`border:1px solid ${theme.borderLight}`,
"border-radius:5px", "display:flex", "flex-direction:column", "padding:0",
"position:relative"
].join(";");
frictionSection.appendChild(frictionScrollBox);
body.appendChild(frictionSection);
function autoSave() {
saveSettings({
category: currentCategory || DEFAULT_CATEGORY,
timeline: savedTimeline,
mode: modeSelect.value,
sortMode: defaultSortMode,
hideAC: hideAC,
hideTags: hideTagsGlobal,
hideRatings: hideRatingsGlobal,
solvedOnly: solvedOnlyGlobal,
minAttempts: minAttemptsGlobal,
ratingMin: ratingMinGlobal,
ratingMax: ratingMaxGlobal,
customStart: startDateInput ? startDateInput.value : "",
customEnd: endDateInput ? endDateInput.value : "",
tagFilters: Array.from(activeTagFilters),
tableVisible: tableVisible,
});
}
function insertCard() {
const visible = Array.from(document.querySelectorAll(".box")).filter(el => {
const r = el.getBoundingClientRect();
return r.width > 220 && r.height > 50;
});
if (visible.length > 0) {
const last = visible[visible.length - 1];
card.style.width = Math.round(last.getBoundingClientRect().width) + "px";
last.insertAdjacentElement("afterend", card);
if (window.ResizeObserver) {
new ResizeObserver(entries => {
for (const e of entries) {
const nw = Math.round(e.contentRect.width);
if (nw > 220) card.style.width = nw + "px";
}
}).observe(last);
}
return;
}
const main = document.querySelector("#pageContent, #mainContent, .mainContent, .content");
if (main) {
card.style.width = Math.round(main.getBoundingClientRect().width) + "px";
main.appendChild(card);
if (window.ResizeObserver) {
new ResizeObserver(entries => {
for (const e of entries) {
const nw = Math.round(e.contentRect.width);
if (nw > 220) card.style.width = nw + "px";
}
}).observe(main);
}
return;
}
document.body.appendChild(card);
card.style.width = "880px";
}
insertCard();
let previousTheme = { isDark: detectDarkMode(), bg: theme.bg, border: theme.border };
function applyTheme() {
card.style.background = theme.bg;
card.style.border = `1px solid ${theme.border}`;
card.style.color = theme.text;
headerDivider.style.background = theme.borderLight;
info.style.color = theme.muted;
frictionSection.style.borderTop = `1px solid ${theme.borderLight}`;
frictionScrollBox.style.borderColor = theme.borderLight;
timelineSelect.style.cssText = selectStyle() + "min-width:130px;";
modeSelect.style.cssText = selectStyle() + "min-width:90px;";
customDateRow.style.background = theme.dropdownSection;
customDateRow.style.borderColor = theme.borderLight;
dateFromLabel.style.color = theme.muted;
dateToLabel.style.color = theme.muted;
const cs = detectDarkMode() ? "dark" : "light";
startDateInput.style.colorScheme = cs;
endDateInput.style.colorScheme = cs;
startDateInput.style.background = theme.inputBg;
startDateInput.style.color = theme.inputText;
startDateInput.style.borderColor = theme.inputBorder;
endDateInput.style.background = theme.inputBg;
endDateInput.style.color = theme.inputText;
endDateInput.style.borderColor = theme.inputBorder;
applyDateBtn.style.background = theme.btnActiveBg;
applyDateBtn.style.color = theme.btnActiveText;
applyDateBtn.style.borderColor = theme.btnActiveBorder;
cancelDateBtn.style.background = theme.btnBg;
cancelDateBtn.style.color = theme.btnText;
cancelDateBtn.style.borderColor = theme.btnBorder;
headerTitle.style.color = theme.muted;
toggleBtn.style.color = theme.muted;
applyToggleVisuals();
applyTableToggleBtnStyle();
Object.keys(categoryButtons).forEach(k => {
const b = categoryButtons[k];
const active = k === currentCategory;
b.style.background = active ? theme.btnActiveBg : theme.btnBg;
b.style.color = active ? theme.btnActiveText : theme.btnText;
b.style.borderColor = active ? theme.btnActiveBorder : theme.btnBorder;
});
}
function updateTheme() {
const newIsDark = detectDarkMode();
const newTheme = createTheme();
if (
newIsDark !== previousTheme.isDark ||
newTheme.bg !== previousTheme.bg ||
newTheme.border !== previousTheme.border
) {
theme = newTheme;
previousTheme = { isDark: newIsDark, bg: theme.bg, border: theme.border };
applyTheme();
if (currentCategory && lastModeData) {
renderTableForCategory(lastModeData, currentCategory, lastDeltaInfo);
renderFrictionPanels(lastModeData, currentCategory);
}
}
}
setTimeout(applyTheme, 100);
const themeObserver = new MutationObserver(updateTheme);
themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['class','style','data-theme'] });
themeObserver.observe(document.body, { attributes: true, attributeFilter: ['class','style','data-theme'] });
['.info', '.roundbox', '#pageContent']
.map(sel => document.querySelector(sel))
.filter(Boolean)
.forEach(el => themeObserver.observe(el, { attributes: true, attributeFilter: ['style','class'] }));
if (window._cfpmThemeInterval) clearInterval(window._cfpmThemeInterval);
window._cfpmThemeInterval = setInterval(updateTheme, 500);
window.addEventListener('focus', updateTheme);
function median(arr) {
if (!arr || !arr.length) return null;
const s = arr.slice().sort((a, b) => a - b);
const m = Math.floor(s.length / 2);
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
}
function totalErrors(p) {
return (p.wa || 0) + (p.tle || 0) + (p.rte || 0) + (p.mle || 0) + (p.other || 0);
}
function getRatingColor(r) {
if (!r || r < 1200) return "#808080";
if (r < 1400) return "#008000";
if (r < 1600) return "#03a89e";
if (r < 1900) return "#0000ff";
if (r < 2100) return "#aa00aa";
if (r < 2400) return "#ff8c00";
if (r < 3000) return "#ff0000";
return "#cc0000";
}
function calcDeltaRating(cat, timelineValue) {
const nowSec = Math.floor(Date.now() / 1000);
let cutoffTime = 0, endTime = nowSec;
if (typeof timelineValue === 'object' && timelineValue.type === 'custom') {
if (timelineValue.start && timelineValue.end) {
cutoffTime = new Date(timelineValue.start).getTime() / 1000;
endTime = new Date(timelineValue.end).getTime() / 1000 + 86399;
}
} else if (timelineValue !== "all") {
cutoffTime = nowSec - parseInt(timelineValue) * 30 * 24 * 3600;
}
let delta = 0, count = 0;
const sorted = [...userRatingHistory].sort((a, b) => a.ratingUpdateTimeSeconds - b.ratingUpdateTimeSeconds);
sorted.forEach(rc => {
const t = rc.ratingUpdateTimeSeconds;
if (t < cutoffTime || t > endTime) return;
const contest = contestMap[rc.contestId];
if (!contest) return;
let contestCat = classifyContest(contest);
if (contestCat === "Div1+Div2") {
contestCat = rc.oldRating >= 1900 ? "Div1" : "Div2";
}
if (contestCat !== cat) return;
delta += (rc.newRating - rc.oldRating);
count++;
});
return { delta, count };
}
function decideUserDivisionForContest(cid, contest, isUnofficial) {
if (!contest || typeof contest.startTimeSeconds !== "number") return "Div2";
if (isUnofficial) return "Div2";
let ratingBefore = 0;
const sorted = [...userRatingHistory].sort((a, b) => a.ratingUpdateTimeSeconds - b.ratingUpdateTimeSeconds);
for (const rc of sorted) {
if (rc.contestId === cid) { ratingBefore = rc.oldRating; break; }
if (rc.ratingUpdateTimeSeconds < contest.startTimeSeconds) ratingBefore = rc.newRating;
}
return ratingBefore >= 1900 ? "Div1" : "Div2";
}
function classifyContest(contest) {
if (!contest || !contest.name) return "Other";
const n = String(contest.name);
if (/Div\.?\s*1\s*\+\s*Div\.?\s*2/i.test(n) || /Div\.?\s*2\s*\+\s*Div\.?\s*1/i.test(n) || /Global/i.test(n)) return "Div1+Div2";
if (/Educational/i.test(n)) return "Div2";
const m = n.match(/Div\.?\s*([1-4])|Division\s*([1-4])/i);
if (m) return "Div" + (m[1] || m[2]);
return "Other";
}
async function fetchContests() {
try {
const res = await fetch("https://codeforces.com/api/contest.list");
const json = await res.json();
if (json.status === "OK") json.result.forEach(c => { contestMap[c.id] = c; });
} catch(e) {
info.textContent = "Contest list unavailable — some data may be incomplete.";
}
}
async function fetchRatedSet(handle) {
try {
const r = await fetch(`https://codeforces.com/api/user.rating?handle=${handle}`);
const d = await r.json();
if (d.status === "OK") {
userRatingHistory = d.result || [];
return new Set(d.result.map(x => x.contestId));
}
} catch(e) {}
userRatingHistory = [];
return new Set();
}
async function fetchAndStore(handle) {
try {
info.textContent = "Fetching your submissions from Codeforces…";
const r = await fetch(`https://codeforces.com/api/user.status?handle=${handle}&count=10000`);
const d = await r.json();
if (d.status !== "OK") {
info.textContent = "Codeforces returned an error: " + (d.comment || "unknown");
return false;
}
rawSubmissions = d.result || [];
ratedContestSet = await fetchRatedSet(handle);
return true;
} catch(e) {
info.textContent = "Could not connect to Codeforces. Please check your connection and try again.";
return false;
}
}
// ── CORE CALCULATION ──
function recalcForMode(mode, timelineMonths) {
const now = Math.floor(Date.now() / 1000);
let cutoffTime = 0, endTime = now;
if (typeof timelineMonths === 'object' && timelineMonths.type === 'custom') {
if (timelineMonths.start && timelineMonths.end) {
cutoffTime = new Date(timelineMonths.start).getTime() / 1000;
endTime = new Date(timelineMonths.end).getTime() / 1000 + 86399;
}
} else if (timelineMonths !== "all") {
cutoffTime = now - (parseInt(timelineMonths) * 30 * 24 * 60 * 60);
}
const filteredSubmissions = (cutoffTime === 0 && endTime === now)
? rawSubmissions
: rawSubmissions.filter(s =>
s.creationTimeSeconds &&
s.creationTimeSeconds >= cutoffTime &&
s.creationTimeSeconds <= endTime
);
const inWindowSet = new Set();
filteredSubmissions.forEach(s => {
if (!s.problem) return;
const cid = s.problem.contestId;
const c = contestMap[cid];
if (!c || typeof c.startTimeSeconds !== "number" || typeof c.durationSeconds !== "number") return;
const st = s.creationTimeSeconds;
const start = c.startTimeSeconds;
const end = start + c.durationSeconds;
if (typeof st === "number" && st >= start && st <= end) inWindowSet.add(cid);
});
let participated = new Set();
if (mode === "total") participated = new Set([...ratedContestSet, ...inWindowSet]);
else if (mode === "rated") participated = new Set([...ratedContestSet]);
else inWindowSet.forEach(cid => { if (!ratedContestSet.has(cid)) participated.add(cid); });
if (cutoffTime !== 0 || endTime !== now) {
const tmp = new Set();
participated.forEach(cid => {
const c = contestMap[cid];
if (c && c.startTimeSeconds >= cutoffTime && c.startTimeSeconds <= endTime) tmp.add(cid);
});
participated = tmp;
}
const categoryIndexTimes = {};
const categoryIndexAttempts = {};
const categoryIndexSolved = {};
const categoryContestCount = {};
CATEGORIES.forEach(c => {
categoryIndexTimes[c] = {};
categoryIndexAttempts[c] = {};
categoryIndexSolved[c] = {};
categoryContestCount[c] = new Set();
});
const unofficialForTable = new Set();
participated.forEach(cid => { if (!ratedContestSet.has(cid)) unofficialForTable.add(cid); });
const firstACSet = new Set();
filteredSubmissions.forEach(s => {
if (!s.problem) return;
const cid = s.problem.contestId;
if (!participated.has(cid)) return;
const contest = contestMap[cid];
if (!contest || typeof contest.startTimeSeconds !== "number" || typeof contest.durationSeconds !== "number") return;
const start = contest.startTimeSeconds;
const end = start + contest.durationSeconds;
const st = s.creationTimeSeconds;
if (typeof st !== "number" || st < start || st > end) return;
const idx = s.problem.index;
const pid = cid + "-" + idx;
let cat = classifyContest(contest);
if (cat === "Div1+Div2") cat = decideUserDivisionForContest(cid, contest, unofficialForTable.has(cid));
if (!categoryIndexAttempts[cat]) {
cat = "Other";
if (!categoryIndexAttempts[cat]) {
categoryIndexAttempts[cat] = {};
categoryIndexTimes[cat] = {};
categoryIndexSolved[cat] = {};
categoryContestCount[cat] = new Set();
}
}
categoryContestCount[cat].add(cid);
categoryIndexAttempts[cat][idx] = (categoryIndexAttempts[cat][idx] || 0) + 1;
categoryIndexTimes[cat][idx] = categoryIndexTimes[cat][idx] || [];
if (s.verdict !== "OK") return;
if (firstACSet.has(pid)) return;
firstACSet.add(pid);
categoryIndexSolved[cat][idx] = (categoryIndexSolved[cat][idx] || 0) + 1;
const timeMin = (st - start) / 60;
const maxAllowed = Math.max(1, Math.round(contest.durationSeconds / 60));
if (timeMin >= 0 && timeMin <= maxAllowed) categoryIndexTimes[cat][idx].push(timeMin);
});
const everAC = new Set();
rawSubmissions.forEach(s => {
if (s.verdict === "OK" && s.problem) everAC.add(s.problem.contestId + "-" + s.problem.index);
});
const inContestCids = new Set();
filteredSubmissions.forEach(s => {
if (!s.problem) return;
const cid = s.problem.contestId;
const contest = contestMap[cid];
if (!contest || typeof contest.startTimeSeconds !== "number" || typeof contest.durationSeconds !== "number") return;
const start = contest.startTimeSeconds;
const end = start + contest.durationSeconds;
const st = s.creationTimeSeconds;
if (typeof st === "number" && st >= start && st <= end) inContestCids.add(cid);
});
const unofficialForList = new Set();
inContestCids.forEach(cid => { if (!ratedContestSet.has(cid)) unofficialForList.add(cid); });
const categoryRawWAMap = {};
CATEGORIES.forEach(c => { categoryRawWAMap[c] = new Map(); });
filteredSubmissions.forEach(s => {
if (!s.problem) return;
const cid = s.problem.contestId;
if (!inContestCids.has(cid)) return;
const contest = contestMap[cid];
if (!contest) return;
const start = contest.startTimeSeconds;
const end = start + contest.durationSeconds;
const st = s.creationTimeSeconds;
if (typeof st !== "number" || st < start || st > end) return;
const idx = s.problem.index;
const pid = cid + "-" + idx;
const tags = s.problem.tags || [];
let cat = classifyContest(contest);
if (cat === "Div1+Div2") cat = decideUserDivisionForContest(cid, contest, unofficialForList.has(cid));
if (!categoryRawWAMap[cat]) cat = "Other";
if (!categoryRawWAMap[cat]) return;
if (!categoryRawWAMap[cat].has(pid)) {
categoryRawWAMap[cat].set(pid, {
pid,
name: s.problem.name || idx,
contestId: cid,
contestName: contest.name || ("Contest " + cid),
index: idx,
rating: s.problem.rating || null,
tags: tags.slice(),
solved: everAC.has(pid),
wa: 0, tle: 0, rte: 0, mle: 0, other: 0
});
}
});