[Greasemonkey] Suggestion: GM_injectCSS

Mark Pilgrim pilgrim at gmail.com
Thu Apr 14 11:05:06 EDT 2005


On 4/14/05, Simon Willison <cs1spw at bath.ac.uk> wrote:
> 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);
> }

Argh!  That's a terrible example!  Please don't copy that!  It uses
global variables!  Plus it will break in Firefox 1.0.3 (no more
window._content)!

Use this instead:

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

To the original point, no, I don't think this should be a GM function.
 I think it should be documented in a developer's FAQ, along with all
the other patterns I listed the other day.

-- 
Cheers,
-Mark


More information about the Greasemonkey mailing list