Skip to content

Instantly share code, notes, and snippets.

@dhasenan
Created November 26, 2016 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhasenan/c3413a0856d81128a11e6a9eb6732771 to your computer and use it in GitHub Desktop.
Save dhasenan/c3413a0856d81128a11e6a9eb6732771 to your computer and use it in GitHub Desktop.
// ==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