-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathre_inverter.js
395 lines (357 loc) · 12.1 KB
/
re_inverter.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
// ==UserScript==
// @name cool_stuff_bro
// @namespace http://userstyles.org
// @description for real this is the best thing ever.
// @include about:privatebrowsing
// @include about:newtab
// @author asp
// @run-at document-start
// @match *://*/*
// @version 0.20171223121727
// ==/UserScript==
/*
to do:
toggle background color on figure,... elements
hide be done on a domain bases
*/
var global_turn_off;
var global_hide;
var global_hue_rotate;
chrome.storage.sync.get("global_hide", function (key_value) {
global_hide = key_value["global_hide"];
});
chrome.storage.sync.get("global_turn_off", function (key_value) {
global_turn_off = key_value["global_turn_off"];
});
chrome.storage.sync.get("global_hue_rotate", function (key_value) {
global_hue_rotate = key_value["global_hue_rotate"];
});
var can_set_global_hide = false; //for hiding/removing elements
var can_set_global_hue_rotate = false; //for setting global hue_rotate
var style_element;
var id = "injected_by_cool_stuff_bro";
var html_style_element;
var html_id = "injected_by_cool_stuff_bro_html";
var youtube_style_element;
var youtube_id = "injected_by_cool_stuff_bro_for_youtube";
var youtube_css = [
".html5-video-player:not(.ytp-transparent), .html5-video-player.ad-interrupting, .html5-video-player.ended-mode, .html5-video-player.ytp-fullscreen",
"{",
"background-color: #fff !important;",
"}",
".ytp-title-text, .ytp-left-controls, .captions-text, .ytp-popup.ytp-settings-menu",
"{",
"filter:invert(1) hue-rotate(" + global_hue_rotate + "deg);",
"}",
"div#caption-window-1",
"{",
"background: none !important",
"}"
].join("\n");
var toggle = true;
var html_toggle = false;
var youtube_toggle = true;
document.addEventListener('keydown', switching);
/*
* function switching(evt){
* console.log(evt.keyCode);
* }*/
//////////////insert
function insert_css(modifier, inversion_type) {
var css = [
modifier,
"{",
inversion_type.slice(0,-1) + " !important;",
"}"
].join("\n");
style_element = document.createElement("style");
style_element.type = "text/css";
style_element.id = id;
style_element.appendChild(document.createTextNode(css));
document.documentElement.appendChild(style_element);
/* var heads = document.getElementsByTagName("head");
* if (heads.length > 0) {
* heads[0].appendChild(style_element);
} else {
// no head yet, stick it whereever
} */
}
function insert_css_html(modifier, inversion_type) {
var css = [
modifier,
"{",
inversion_type.slice(0,-1) + " !important;",//to override all others
"}"
].join("\n");
html_style_element = document.createElement("style");
html_style_element.type = "text/css";
html_style_element.id = html_id;
html_style_element.appendChild(document.createTextNode(css));
document.documentElement.appendChild(html_style_element);
}
function insert_youtube_css() {
youtube_style_element = document.createElement("style");
youtube_style_element.type = "text/css";
youtube_style_element.id = youtube_id;
youtube_style_element.appendChild(document.createTextNode(youtube_css));
var heads = document.getElementsByTagName("head");
document.documentElement.appendChild(youtube_style_element);
}
function insert_youtube_del_css() {
var youtube_style_del_element = document.createElement("style");
var youtube_del_css = [".ytp-big-mode .ytp-gradient-top, .ytp-big-mode .ytp-gradient-bottom, .ytp-gradient-top, .ytp-gradient-bottom",
"{",
"background-image: none !important;",
"}"].join("\n");
var youtube_del_id = "injected_by_cool_stuff_bro_for_youtube_del";
youtube_style_del_element.type = "text/css";
youtube_style_del_element.id = youtube_del_id;
youtube_style_del_element.appendChild(document.createTextNode(youtube_del_css));
var heads = document.getElementsByTagName("head");
document.documentElement.appendChild(youtube_style_del_element);
}
//////////replace
function replace_css(modifier, inversion_type) {
var css = [
modifier,
"{",
inversion_type.slice(0,-1) + " !important;",
"}"
].join("\n");
var textnode = (document.createTextNode(css));
style_element.replaceChild(textnode, style_element.childNodes[0]);
}
function replace_css_html(modifier, inversion_type) {
var css = [
modifier,
"{",
inversion_type.slice(0,-1) + " !important;",
"}"
].join("\n");
var textnode = (document.createTextNode(css));
html_style_element.replaceChild(textnode, html_style_element.childNodes[0]);
}
function replace_youtube_css(toggle_youtube) {
//youtube_style_element.innerText === ""
var css = toggle_youtube ? youtube_css : [
"div#caption-window-1",
"{",
"background: none !important",
"}"
].join("\n");
var textnode = (document.createTextNode(css));
youtube_style_element.replaceChild(textnode, youtube_style_element.childNodes[0]);
}
/*
* function edit_style(inversion_type) {
* var elts = document.querySelectorAll("*[style]");
* for (var elt of elts) {
* elt.style.cssText += inversion_type;
* }
* }
* /*edit_style("filter: hue-rotate(" +global_hue_rotate + "deg) invert(100%)");
*/
/*list of other modifiers
* 'img:not([src$=".png"]):not([src$=".gif"]):not([src$=".PNG"]):not([src$=".GIF"]),[style*=".jpg"],[style*=".JPG"],[style*=".jpeg"],[style*=".JPEG"],canvas,embed,object,video';//for other
* 'img,[style*=".jpg"],[style*=".JPG"],[style*=".jpeg"],[style*=".JPEG"],video,iframe,canvas,embed,object';
*/
var modifier = 'img,video,iframe,canvas,object,svg';//svg
var url = new URL(document.URL);
var domain = url.hostname;
chrome.storage.sync.get(domain, function (domain_settings) {
if (global_turn_off) {
toggle = !toggle;
insert_css(modifier, "filter:hue-rotate(0deg) invert(0);"); //for keep inverted
}
else {
if (global_hide) {
insert_css(modifier, "filter: opacity(0);");
}
else {
if(domain_settings[domain] == true){ //using isEmpty in firefox code
toggle = !toggle;
insert_css(modifier, "filter:hue-rotate(0deg) invert(0);"); //for keep inverted
}
else{
console.log(domain_settings)
insert_css(modifier, "filter:hue-rotate(" + global_hue_rotate + "deg) invert(100%);");
}
//this is to emeplement domain specific blackListing and hidiing.
// if (isEmpty(domain_settings)) { //if not in black list
// insert_css(modifier, "filter:hue-rotate(" + global_hue_rotate + "deg) invert(100%);"); //for re-inverted
// } else {
// if (!isEmpty(domain_settings.blackList)) {
// toggle = !toggle;
// insert_css(modifier, "filter:hue-rotate(0deg) invert(0);"); //for keep inverted
// }
// if (!isEmpty(domain_settings.hide)) {
// toggle = !toggle;
// insert_css(modifier, "filter: opacity(0);"); //for keep inverted
// }
}
}
});
//insert_css_html("body", ""); //for html //currenty goes to body
insert_youtube_css(youtube_toggle); //for youtube
insert_youtube_del_css(); //del youtube ugly white bottom bar
function switching(evt) {
var inversion_type;
//var can_set_global_hide; global variable
//var modifier = 'img,video,iframe,canvas,object'; defined as global variable
evt = evt || window.event;
var charCode = evt.keyCode || evt.which;
var altPressed,
ctrlPressed;
altPressed = evt.altKey;
ctrlPressed = evt.ctrlKey;
if (altPressed && ctrlPressed) {
switch (charCode) {
case 90: //'z':
//to toggle invert
modifier = 'img,video,iframe,canvas,object,svg';//svg
inversion_type = toggle ? "filter:hue-rotate(0deg) invert(0);" : "filter:hue-rotate(" + global_hue_rotate + "deg) invert(100%);";
can_set_global_hide = false;
toggle = !toggle;
replace_css(modifier, inversion_type);
replace_youtube_css(toggle);
break;
case 89: //'y'
//ttoogle youtube
youtube_toggle=! youtube_toggle;
replace_youtube_css(youtube_toggle);
break;
case 65: //'a'
//to invert only img
modifier = "img,svg";//svg
can_set_global_hide = false;
toggle = true;
inversion_type = "filter: hue-rotate(" + global_hue_rotate + "deg) invert(100%);";
replace_css(modifier, inversion_type);
replace_youtube_css(true);
break;
case 83: //'s':
//to invert only video
modifier = 'video,iframe';
can_set_global_hide = false;
toggle = true;
inversion_type = "filter:hue-rotate(" + global_hue_rotate + "deg) invert(100%);";
replace_css(modifier, inversion_type);
replace_youtube_css(true);
break;
case 85: //u
//to toggle pdf
inversion_type = html_toggle ? "filter:hue-rotate(0deg) invert(0);" : "filter:hue-rotate(" + global_hue_rotate + "deg) invert(100%);";
html_toggle = !html_toggle;
replace_css_html("embed", inversion_type);
break;
//
// global settings below
//
case 68: //'d':
//toggle hide, set global_hide on double triggering hotkey
inversion_type = "filter: opacity(0);";
replace_css(modifier, inversion_type); //removes last inverted (modifier is global)
if (can_set_global_hide) {
chrome.storage.sync.get("global_hide", function (key_value) {
var new_bool = !key_value["global_hide"];
chrome.storage.sync.set({
"global_hide": new_bool
}, function () {
alert('global hide is now ' + new_bool);
});
});
}
can_set_global_hide = true;
break;
case 72: //h
//toggle global_hue_rotate, set global_hue_rotate on double click
var new_hue_rotate = prompt("new hue-rotate value from 0 to 180");
can_set_global_hue_rotate = confirm("Ok for global or cancel for page only"); //can set hue rotate when new hue rvalue is same
inversion_type = "filter:hue-rotate(" + new_hue_rotate + "deg) invert(100%);";
replace_css(modifier, inversion_type);
global_hue_rotate = new_hue_rotate;
if (can_set_global_hue_rotate) {
chrome.storage.sync.set({
"global_hue_rotate": global_hue_rotate
}, function () {
alert('global_hue_rotate is now ' + global_hue_rotate);
});
}
break;
case 69: //e
//toggle global_keep_inverted
chrome.storage.sync.get("global_turn_off", function (key_value) {
var new_bool = !key_value["global_turn_off"];
chrome.storage.sync.set({
"global_turn_off": new_bool
}, function () {
alert('turn off extension is now ' + new_bool);
});
});
break;
case 66: //b
//toggle memebership of domain in blacklist
var url = new URL(document.URL);
var domain = url.hostname;
chrome.storage.sync.get(domain, function (black_list_re_invert) {
var url = new URL(document.URL);
var domain = url.hostname;
if (isEmpty(black_list_re_invert)) { //if not in blacklist, enter blacklist
chrome.storage.sync.set({
[domain]: true
}, function () {
alert(domain + ' would NOT be re_inverted from now on.');
});
} else { //if in blacklist, remove from blacklist
chrome.storage.sync.remove(domain, function () {
alert(domain + ' would be re_inverted from now on.');
});
}
});
break;
//
// default
//
default:
break;
}
}
}
function isEmpty(obj) {
for (var x in obj) {
if (obj.hasOwnProperty(x))
return false;
}
return true;
}
/*
* case 72: //h
* // html toggle
* inversion_type = html_toggle? "": "background: rgb(255, 255, 255) !important; color: rgb(0, 0, 0) !important;";
* html_toggle = !html_toggle;
* replace_css_html("body", inversion_type);
* break;
*/
/*
* case 68: //'d':
* //to invert normal invert
* modifier = "img";
* toggle = false;
* inversion_type = "filter:hue-rotate(0deg) invert(100%);";
* replace_css(modifier, inversion_type);
* replace_youtube_css(true);
* break;
*/
/*
* case 65: //'a':
* //to youtube inversion
* replace_youtube_css();
* break;
*
* case 83: //'s':
* //to unreinvert
* modifier = 'img,[style*=".jpg"],[style*=".JPG"],[style*=".jpeg"],[style*=".JPEG"],canvas,embed,object,video';
* inversion_type = "";
* replace_css(modifier, inversion_type);
* break;
*/