block_stupid_fonts_v1.2.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_v1.2.user.js (3314B)
       ---
            1 // ==UserScript==
            2 // @name        Block stupid fonts
            3 // @namespace   -
            4 // @description Block stupid fonts
            5 // @version     1.2
            6 // @grant       none
            7 // ==/UserScript==
            8 //
            9 //
           10 /**
           11  * Userscript to whitelist or replace certain fonts and block the rest.
           12  * Author: Hiltjo Posthuma <hiltjo AT codemadness DOT org>
           13  * License: WTFPL.
           14  *
           15  * TODO:
           16  * [ ] some sites still show blacklisted fonts: http://www.polygon.com/2012/10/17/3515164/league-of-legends-lan-version-in-development-at-riot-games-mac-client
           17  * [ ] test on sites with <body onload=""> event.
           18  * [x] remove whitelist, replacelist basicly does the same if replaced with the same font.
           19  * [?] don't set defaultfont?, doesnt always seem to yield the desired results.
           20  * [x] dont use window onload event, use greasemonkey @run-at metadata?
           21  *     doesn't seem to work for all sites.
           22  * [x] able to replace certain fonts.
           23  * [x] set @grant metadata to prevent Greasemonkey whining when you change the script.
           24  *
           25  * Changelog:
           26  *  v1.2:
           27  *      - Set Greasemonkey @grand metadata line to lower permissions.
           28  *      - Ability to replace fonts.
           29  *      - Simplification, remove stats and 'cache' logic, assume the browsers javascript engine is smart *derp*.
           30  *      - Inherit font from parent if no fonts match the whitelist.
           31  *      - Specify a default sane font for sites.
           32  *      - Remove window load event.
           33  *
           34  *         v1.1:
           35  *      - Added execute once on window load.
           36  */
           37 (function() {
           38         /* Replace these fonts (match regex, replaced font name) */
           39         var replacelist = [
           40                 /* Whitelist */
           41                 [ 'arial' ],
           42                 [ 'helvetica' ],
           43                 [ 'liberation sans' ],
           44                 [ 'verdana' ],
           45                 [ 'tahoma' ],
           46                 [ 'serif' ],
           47                 [ 'sans-serif' ],
           48                 [ 'monospace' ],
           49                 /* Replace */
           50                 [ '.* mono', 'monospace' ],
           51                 [ 'courier .*', 'monospace' ],
           52                 [ '.*', null ] // Remove the rest.
           53         ];
           54         var defaultfont = 'sans-serif';
           55         var els = document.getElementsByTagName('*');
           56         for(var i = 0; i < els.length; i++) {
           57                 var cstyle = window.getComputedStyle(els[i], null);
           58                 if(cstyle === null || typeof(cstyle) == 'undefined')
           59                         continue;
           60                 var fontfamily = cstyle.getPropertyValue('font-family') || '';
           61                 if(fontfamily == '')
           62                         continue;
           63                 var fonts = fontfamily.split(',');
           64                 var newfonts = [];
           65                 var ischanged = false;
           66                 for(var j = 0; j < fonts.length; j++) {
           67                         for(var r = 0; r < replacelist.length; r++) {
           68                                 var re = new RegExp('^["\']?' + replacelist[r][0] + '["\']?$', 'i');
           69                                 if(fonts[j].match(re)) {
           70                                         if(replacelist[r].length > 1) {
           71                                                 if(replacelist[r][1] !== null)
           72                                                         newfonts.push(replacelist[r][1]);
           73                                                 ischanged = true;
           74                                         } else
           75                                                 newfonts.push(fonts[j]);
           76                                         break;
           77                                 }
           78                         }
           79                 }
           80                 if(ischanged || fonts.length != newfonts.length) { /* Only set style if fonts were changed. */
           81                         var fns = newfonts.join(',');
           82                         if(!fns.length) {
           83 //                                els[i].style.fontFamily = fns;
           84 //                        } else {
           85                                 if(els[i].tagName.toLowerCase() == 'body' || els[i].tagName.toLowerCase() == 'html') {
           86                                         // Make sure to set a proper default font if the site doesnt specify any sane font in our whitelist.
           87 //                                        els[i].style.fontFamily = defaultfont;
           88                                         fns = defaultfont;
           89                                 } else {
           90 //                                        els[i].style.fontFamily = 'inherit';
           91                                         fns = 'sans-serif'; // 'inherit';
           92                                 }
           93                         }
           94                         els[i].style.setProperty('font-family', fns, 'important');
           95         //                els[i].style.fontFamily = fns;
           96                 }
           97         }
           98 })();