-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
141 lines (114 loc) · 3.36 KB
/
Copy pathcontentScript.js
File metadata and controls
141 lines (114 loc) · 3.36 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
// Invoked when we get on an itch.io/dashboard website
(() => {
const saveKey = "scheme";
const dataAtr = "data-game_id";
const maxSceheme = 8;
// Invoked when we receive a message
chrome.runtime.onMessage.addListener((request, sender, response) => {
if (request.type == "Repaint")
Repaint();
});
function Repaint() {
GetCurrentScheme((schemeIndex) => {
const { elements, gamesData } = GetPageElements();
const colors = GetScemeColors(schemeIndex, gamesData.length);
const sortedGamesData = gamesData.sort((a, b) => a.count - b.count);
for (let element of elements) {
const id = element.getAttribute(dataAtr);
const data = sortedGamesData.find(item => item.id == id);
let index = sortedGamesData.indexOf(data);
index = (sortedGamesData.length - 1) - index
element.setAttribute("fill", colors[index]);
}
});
};
function GetCurrentScheme(onResult) {
chrome.storage.sync.get(
[saveKey], (obj) => {
let currentScheme = 0;
if (obj[saveKey] != null)
currentScheme = JSON.parse(obj[saveKey]);
else // save if we can't find any
SaveScheme(0);
if (currentScheme >= maxSceheme)
currentScheme = maxSceheme - 1;
onResult(currentScheme)
}
);
}
function GetScemeColors(scheme, count) {
switch (scheme) {
case 0:
return GetLerps("#6b1d1d", "#f2d234", count);
case 1:
return GetLerps("#483150", "#64e2ba", count);
case 2:
return GetLerps("#2f413b", "#cbe84b", count);
case 3:
return GetLerps("#343f4f", "#ff5d8e", count);
case 4:
return GetLerps("#313131", "#efefef", count);
case 5:
return GetLerps("#562335", "#93c9ed", count);
case 6:
return GetLerps("#5c1b26", "#fc3535", count);
case 7:
return GetLerps("#711d12", "#59e7d2", count);
}
}
function GetLerps(color1, color2, count) {
const colors = [];
for (let i = 0; i < count; i++) {
const percent = i / count;
colors.push(LerpColor(color1, color2, percent));
}
return colors;
}
function LerpColor(color1, color2, percent) {
const trueColor1 = HexToRgb(color1);
const trueColor2 = HexToRgb(color2);
const r = LerpNumber(trueColor1.r, trueColor2.r, percent);
const g = LerpNumber(trueColor1.g, trueColor2.g, percent);
const b = LerpNumber(trueColor1.b, trueColor2.b, percent);
return RgbToHex(r, g, b);
}
function LerpNumber(min, max, percent) {
return min + (max - min) * percent;
}
function HexToRgb(hex) {
// Remove the hash if it's present
hex = hex.replace(/^#/, '');
// Parse the hex values
const r = parseInt(hex.slice(0, 2), 16) / 255;
const g = parseInt(hex.slice(2, 4), 16) / 255;
const b = parseInt(hex.slice(4, 6), 16) / 255;
return { r, g, b };
}
function RgbToHex(r, g, b) {
const toHex = (x) => {
const hex = Math.round(x * 255).toString(16);
return hex.length === 1 ? '0' + hex : hex;
};
return '#' + toHex(r) + toHex(g) + toHex(b);
}
function GetPageElements() {
let allElements = document.getElementsByTagName('*');
const elements = [];
const gamesData = [];
for (let element of allElements) {
if (element.hasAttribute(dataAtr)) {
const id = element.getAttribute(dataAtr);
elements.push(element);
const match = gamesData.find((value) => value.id == id);
if (match == null)
gamesData.push({ id: id, count: 1 });
else
match.count++;
}
}
return {
elements,
gamesData
};
}
})();