Skip to content

Instantly share code, notes, and snippets.

@kobachi
Last active March 19, 2022 10:49
Show Gist options
  • Save kobachi/050cc8f70b12f4ffbf6e89c9367d13eb to your computer and use it in GitHub Desktop.
Save kobachi/050cc8f70b12f4ffbf6e89c9367d13eb to your computer and use it in GitHub Desktop.
Custom Font Remover
// ==UserScript==
// @name Custom Font Remover
// @version 2.1
// @include http://*
// @include https://*
// @grant none
// ==/UserScript==
const GM_log = () => {};//unsafeWindow.console.log;
const BLACK_LIST = [
"segoe ui", "meiryo", "system-ui", "-apple-system", "blinkmacsystemfont",
"roboto", "helvetica", "arial", "inherit", 'freight sans bold',
"hiragino kaku gothic pro", "ms pgothic", "helvetica neue", "segoe ui historic",
"georgia", "sfprodisplay-bold", "sfprotext-semibold", "sfprotext-regular", ".sfnstext-regular",
"menlo", "consolas", "monaco","google sans", "helveticaneue-light",
"helveticaneue", "sans-serif-medium", "palatino linotype", "ヒラギノ角ゴ pro w3", "メイリオ",
"ヒラギノ丸ゴ pro w4", "ヒラギノ丸ゴ pro", "hiragino maru gothic pro", "hg丸ゴシックm-pro", "hgmarugothicmpro",
"hiragino kaku gothic pro w3", "ms pゴシック", "biz udpgothic", "hiragino sans",
"apple color emoji", "segoe ui emoji", "sfmono-regular", "sf mono", "liberation mono",
"ui-monospace", "courier", "hiragino kaku gothic pron", "yu gothic", "tunga",
"trebuchet ms", "tahoma", "verdana", "lucida grande", "andale mono",
"dejavu sans mono", "arial black", "Impact", "open sans", "ヒラギノ角ゴシック",
"ヒラギノ角ゴ pron w3", "droid sans", "yugothic", "游ゴシック", "courier new",
"arialmt", "osaka", "hirakakupron-w3", "ms ui gothic", "hgゴシックm",
"noto sans", "segoe ui symbol", "noto color emoji", "droid sans mono", "biz udgothic",
"meiryo ui", "游ゴシック medium", "yu gothic medium", "ヒラギノ角ゴ pro"
];
const cleanFont = function(tag) {
const fontRules = Array.from(document.styleSheets)
.flatMap(s => {
try {
return Array.from(s.rules);
} catch (e) {
return null;
}
})
.filter(r => r != null && typeof r.style != "undefined")
.filter(r => r.style.fontFamily != "" && typeof r.style.fontFamily != "undefined");
var clean = false;
fontRules.forEach(rule => {
const original = rule.style.fontFamily;
const modified = original.split(/, ?/)
.filter(f => BLACK_LIST.indexOf(f.toLowerCase().replaceAll(/["']/g, "")) == -1)
.join(", ");
if (original != modified) {
clean = true;
rule.style.fontFamily = modified;
if (modified != "" && modified != "sans-serif" && modified != "serif" && modified != "monospace") {
GM_log("[on" + tag + "] Modified: " + rule.selectorText + " { font-family: " + modified + "; }");
}
}
});
const weightRules = Array.from(document.styleSheets)
.flatMap(s => {
try {
return Array.from(s.rules);
} catch (e) {
return null;
}
})
.filter(r => r != null && typeof r.style != "undefined")
.filter(r => r.style.fontWeight == "lighter" || (/^[0-9]+$/.test(r.style.fontWeight) && parseInt(r.style.fontWeight) < 400));
weightRules.forEach(rule => {
rule.style.fontWeight = "";
clean = true;
});
const spacingRules = Array.from(document.styleSheets)
.flatMap(s => {
try {
return Array.from(s.rules);
} catch (e) {
return null;
}
})
.filter(r => r != null && typeof r.style != "undefined")
.filter(r => r.style.letterSpacing != "" && typeof r.style.letterSpacing != "undefined");
spacingRules.forEach(rule => {
rule.style.letterSpacing = "";
clean = true;
});
if (clean) {
GM_log("[on" + tag + "] Font cleanup successfuly finished.");
}
};
window.addEventListener("load", () => { cleanFont("Load") });
cleanFont("Init");
@kobachi
Copy link
Author

kobachi commented Mar 19, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment