-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
78 lines (66 loc) · 2.39 KB
/
content.js
File metadata and controls
78 lines (66 loc) · 2.39 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
function fixGraph() {
const graph = document.querySelector('.js-calendar-graph');
if (!graph) return;
const days = Array.from(graph.querySelectorAll('[data-level]'));
if (days.length === 0) return;
let max = 0;
const data = days.map(day => {
let count = 0;
// Strategy 1: data-count (Legacy/Certain views)
if (day.hasAttribute('data-count')) {
count = parseInt(day.getAttribute('data-count'), 10);
}
// Strategy 2: aria-labelledby (Modern view - Tooltip based)
else if (day.hasAttribute('aria-labelledby')) {
const tooltipId = day.getAttribute('aria-labelledby');
const tooltipEl = document.getElementById(tooltipId);
if (tooltipEl) {
const text = tooltipEl.textContent;
const match = text.match(/(\d+)\s+contribution/i);
if (match) {
count = parseInt(match[1], 10);
} else if (/No\s+contribution/i.test(text)) {
count = 0;
}
}
}
// Strategy 3: Parse aria-label or text content directly
else {
const text = day.textContent || day.getAttribute('aria-label') || "";
const match = text.match(/(\d+)\s+contribution/i);
if (match) {
count = parseInt(match[1], 10);
}
}
if (isNaN(count)) count = 0;
if (count > max) max = count;
return { element: day, count };
});
if (max === 0) return;
data.forEach(({ element, count }) => {
if (count === 0) {
element.setAttribute('data-level', '0');
return;
}
// Logarithmic scale implementation
const logValue = Math.log(count + 1);
const logMax = Math.log(max + 1);
const ratio = logValue / logMax;
// Scale to range 1-4
let level = Math.ceil(ratio * 4);
if (level < 1) level = 1;
if (level > 4) level = 4;
element.setAttribute('data-level', level.toString());
});
}
// Initial run
fixGraph();
// Handle dynamic navigation (GitHub uses Turbo/Pjax)
let timeout = null;
const observer = new MutationObserver((mutations) => {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
fixGraph();
}, 300);
});
observer.observe(document.body, { childList: true, subtree: true });