GM_addStyle() on document-start - how to remove the styles?

872 views
Skip to first unread message

Matt Kruse

unread,
Mar 8, 2012, 9:58:13 AM3/8/12
to greasemonkey-users
@run-at document-start is working great to apply "theme" CSS to a site
before anything renders. Kudos on adding this feature!

I want to allow the user to un-apply the theme, but I can't find any
hook into the added CSS. It's not in a <style> block in the page when
I view source.

Is there any way to hook into the styles added with GM_addStyle() when
running at document-start, so I can disable it?

Thanks!

Matt Kruse

Anthony Lieuallen

unread,
Mar 8, 2012, 11:13:22 AM3/8/12
to greasemon...@googlegroups.com
On Thu, Mar 8, 2012 at 9:58 AM, Matt Kruse <ma...@thekrusefamily.com> wrote:
@run-at document-start is working great to apply "theme" CSS to a site
before anything renders. Kudos on adding this feature!

Really?  I'd love to see your script.
 
I want to allow the user to un-apply the theme, but I can't find any
hook into the added CSS. It's not in a <style> block in the page when
I view source.

When you use view source, you see the downloaded source, not the "applied" source after JS modifications.  Use the web console / DOM inspector / Firebug.
 
Is there any way to hook into the styles added with GM_addStyle() when
running at document-start, so I can disable it?

GM_addStyle() doesn't do anything different at different run-at times.  It tries to return the <style> node that it created, but I'm pretty sure it fails to do so (hitting built in security mechanisms).  It's a _really_ simple function though[1] so it should be straightforward to write one yourself that gives you a definite handle to the style node.

OTOH, perhaps a better way is probably to just write all your rules to include something like "body.my_script_name" in the selector.  Apply that class to the body, and remove it to remove the styles.

[1] https://github.com/arantius/greasemonkey/blob/master/content/miscapis.js#L98

Robert

unread,
Mar 8, 2012, 3:19:55 PM3/8/12
to greasemon...@googlegroups.com

@run-at document-start is working great to apply "theme" CSS to a site
before anything renders. Kudos on adding this feature!

Really?  I'd love to see your script.

Then, you should take a look at www.socialfixer.com ;-)


Robert


Anthony Lieuallen

unread,
Mar 8, 2012, 3:37:20 PM3/8/12
to greasemon...@googlegroups.com
On Thu, Mar 8, 2012 at 3:19 PM, Robert <enk...@uni.de> wrote:
before anything renders. Kudos on adding this feature!

Really?  I'd love to see your script.
@run-at document-start is working great to apply "theme" CSS to a site

Then, you should take a look at www.socialfixer.com ;-)


You mean http://www.socialfixer.com/socialfixer.user.js ?  There's no run-at there.

Robert

unread,
Mar 8, 2012, 3:52:17 PM3/8/12
to greasemon...@googlegroups.com

You mean http://www.socialfixer.com/socialfixer.user.js ?  There's no run-at there.

Well... Maybe in a couple of days/weeks. (There was no new beta in the last days, so something special must happen! :))))))))

Robert

Reply all
Reply to author
Forward
0 new messages