forked from Owyn/Universal_Dark_Theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uni_dark_theme.user.js
214 lines (205 loc) · 9.11 KB
/
uni_dark_theme.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
// ==UserScript==
// @name Universal Dark Theme Maker
// @namespace uni_dark_theme
// @version 1.19
// @description Simple Dark Theme style for any website which you can configure per-site
// @downloadURL https://github.com/Owyn/Universal_Dark_Theme/raw/master/uni_dark_theme.user.js
// @updateURL https://github.com/Owyn/Universal_Dark_Theme/raw/master/uni_dark_theme.user.js
// @supportURL https://github.com/Owyn/Universal_Dark_Theme/issues
// @homepage https://github.com/Owyn/Universal_Dark_Theme
// @icon https://images2.imgbox.com/b3/67/Aq5XazuW_o.png
// @author Owyn
// @match *://*/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addElement
// @grant GM_registerMenuCommand
// @grant unsafeWindow
// @sandbox JavaScript
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
var el;
var css;
var cfg_color;
var cfg_bgclr;
var cfg_bgimg;
var cfg_visclr;
var cfg_excl;
var cfg_css;
var cfg_js;
var cfg_active;
switch(localStorage.getItem('active'))
{
case "true": // back-compatibility
case "1":
cfg_active = true;
break;
default:
cfg_active = false;
break;
}
function load_settings()
{
cfg_excl = localStorage.getItem('excl') || "";
cfg_css = localStorage.getItem('css') || "";
cfg_js = localStorage.getItem('js') || "";
cfg_bgimg = (localStorage.getItem('bgimg') === '1');
if (typeof GM_getValue !== "undefined")
{
cfg_color = GM_getValue("Color", "#c0c0c0");
cfg_bgclr = GM_getValue("bgColor", "#2e2e2e");
cfg_visclr = GM_getValue("visitedColor", "#a4a4a4");
}
}
function activate(yes, prev_active)
{
if(prev_active && el){console.info("Removing dark style..."); el.remove();}
if(yes)
{
make_css();
console.info("adding dark style...");
el = addStyle(css);
console.info(el);
if(cfg_js){eval(cfg_js);}
}
}
function toggleDT()
{
load_settings();
cfg_active = !cfg_active;
activate(cfg_active, !cfg_active);
if(!cfg_active)
{
localStorage.removeItem('active');
}
else
{
localStorage.setItem('active', "1");
}
}
if (typeof GM_registerMenuCommand !== "undefined")
{
GM_registerMenuCommand("Dark Theme Configuration", cfg, "D");
GM_registerMenuCommand("Toggle Dark Theme", toggleDT, "T");
}
function make_css()
{
let exclusions;
let exc_txt = ""
if(cfg_excl !== "")
{
exclusions = cfg_excl.split(",");
for (var i = 0, len = exclusions.length; i < len; i++)
{
exc_txt += ":not("+exclusions[i]+")";
}
}
let bgimg_txt = cfg_bgimg ? "-color" : "";
////////////// Main thing, the style!:
css = `
*`+exc_txt+` {
color: `+cfg_color+` !important;
background`+bgimg_txt+`: `+cfg_bgclr+` !important;
border-color: `+cfg_color+` !important;
color-scheme: dark;
}
:visited`+exc_txt+`, a:hover`+exc_txt+` {
color: `+cfg_visclr+` !important;
}
input:focus,textarea:focus,select:focus`+exc_txt+`{
outline: 1px solid `+cfg_visclr+` !important;
}
`+cfg_css+`
`;
//////////////
}
function addStyle(aCss)
{
return GM_addElement(document.documentElement, 'style', {textContent: aCss});
/*let style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.textContent = aCss;
document.documentElement.appendChild(style);
return style;*/
};
if(cfg_active)
{
console.info("Adding dark style...");
load_settings();
make_css();
el = addStyle(css);
console.info(unsafeWindow);
console.info(el);
window.addEventListener("DOMContentLoaded", function(){ el = document.documentElement.appendChild(el); if(cfg_js){eval(cfg_js);} }); // make sure style element is at the bottom & execute custom JS
}
var t;
function cfg()
{
if (typeof GM_setValue !== "undefined")
{
function saveCfg()
{
GM_setValue("Color", document.getElementById("color").value);
GM_setValue("bgColor", document.getElementById("bgclr").value);
GM_setValue("visitedColor", document.getElementById("visitedColor").value);
localStorage.setItem('excl', document.getElementById("excl").value);
localStorage.setItem('css', document.getElementById("css").value);
localStorage.setItem('js', document.getElementById("js").value);
localStorage.setItem('active', document.getElementById("active").checked ? "1" : "0");
localStorage.setItem('bgimg', document.getElementById("bgimg").checked ? "1" : "0");
// pretty text "saved"
document.getElementById("cfg_save").value = "SAVED !";
clearTimeout(t);
t = setTimeout(function() {document.getElementById("cfg_save").value = "Save configuration";},1500)
// update active configuration
cfg_color = document.getElementById("color").value;
cfg_bgclr = document.getElementById("bgclr").value;
cfg_bgimg = document.getElementById("bgimg").checked;
cfg_visclr = document.getElementById("visitedColor").value;
cfg_excl = document.getElementById("excl").value;
cfg_css = document.getElementById("css").value;
cfg_js = document.getElementById("js").value;
activate(document.getElementById("active").checked, cfg_active );
cfg_active = document.getElementById("active").checked;
// clean up
if(!document.getElementById("active").checked) { localStorage.removeItem('active'); }
if(!document.getElementById("bgimg").checked) { localStorage.removeItem('bgimg'); }
if(!document.getElementById("excl").value) { localStorage.removeItem('excl'); }
if(!document.getElementById("css").value) { localStorage.removeItem('css'); }
if(!document.getElementById("js").value) { localStorage.removeItem('js'); }
}
load_settings();
var div = document.createElement("div");
div.style = "margin: auto; width: fit-content; height: fit-content; border: 1px solid black; color: "+cfg_color+"; background: "+cfg_bgclr+"; position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 8888888; line-height: 1;";
div.innerHTML = "<b><br><center>Configuration</center></b>"
+ "<div style='margin: auto; display: table;'><br><input id='color' type='text' size='7' style='display:inline; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; width:initial; padding: initial; margin: initial;'> Text color (empty = site default)"
+ "<br><br><input id='bgclr' type='text' size='7' style='display:inline; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; width:initial; padding: initial; margin: initial;'> Background color"
+ "<br><br><input id='visitedColor' type='text' size='7' style='display:inline; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; width:initial; padding: initial; margin: initial;'> <a href='' onclick='return false;'>Visited & hovered links color</a>"
+ "<br><br></div><center><b>Per-site settings (stored in browser cookies called LocalStorage):</b>"
+ "<br><br><input id='active' type='checkbox' style='display:inline; width:initial; padding: initial; margin: initial;'> Enabled for this website"
+ "<br><br><input id='bgimg' type='checkbox' style='display:inline; width:initial; padding: initial; margin: initial;'> Keep background-images"
+ "<br><br>Excluded css elements (e.g. \"#id1,.class2,input\"):<br><textarea id='excl' style='margin: 0px; width: 400px; height: 50px; resize:both; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; display:inline; padding: initial; margin: initial; min-height: initial;'></textarea>"
+ "<br><br>Custom CSS style:<br><textarea id='css' style='margin: 0px; width: 400px; height: 50px; resize:both; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; display:inline; padding: initial; margin: initial; min-height: initial;'></textarea>"
+ "<br><br>Custom JS Action:<br><textarea id='js' style='margin: 0px; width: 400px; height: 50px; resize:both; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; display:inline; padding: initial; margin: initial; min-height: initial;'></textarea>"
+ "<br><input id='cfg_save' type='button' value='Save configuration' style='display:inline; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; width:initial; padding: initial; margin: initial;'> <input id='cfg_close' type='button' value='Close' style='display:inline; color: "+cfg_color+"; background-color: "+cfg_bgclr+"; width:initial; padding: initial; margin: initial;'></center>";
document.body.appendChild(div);
document.getElementById("color").value = cfg_color;
document.getElementById("bgclr").value = cfg_bgclr;
document.getElementById("bgimg").checked = cfg_bgimg;
document.getElementById("visitedColor").value = cfg_visclr;
//
document.getElementById("active").checked = cfg_active;
document.getElementById("excl").value = cfg_excl;
document.getElementById("css").value = cfg_css;
document.getElementById("js").value = cfg_js;
document.getElementById("cfg_save").addEventListener("click", saveCfg, true);
document.getElementById("cfg_close").addEventListener("click", function(){div.remove();clearTimeout(t);}, true);
}
else
{
alert("Sorry, Chrome userscripts in native mode can't have configurations! Install TamperMonkey userscript-manager extension");
}
}
})();