GM_addStyle() with @run-at document-start fails silently in FF10/11/12?

387 views
Skip to first unread message

Matt Kruse

unread,
May 31, 2012, 3:52:48 PM5/31/12
to greasemonkey-users
I've been using code with @run-at document-start, which calls
GM_addStyle() to add CSS before the screen paints, thereby avoiding
any flash of unstyled content. This was working well in Firefox 3.6.

I recently updated to FF 12, and now I find that this no longer works.
Calls to GM_addStyle() in document-start scripts fail silently, with
no way to detect if it worked (that I can tell). I tested in Firefox
10 and 11 (portable) and see the same behavior.

Is this a new limitation in Firefox? Does anyone know why it worked in
previous versions?
Barring any way to make it work, is there a way to *detect* if it
failed, before DOMContentLoaded?

For now, I'm going to add a wrapper function that will delay calls to
GM_addStyle() until DOMContentLoaded, but I'd really like to have a
way to insert the CSS before the page initially paints, to avoid ugly
re-rendering when the styles kick in.

Any thoughts?
Thanks!

Matt Kruse

Sam Larison

unread,
May 31, 2012, 4:37:57 PM5/31/12
to greasemon...@googlegroups.com
This issue had been opened previously but no adequate solution was found for inclusion inside GM, perhaps eventually something better will be found.  Basically you need to make sure document.getElementsByTagName('head')[0] exists, if it does not exist then you should wait a bit and check again, once that exists you should be able to add styles successfully.  If it was working for you previously then it's probably because the site was complex enough to slow down processing of your script just enough that when the script started to run <head> already existed in the document.


Matt Kruse

--
You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To post to this group, send email to greasemon...@googlegroups.com.
To unsubscribe from this group, send email to greasemonkey-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.


Matt Kruse

unread,
May 31, 2012, 4:49:41 PM5/31/12
to greasemonkey-users
On May 31, 3:37 pm, Sam Larison <samlari...@gmail.com> wrote:
> If it was
> working for you previously then it's probably because the site was complex
> enough to slow down processing of your script just enough that when the
> script started to run <head> already existed in the document.

Ah, I see. Actually, for the same site - google.com - it behaves
differently.

In Firefox 3.6, there is a HEAD element at document-start, but in
Firefox 10/11/12, there is no HEAD element. That's why it works in one
but not the other.

Neither one has a BODY element at document-start.

So perhaps there is some difference in the FF versions which cause the
HEAD to be available in 3.6, and fooling me into thinking that GM was
using some other method to insert the CSS before the page loaded.

I will queue up styles and poll for HEAD to insert them. Thanks!

Matt Kruse

Anthony Lieuallen

unread,
May 31, 2012, 6:10:25 PM5/31/12
to greasemon...@googlegroups.com
On Thu, May 31, 2012 at 3:52 PM, Matt Kruse <ma...@thekrusefamily.com> wrote:
Any thoughts?

The long term answer is that once we drop compatibility with Firefox 3.6 ( https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/statistics/?last=30 still over 200k users) we'll probably change the way that document-start is implemented, and this will work better.

It's hard to say how "long" that long term is, though.

Sam Larison

unread,
May 31, 2012, 6:34:03 PM5/31/12
to greasemon...@googlegroups.com
I am not sure it is a completely silent failure, GM_addStyle should be returning null in these cases.  

On Thu, May 31, 2012 at 3:52 PM, Matt Kruse <ma...@thekrusefamily.com> wrote:
Reply all
Reply to author
Forward
0 new messages