[Greasemonkey] Suggestion: GM_injectCSS

Simon Willison cs1spw at bath.ac.uk
Thu Apr 14 10:54:17 EDT 2005


One very common pattern in user scripts is an "injectCSS" function, 
which takes some CSS rules and adds them to a stylesheet on the current 
page. Mark's LIP script is a classic example:

http://diveintomark.org/projects/greasemonkey/lip.user.js

injectCSS: function(css)
{
     head = window._content.document.getElementsByTagName("head")[0];
     style = window._content.document.createElement("style");
     style.setAttribute("type", 'text/css');
     style.innerHTML = css;
     head.appendChild(style);
}

Since this is such a common pattern, it might be worth adding it as one 
of Greasemonkey's built in GM_ family of functions. I suggest 
GM_injectCSS.

Cheers,

Simon



More information about the Greasemonkey mailing list