-
Notifications
You must be signed in to change notification settings - Fork 0
/
themes.user.js
117 lines (103 loc) · 3.45 KB
/
themes.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
// ==UserScript==
// @name AskfmForHumans/themes
// @name:ru AskfmForHumans/themes
// @version 1.2.0
// @namespace https://github.com/AskfmForHumans
// @author https://github.com/AskfmForHumans
// @homepage https://afh.snowwm.ml/userjs/themes
// @license MIT
//
// @description Restore the choice from 18 color themes
// @description:ru Возвращает выбор из 18 цветовых тем
//
// @match https://ask.fm/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// @grant GM_getResourceText
// @grant GM_getResourceURL
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @resource style https://github.com/AskfmForHumans/user.js/raw/main/themes.css
// @resource icon https://github.com/AskfmForHumans/user.js/raw/main/icon-owl.ico
// ==/UserScript==
(function () {
"use strict"
const LOG_PREFIX = "afh/themes:"
const THEME_NAMES = [ // by WandAlKa
"[Без темы]",
"Томатная жижа",
"Голубой вагон",
"Хмурь",
"Глухая чаща",
"Кровавое месиво",
"Сумеречная зона",
"Фиалка в горшочке",
"Барби",
"Мёртвый розовый",
"Стервозный розовый",
"Shitty Life",
"Гематома",
"Стратосфера",
"Морфей",
"Басейн",
"Болотная водоросль",
"Фисташка",
"Тенистый мох",
]
const menuItems = []
GM_addStyle(GM_getResourceText("style"))
update()
// Re-apply when a new body is created (on URL change).
const observer = new MutationObserver(() => update())
observer.observe(document.documentElement, {
childList: true,
})
function update(themeId) {
if (themeId !== undefined) {
GM_setValue("afh-askfm-theme", themeId)
} else {
themeId = GM_getValue("afh-askfm-theme") || 0
}
rebuildMenu(themeId)
applyTheme(themeId)
}
function rebuildMenu(currentTheme) {
// clear current items
for (const i of menuItems) {
GM_unregisterMenuCommand(i)
}
menuItems.length = 0
for (const [id, name] of THEME_NAMES.entries()) {
const text = (id === currentTheme ? `${id}. ${name} ✔️` : `${id}. ${name}`)
const cmd = GM_registerMenuCommand(text, () => update(id))
menuItems.push(cmd)
}
}
function applyTheme(currentTheme) {
console.info(LOG_PREFIX, `apply ${currentTheme}`)
for (const i of THEME_NAMES.keys()) {
document.body.classList.remove(`theme-${i}`)
}
if (currentTheme > 0) {
setIcon(true)
document.body.classList.add(`theme-${currentTheme - 1}`)
} else {
setIcon(false)
}
}
function setIcon(enabled) {
let iconElem = document.getElementById("afh-icon")
if (!iconElem) {
iconElem = document.createElement("link")
iconElem.id = "afh-icon"
iconElem.rel = "icon"
document.head.appendChild(iconElem)
}
if (enabled) {
iconElem.href = GM_getResourceURL("icon")
} else {
iconElem.href = "/favicon.ico"
}
}
})()