// ==UserScript== // @name Block stupid fonts // @namespace - // @description Block stupid fonts // @version 1 // ==/UserScript== // // /** * Userscript to whitelist certain fonts and block the rest. * Author: Hiltjo Posthuma * License: WTFPL. */ (function() { /* These fonts are kindof OK (imho)... */ var whitelist = [ /arial/i, /helvetica/i, /liberation sans/i, /verdana/i, /tahoma/i, /serif/i, /sans-serif/i, /monospace/i ]; var els = document.getElementsByTagName('*'); var blocked = {}; /* blocked cache */ var whitelisted = {}; for(var i = 0; i < els.length; i++) { var fontfamily = window.getComputedStyle(els[i], null).getPropertyValue('font-family') || ''; if(fontfamily == '') continue; var fonts = fontfamily.split(','); var newfonts = []; var replaced = 0; /* amount of fonts replaced for this property */ for(var j = 0; j < fonts.length; j++) { var iswhitelisted = whitelisted[fonts[j]] || 0; blocked[fonts[j]] = blocked[fonts[j]] || 0; /* make sure blocked[font] is set */ if(!iswhitelisted && !blocked[fonts[j]]) { /* lookup (not blocked or whitelisted already). */ for(var k = 0; k < whitelist.length; k++) { /* check against whitelist */ if(fonts[j].match(whitelist[k])) { whitelisted[fonts[j]] = (whitelisted[fonts[j]] || 0) + 1; /* save for whitelisted faster lookup */ iswhitelisted = true; break; } } } if(iswhitelisted) { newfonts.push(fonts[j]); replaced++; } else blocked[fonts[j]]++; /* save for blocked faster lookup and stats */ } if(replaced != fonts.length) { /* Only set style if some fonts were replaced. */ var fns = newfonts.join(','); if(fns.length) els[i].style.fontFamily = fns; else els[i].style.fontFamily = null; } } for(key in blocked) /* stats */ if(blocked[key] > 0) console.log('FontBlock: blocked ' + key + ' ' + blocked[key] + ' times'); })();