Last active
May 10, 2019 00:13
-
-
Save jfmherokiller/24fc0f3eb58d491f5d1902fb8d4db88e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name BlackenPage | |
// @version 1.0.3 | |
// @match http://*/* | |
// @match https://*/* | |
// @grant GM_xmlhttpRequest | |
// @run-at document-start | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
window.addEventListener('load', () => { | |
addButton('blacken', selectReadFn); | |
}); | |
function addButton(text, onclick) { | |
let button = document.createElement('button'); | |
let btnStyle = button.style; | |
document.body.appendChild(button); | |
button.innerHTML = text; | |
button.onclick = onclick; | |
btnStyle.position = 'absolute'; | |
btnStyle.top = '10px'; | |
btnStyle.zIndex = '999'; | |
return button; | |
} | |
async function aaaa() { | |
function RGBtoHSL(RGBColor) { | |
const R = RGBColor[0]; | |
const G = RGBColor[1]; | |
const B = RGBColor[2]; | |
let H, S; | |
const cMax = Math.max(Math.max(R, G), B); | |
const cMin = Math.min(Math.min(R, G), B); | |
const sum = cMax + cMin; | |
const diff = cMax - cMin; | |
const L = sum >> 1; | |
if (cMax === cMin) { | |
S = 0; | |
H = 0; | |
} | |
else { | |
if (L <= 0.5) { | |
S = diff / sum; | |
} | |
else { | |
S = diff / (2 - sum); | |
} | |
const Rdelta = R / 6 / diff; | |
const Gdelta = G / 6 / diff; | |
const Bdelta = B / 6 / diff; | |
if (R === cMax) { | |
H = Gdelta - Bdelta; | |
} | |
else if (G === cMax) { | |
H = (1 / 3) + Bdelta - Rdelta; | |
} | |
else { | |
H = (2 / 3) + Rdelta - Gdelta; | |
} | |
if (H < 0) { | |
H += 1; | |
} | |
if (H > 1) { | |
H -= 1; | |
} | |
} | |
return [H, S, L]; | |
} | |
function getRGBColor(node, prop) { | |
const rgb = getComputedStyle(node, null).getPropertyValue(prop); | |
if (/rgb\((\d+),\s(\d+),\s(\d+)\)/.exec(rgb)) { | |
const r = parseInt(RegExp.$1, 10); | |
const g = parseInt(RegExp.$2, 10); | |
const b = parseInt(RegExp.$3, 10); | |
return [r / 255, g / 255, b / 255]; | |
} | |
return rgb; | |
} | |
function hslToCSS(hsl) { | |
const [hue, saturation, lightness] = hsl; | |
return `hsl(${Math.round(hue * 360)}, ${Math.round(saturation * 100)}%, ${Math.round(lightness * 100)}%)`; | |
} | |
const props = ["color", "background-color", "border-left-color", "border-right-color", "border-top-color", "border-bottom-color"]; | |
const props2 = ["color", "backgroundColor", "borderLeftColor", "borderRightColor", "borderTopColor", "borderBottomColor"]; | |
if (typeof getRGBColor(document.documentElement, "background-color") == "string") { | |
document.documentElement.style.backgroundColor = "white"; | |
} | |
revl(document.documentElement); | |
function revl(n) { | |
let i, x; | |
if (n.nodeType == Node.ELEMENT_NODE) { | |
for (i = 0; x = n.childNodes[i]; ++i) { | |
revl(x); | |
} | |
for (i = 0; x = props[i]; ++i) { | |
const color = getRGBColor(n, x); | |
if (typeof (color) != "string") { | |
const hsl = RGBtoHSL(color); | |
let lightness = hsl[2]; | |
lightness = 1 - lightness; | |
n.style[props2[i]] = hslToCSS(hsl); | |
} | |
} | |
} | |
} | |
} | |
function selectReadFn() { | |
const promise = aaaa(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment