This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invert-all-media.js
155 lines (139 loc) · 6.61 KB
/
invert-all-media.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
// ==UserScript==
// @name Dynamic Invert All Media Script
// @namespace Violentmonkey Scripts
// @author yzrsng
// @description Userscript for Invert Rendering. 背景画像を含むメディアを反転させるスクリプト。
// @version 0.1
// @include http://*
// @include https://*
// @match http://*
// @match https://*
// @grant none
// ==/UserScript==
(function() {
'use strict';
/**
* # TODO
*
* デスクトップブラウザでiframeの中身に巡回するのを防ぐ
* サイト側のCSS切り替えに対応 observer
*/
// remove build in filter for Invert mode on Yuzu Browser
const yuzubrowserInvertModeStyle = document.getElementById('yuzubrowser_invert_mode');
if (yuzubrowserInvertModeStyle !== null) {
yuzubrowserInvertModeStyle.parentNode.removeChild(yuzubrowserInvertModeStyle);
}
const patrolledClass = "invertPatrolledElement-yz";
const invertClassRoot = "invertedRootElement-yz";
const invertClassChild = "invertedChildElement-yz";
const myCss = document.createElement('style');
myCss.type = "text/css";
myCss.id = 'dynamic_invert_media_filter';
myCss.insertAdjacentHTML('beforeend', 'iframe[src*="embed"]:not(.' + invertClassChild + '),');
myCss.insertAdjacentHTML('beforeend', 'iframe[data-src*="embed"]:not(.' + invertClassChild + '),');
myCss.insertAdjacentHTML('beforeend', 'img:not(.' + invertClassChild + '),');
myCss.insertAdjacentHTML('beforeend', 'embed:not(.' + invertClassChild + '),');
myCss.insertAdjacentHTML('beforeend', 'video:not(.' + invertClassChild + '),');
myCss.insertAdjacentHTML('beforeend', 'canvas:not(.' + invertClassChild + '),');
myCss.insertAdjacentHTML('beforeend', '.' + invertClassRoot + '{filter:invert(1)}');
document.getElementsByTagName('head')[0].appendChild(myCss);
const invertExistingFilterClass = "invertedFilterElement-yz-";
let invertExistingFilterCount = 1;
const judgeBgColorClassWhite = "judgeBgColor-yz-white";
const judgeBgColorClassBlack = "judgeBgColor-yz-Black";
myCss.insertAdjacentHTML('beforeend', '.' + judgeBgColorClassWhite + '{background-color:rgba(255, 255, 255, 0.75) !important}');
myCss.insertAdjacentHTML('beforeend', '.' + judgeBgColorClassBlack + '{background-color:rgba(0, 0, 0, 0.75) !important}');
const grantedBgColorClass = "grantBgColor-yz-";
let grantBgColorCount = 1;
myCss.insertAdjacentHTML('beforeend', '.' + grantedBgColorClass + "0" + '{background-color:white !important}');
// function printElms(elms) {
// console.log(elms);
// console.log(elms.length);
// }
// function judgeBgColor(elm) {
// const elmStyle = window.getComputedStyle(elm);
// const elmColor = elmStyle.getPropertyValue("color");
// const posNumber = elmColor.indexOf("(");
// const elmClrValue = elmColor.substring(posNumber+1,elmColor.length-1);
// const clrArray = elmClrValue.split(', ');
// for (let i = 0; i < clrArray.length; i++) {
// clrArray[i] = parseInt(clrArray[i], 10);
// }
// const judgePoint = clrArray[0]*9 + clrArray[1]*13 + clrArray[2]*8;
// if (judgePoint < 3830) {
// elm.classList.add(judgeBgColorClassWhite);
// } else {
// elm.classList.add(judgeBgColorClassWhite);
// }
// }
function returnParentsBgColor(elm) {
const parentElmStyle = window.getComputedStyle(elm.parentNode);
const parentElmBgColor = parentElmStyle.getPropertyValue("background-color");
if (parentElmBgColor === "rgba(0, 0, 0, 0)") {
if (elm.parentNode.tagName === "HTML") {
return "rgb(0, 0, 0)";
}
return returnParentsBgColor(elm.parentNode);
}
return parentElmBgColor;
}
function markChildElms(elms) {
for (let i = 0; i < elms.length; i++) {
elms[i].classList.add(patrolledClass);
elms[i].classList.add(invertClassChild);
markChildElms(elms[i].children);
}
}
function invertRootElms(elms) {
for (let i = 0; i < elms.length; i++) {
if (elms[i].classList.contains(patrolledClass)) {
continue;
}
elms[i].classList.add(patrolledClass);
if (elms[i].tagName !== "HTML") {
if (elms[i].parentNode.classList.contains(invertClassChild)) {
elms[i].classList.add(invertClassChild);
markChildElms(elms[i].children);
continue;
}
}
const ssStyle = window.getComputedStyle(elms[i]);
const ssStyleBefore = window.getComputedStyle(elms[i], '::before');
const ssStyleAfter = window.getComputedStyle(elms[i], '::after');
if (ssStyle.getPropertyValue('background-image').match(/url\(/) || ssStyleBefore.getPropertyValue('background-image').match(/url\(/) || ssStyleAfter.getPropertyValue('background-image').match(/url\(/) || elms[i].tagName === 'IMG' || elms[i].tagName === 'EMBED' || elms[i].tagName === 'VIDEO' || elms[i].tagName === 'CANVAS') {
// if (ssStyle.getPropertyValue('background-repeat').match(/-/) || ssStyle.getPropertyValue('background-repeat').match(/space/)) {
// // judgeBgColor(elms[i]);
// elms[i].classList.add(grantedBgColorClass + grantBgColorCount);
// myCss.insertAdjacentHTML('beforeend', "." + grantedBgColorClass + grantBgColorCount + "{background-color:" + returnParentsBgColor(elms[i]) + " !important}");
// grantBgColorCount++;
// }
if (ssStyle.getPropertyValue('background-color') === "rgba(0, 0, 0, 0)" && elms[i].childNodes.length !== 0) {
// judgeBgColor(elms[i]);
elms[i].classList.add(grantedBgColorClass + grantBgColorCount);
myCss.insertAdjacentHTML('beforeend', "." + grantedBgColorClass + grantBgColorCount + "{background-color:" + returnParentsBgColor(elms[i]) + " !important}");
grantBgColorCount++;
}
const filterValue = ssStyle.getPropertyValue('filter');
if (filterValue !== "none" && filterValue !== "invert(1)") {
elms[i].classList.add(invertExistingFilterClass + invertExistingFilterCount);
myCss.insertAdjacentHTML('beforeend', "." + invertExistingFilterClass + invertExistingFilterCount + "{filter:" + filterValue + " invert(1)" + "}");
invertExistingFilterCount++;
} else {
elms[i].classList.add(invertClassRoot);
}
if (elms[i].children.length === 0) {
continue;
}
markChildElms(elms[i].children);
}
}
}
const myElms = document.getElementsByTagName('*');
if (window.getComputedStyle(myElms[0]).getPropertyValue('background-color') === "rgba(0, 0, 0, 0)") {
myCss.insertAdjacentHTML('beforeend', "html{background-color:white}");
}
// invertRootElms(myElms);
const id = setInterval(function(){
invertRootElms(myElms);
}, 2000);
})();