// ==UserScript==
// @name - Fonts - Apply Text Shadow
// @match *://*/*
// @version 2.17.1
// @grant none
// ==/UserScript==
'use strict';
var isRunning;
function applyFilter() {
var AllElem=document.querySelectorAll(':not(script):not(style):not(area):not(base):not(br):not(col):not(embed):not(hr):not(img):not(input):not(keygen):not(link):not(meta):not(param):not(source):not(track):not(wbr):not(table):not(tbody):not(tr):not(ul)')
for (var i=0;i<AllElem.length;i++) {
for (var j=0;j<AllElem[i].childNodes.length;j++) { // cycle through element nodes
if (AllElem[i].childNodes[j].nodeType===3 && AllElem[i].childNodes[j].textContent.trim().length>0) {// is it a text node?
if (window.getComputedStyle(AllElem[i]).getPropertyValue('text-shadow')=='none'){ // do not run if text-shadow is already present
var Col=window.getComputedStyle(AllElem[i]).getPropertyValue('color').replace(/[^\d,.]/g,'').split(',') // text color array (R/G/B/A)
if (typeof(Col[3])=='undefined'||Col[3].split('.')[0]=='1') { // run if element does not have an alpha channel already applied
var Lum=Math.round(0.2126*Col[0]+0.7152*Col[1]+0.0722*Col[2]) // luminosity
var Opa=parseFloat(255*(255-Lum)/65025).toFixed(1) // opacity between 0 and 1
if (Lum<128) Opa=1
AllElem[i].style.setProperty('text-shadow','0 0 0px rgba('+Col[0]+','+Col[1]+','+Col[2]+','+Opa+')','important') // set text shadow with alpha
}
}
}
}
}
}
function waitAndApplyFilter() {
if (typeof(isRunning)!='undefined') clearTimeout(isRunning)
isRunning=setTimeout(function(){applyFilter()},100)
}
const callback = (mutationList, observer) => { // called every time BODY has changed
for (const mutation of mutationList) {
if (mutation.type === "childList") waitAndApplyFilter()
}
};
applyFilter();
const targetNode = document.getElementsByTagName("body")[0]
// Options for the observer (which mutations to observe)
const config = { attributes: false, childList: true, subtree: true };
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);