[Greasemonkey] Replacement for addGlobalStyle()

Thom Wetzel lmnopc at lmnopc.com
Tue Jul 5 00:11:05 EDT 2005


I was looking for an IE/Turnabout-compatible addGlobalStyle() function 
and came across something really cool I thought I'd share.  Not only is 
this IE-compatible, but it's also quite a bit faster in 
Firefox/Greasemonkey.  In my Safe-for-Work Shack script I average a 
savings of 150ms using the new version below. 

Christian from http://aleto.ch/ originally posted this code to 
http://meyerweb.com/eric/thoughts/2004/11/10/s5-11a2/ and he also 
created a test page so you can see it in action: 
http://aleto.ch/test/increaseFontSize2.html

    function addGlobalStyle(string){
        if(/microsoft/i.test(navigator.appName) && 
!/opera/i.test(navigator.userAgent)){
            document.createStyleSheet().cssText=string;
        }
        else {
            var ele=document.createElement('link');
            ele.rel='stylesheet';
            ele.type='text/css';
            ele.href='data:text/css;charset=utf-8,'+escape(string);
            document.getElementsByTagName('head')[0].appendChild(ele);
        }
    }

I hope someone else finds this as useful as I do. 

Thom Wetzel
www.thomwetzel.com


More information about the Greasemonkey mailing list