-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiddy.user.js
398 lines (304 loc) · 9.05 KB
/
giddy.user.js
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
// ==UserScript==
// @name Giddy
// @description apply css to your github pages!
// @version test
// @namespace https://github.com/macimas
// @author macimas (https://macimas.github.io)
// @match https://github.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @run-at document-start
// @grant unsafeWindow
// @grant window.onurlchange
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// ==/UserScript==
function getCookieCrumb(id, options = {}) {
let value = document.cookie
.split("; ")
.find((crumb) => crumb.startsWith(id + "="))
?.split("=")[1];
if (!value) return null;
value = decodeURIComponent(value);
if (options.json) {
value = JSON.parse(value);
}
return value;
}
function htemplate(layout) {
let node_count = 0;
function createNode(tag, attributes, layout) {
const node = document.createElement(tag);
for (const attribute in attributes) {
const value = attributes[attribute];
if (attribute == "it") {
value(node);
continue;
}
node[attribute] = value;
}
if (layout) {
node.append(...createNodes(layout));
}
node_count++;
return node;
}
function createNodes(layout) {
const nodes = [];
for (const item of layout) {
if (typeof item == "string") {
nodes.push(item);
}
if (Array.isArray(item)) {
const node = createNode(...item);
nodes.push(node);
}
}
return nodes;
}
const nodes = createNodes(layout);
const result = (nodes.length == 1)
? nodes[0] || null
: nodes;
//console.log(`created ${node_count} nodes`, result);
return result;
}
/*
==
*/
// TODO: change classes when switching mode and theme
const html = document.documentElement;
let html_classes = [];
function addHelperClasses() {
/*
.dark-mode Dark mode
.theme-dark Dark default
.theme-dark-high-contrast Dark high contrast
.theme-dark-colorblind Dark Protanopia & Deuteranopia
.theme-dark-tritanopia Dark Tritanopia
.theme-dark-dimmed Dark dimmed
.light-mode Light mode
.theme-light Light default
.theme-light-high-contrast Light high contrast
.theme-light-colorblind Light Protanopia & Deuteranopia
.theme-light-tritanopia Light Tritanopia
*/
const data = getCookieCrumb("color_mode", { json: true });
const prefers_dark = (getCookieCrumb("preferred_color_mode") == "dark");
const color_mode = data.color_mode;
let theme;
const classes = [];
if ((color_mode == "auto" && prefers_dark) || (color_mode == "dark")) {
theme = "dark_theme";
}
if ((color_mode == "auto" && !prefers_dark) || (color_mode == "light")) {
theme = "light_theme";
}
classes.push(data[theme].color_mode + "-mode");
classes.push("theme-" + data[theme].name.replaceAll("_", "-"));
console.log(classes);
html.classList.add(...classes);
}
function observeTheme(list) {
const attributes = [
"data-color-mode",
"data-dark-theme",
"data-light-theme"
];
for (const item of list) {
const attribute = item.attributeName;
if (attribute == "class") {
if (html.attributes.class?.value) return;
addHelperClasses();
return;
}
}
}
new MutationObserver(observeTheme)
.observe(document.documentElement, { attributeOldValue: true });
addHelperClasses()
unsafeWindow.addHelperClasses = addHelperClasses;
/*
==
*/
const profile_css = htemplate([
["style", {
id: "giddy_profile_css"
}]
]);
const general_css = htemplate([
["style", {
id: "giddy_general_css",
innerText: (`
footer.footer
{ position: relative }
footer.footer::before {
content: "";
z-index: -1;
position: absolute;
top: 14px;
bottom: 0;
left: 0;
right: 0;
background: var(--bgColor-default);
border-top: var(--borderWidth-thin) solid var(--borderColor-default);
}
/* overview */
.graph-before-activity-overview
{ background: var(--bgColor-default) }
.graph-before-activity-overview
{ border-radius: var(--borderRadius-medium) }
.js-yearly-contributions:has(.activity-overview-box) .graph-before-activity-overview {
border-bottom-left-radius: unset;
border-bottom-right-radius: unset;
}
.js-profile-timeline-year-list {
background-color: unset !important;
}
.filter-list.small {
overflow: hidden;
border: var(--borderWidth-thin) solid var(--borderColor-default);
border-radius: var(--borderRadius-medium) !important;
}
.filter-list.small li:last-child .filter-item
{ border-bottom: unset !important }
.filter-list.small .filter-item {
margin-bottom: unset !important;
border-radius: unset;
border-bottom: var(--borderWidth-thin) solid var(--borderColor-default);
}
.filter-list.small .filter-item:not(.selected, :hover) {
background: var(--bgColor-default);
}
.filter-list.small .filter-item.selected {
border-color: var(--bgColor-default);
}
.contribution-activity-listing {
width: 100%;
padding: 16px;
background: var(--bgColor-default);
border: var(--borderWidth-thin) solid var(--borderColor-default);
border-bottom: unset;
}
.contribution-activity-listing:nth-child(3) {
border-top-left-radius: var(--borderRadius-medium);
border-top-right-radius: var(--borderRadius-medium);
}
.contribution-activity-listing:last-of-type {
margin-bottom: 8px;
border-bottom: var(--borderWidth-thin) solid var(--borderColor-default);
border-bottom-left-radius: var(--borderRadius-medium);
border-bottom-right-radius: var(--borderRadius-medium);
}
.contribution-activity-listing > div {
padding: unset !important;
padding-right: 16px !important;
}
.contribution-activity-listing > div > h3 {
position: relative;
border-bottom: unset !important;
font-size: 16px !important;
padding-left: 40px;
}
.contribution-activity-show-more {
border-radius: var(--borderRadius-medium) !important;
}
.contribution-activity-listing > div > h3 span
{ padding: unset !important }
.TimelineItem:last-child::before
{ height: 18px }
/* repositories */
#user-profile-frame > div > div:first-child
{ border-bottom: unset !important }
#user-repositories-list {
background: var(--bgColor-default);
border: var(--borderWidth-thin) solid var(--borderColor-default);
border-radius: var(--borderRadius-medium);
}
#user-repositories-list li
{ padding: 16px !important }
#user-repositories-list li:last-child
{ border-bottom: unset !important }
#user-repositories-list .paginate-container
{ display: none }
/* stars */
.blankslate-spacious {
background: var(--bgColor-default) !important;
border-radius: var(--borderRadius-medium) !important;
}
.col-lg-12 > div.d-block {
padding: 16px !important;
background: var(--bgColor-default);
border: var(--borderWidth-thin) solid var(--borderColor-default);
}
.col-lg-12 > div.d-block:nth-child(3) {
margin-top: 20px;
border-top-left-radius: var(--borderRadius-medium);
border-top-right-radius: var(--borderRadius-medium);
}
.col-lg-12 > div.d-block:last-of-type {
border-bottom-left-radius: var(--borderRadius-medium);
border-bottom-right-radius: var(--borderRadius-medium);
}
.col-lg-12 > div.d-block:not(:last-of-type) {
border-bottom: unset !important;
}
`)
}]
]);
// general_css.innerText = null;
document.documentElement.append(profile_css);
document.documentElement.append(general_css);
let giddy_profile_css_enabled = JSON.parse(localStorage.giddy_profile_css_enabled || null) ?? true;
let last_username;
function toggleGiddy() {
giddy_profile_css_enabled = !giddy_profile_css_enabled;
localStorage.giddy_profile_css_enabled = giddy_profile_css_enabled;
buildMenu();
}
function buildMenu() {
const text = (giddy_profile_css_enabled) ? "Disable" : "Enable"
GM_unregisterMenuCommand("toggle_giddy");
GM_registerMenuCommand(`${text} profile CSS`, toggleGiddy, {
id: "toggle_giddy"
});
if (giddy_profile_css_enabled) {
last_username = null;
addProfileCSS();
} else {
profile_css.innerHTML = null;
}
}
async function addProfileCSS(event) {
if (!giddy_profile_css_enabled) return;
const is_profile_page = !!document.querySelector("body.page-profile");
const username = location.pathname.slice(1);
if (!is_profile_page || username.includes("/")) {
profile_css.innerHTML = null;
last_username = null;
return;
}
if (last_username == username) return;
last_username = username;
profile_css.innerHTML = null;
const fetch_data_url = `https://raw.githubusercontent.com/macimas/giddy/main/index/${username}`;
const fetch_data = await fetch(fetch_data_url).then((res) => {
if (res.status == 404) console.log(username, "does not have profile css");
return res;
});
if (!fetch_data.ok) return;
const data = (await fetch_data.text()).split("\n");
const hash = data[0];
const fetch_base_url = `https://raw.githubusercontent.com/${username}/${username}/${hash}/`;
const fetch_css = await fetch(fetch_base_url + "giddy.css").then((res) => {
if (res.status == 404) console.log(`something stupid is going on`);
return res;
});
if (!fetch_css.ok) return;
let css = (await fetch_css.text())
.replace(/(url\(["']?)\.\//g, "$1" + fetch_base_url);
profile_css.innerHTML = css;
console.log(username, "has profile css");
}
new MutationObserver(addProfileCSS)
.observe(document.body, { attributes: true });
buildMenu();