Last active
June 2, 2024 12:55
-
-
Save KenHV/6376f0455caefb687f1baed1e4d70966 to your computer and use it in GitHub Desktop.
Userscript to force the use of default fonts
This file contains 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 Default fonts | |
// @version 1.1 | |
// @description Replace web fonts with browser's default fonts | |
// @author KenHV | |
// @namespace https://kenhv.com | |
// @supportURL https://kenhv.com | |
// @homepageURL https://kenhv.com | |
// @match *://*/* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
(function () { | |
"use strict"; | |
// Cache to keep track of processed rules and stylesheets | |
const processedRules = new WeakSet(); | |
const processedStyleSheets = new WeakSet(); | |
function fontReplace() { | |
Array.from(document.styleSheets).forEach((styleSheet) => { | |
if (processedStyleSheets.has(styleSheet)) { | |
return; | |
} | |
let rules; | |
try { | |
rules = Array.from(styleSheet.cssRules); | |
} catch (err) { | |
return; | |
} | |
rules.forEach((rule) => { | |
if (rule instanceof CSSStyleRule && !processedRules.has(rule)) { | |
if (rule.style.fontFamily.includes("sans")) { | |
rule.style.fontFamily = "sans-serif"; | |
processedRules.add(rule); | |
return; | |
} | |
if (rule.style.fontFamily.includes("serif")) { | |
rule.style.fontFamily = "serif"; | |
processedRules.add(rule); | |
return; | |
} | |
if (rule.style.fontFamily.includes("mono")) { | |
rule.style.fontFamily = "monospace"; | |
processedRules.add(rule); | |
return; | |
} | |
processedRules.add(rule); | |
} | |
}); | |
processedStyleSheets.add(styleSheet); | |
}); | |
} | |
fontReplace(); | |
// Use MutationObserver to react to changes in the DOM | |
const observer = new MutationObserver(() => { | |
fontReplace(); | |
}); | |
observer.observe(document.documentElement, { | |
childList: true, | |
subtree: true, | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment