[Greasemonkey] Replacement for addGlobalStyle()

Mark Pilgrim pilgrim at gmail.com
Tue Jul 5 11:23:43 EDT 2005


On 7/4/05, Thom Wetzel <lmnopc at lmnopc.com> wrote:
> 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.

I came up with this cross-browser function for Zoom Google:

http://diveintomark.org/projects/greasemonkey/accessibility/zoom-google.user.js

function addGlobalStyle(css) {
    try {
        var elmHead, elmStyle;
        elmHead = document.getElementsByTagName('head')[0];
        elmStyle = document.createElement('style');
        elmStyle.type = 'text/css';
        elmHead.appendChild(elmStyle);
        elmStyle.innerHTML = css;
    } catch (e) {
        document.styleSheets[0].cssText += css;
    }
}

Though this probably requires that the page has a stylesheet at all
(Google did, so I didn't care).

-- 
Cheers,
-Mark


More information about the Greasemonkey mailing list