block_stupid_fonts.user.js - www.codemadness.org - www.codemadness.org saait content files
 (HTM) git clone git://git.codemadness.org/www.codemadness.org
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       block_stupid_fonts.user.js (1974B)
       ---
            1 // ==UserScript==
            2 // @name        Block stupid fonts
            3 // @namespace   -
            4 // @description Block stupid fonts
            5 // @version     1
            6 // ==/UserScript==
            7 //
            8 //
            9 /**
           10  * Userscript to whitelist certain fonts and block the rest.
           11  * Author: Hiltjo Posthuma <hiltjo AT codemadness DOT org>
           12  * License: WTFPL.
           13  */
           14 (function() {
           15         /* These fonts are kindof OK (imho)... */
           16         var whitelist = [
           17                 /arial/i,
           18                 /helvetica/i,
           19                 /liberation sans/i,
           20                 /verdana/i,
           21                 /tahoma/i,
           22                 /serif/i,
           23                 /sans-serif/i,
           24                 /monospace/i
           25         ];
           26         var els = document.getElementsByTagName('*');
           27         var blocked = {}; /* blocked cache */
           28         var whitelisted = {};
           29         for(var i = 0; i < els.length; i++) {
           30                 var fontfamily = window.getComputedStyle(els[i], null).getPropertyValue('font-family') || '';
           31                 if(fontfamily == '')
           32                         continue;
           33                 var fonts = fontfamily.split(',');
           34                 var newfonts = [];
           35                 var replaced = 0; /* amount of fonts replaced for this property */
           36                 for(var j = 0; j < fonts.length; j++) {
           37                         var iswhitelisted = whitelisted[fonts[j]] || 0;
           38                         blocked[fonts[j]] = blocked[fonts[j]] || 0; /* make sure blocked[font] is set */
           39                         if(!iswhitelisted && !blocked[fonts[j]]) { /* lookup (not blocked or whitelisted already). */
           40                                 for(var k = 0; k < whitelist.length; k++) { /* check against whitelist */
           41                                         if(fonts[j].match(whitelist[k])) {
           42                                                 whitelisted[fonts[j]] = (whitelisted[fonts[j]] || 0) + 1; /* save for whitelisted faster lookup */
           43                                                 iswhitelisted = true;
           44                                                 break;
           45                                         }
           46                                 }
           47                         }
           48                         if(iswhitelisted) {
           49                                 newfonts.push(fonts[j]);
           50                                 replaced++;
           51                         } else
           52                                 blocked[fonts[j]]++; /* save for blocked faster lookup and stats */
           53                 }
           54                 if(replaced != fonts.length) { /* Only set style if some fonts were replaced. */
           55                         var fns = newfonts.join(',');
           56                         if(fns.length)
           57                                 els[i].style.fontFamily = fns;
           58                         else
           59                                 els[i].style.fontFamily = null; 
           60                 }
           61         }
           62         for(key in blocked) /* stats */
           63                 if(blocked[key] > 0)
           64                         console.log('FontBlock: blocked ' + key + ' ' + blocked[key] + ' times');
           65 })();