Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name no light fonts
// @namespace http://ikeran.org
// @description prevents sites from using font-weight below 400
// @include *
// @version 1
// @grant none
// ==/UserScript==
(function() {
let min = 14; // px
let max = 18; // px
let hdrMax = 32; // px
function fixFonts(elem) {
if (!elem) return;
let fs = window.getComputedStyle(elem, null).fontSize;
let size = parseInt(fs);
if (size < min) {
size = min;
}
let t = elem.tagName.toLowerCase();
if (t == "h1" || t == "h2") {
if (size > hdrMax) {
size = hdrMax;
}
} else if (size > max) {
size = max;
}
size = "" + size + "px";
elem.style.fontSize = size;
let kids = elem.children;
for (let i = 0; i < kids.length; i++) {
fixFonts(kids[i]);
}
}
var forbidFontWeight = function() {
document.body.style.setProperty("font-weight", "400", "important");
}
var NAME = "dhasenan-font-weight";
fixFonts(document.body);
var cache = localStorage.getItem(NAME);
if (cache) {
if (cache.isSafe)
return;
else
forbidFontWeight();
}
var elements = document.getElementsByTag('*');
for (var i = 0 ; i < elements.length; i++) {
if (window.getComputedStyle(elements[i]).getPropertyValue("font-weight") < 400) {
localStorage.setItem(NAME, {isSafe: false});
forbidFontWeight();
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment