Chookie
Chookie

Reputation: 69

How do I add a css class in a GM-script?

I found out that this is the way to style all images on a site.

GM_addStyle("img { border: 3px dotted green;background-color: red; }");

How do I style just a few? Is there a way to add a class to the GM_addStyle so that I can call this class later with jQuery like .addClass('myClass')?

Thank you in advance for your help and time for answering the question.

Upvotes: 2

Views: 1173

Answers (2)

w35l3y
w35l3y

Reputation: 8783

GM_addStyle((<><![CDATA[
    .myClass {
        border: 3px dotted green;
        background-color: red;
    }
]]></>).toString());

This way you don't need to end each line with \

Upvotes: 2

Brock Adams
Brock Adams

Reputation: 93613

Yes,

GM_addStyle(".myClass { border: 3px dotted green;background-color: red; }");

will create a class style that you can then add via jQuery's .addClass('myClass').

.
Note: you can add more than 1 style with each GM_addStyle statement, too. Like:

GM_addStyle
(
   '.ClearFloats                                            \
    {                                                       \
        clear:              both;                           \
    }                                                       \
    .HideIt                                                 \
    {                                                       \
        display:            none;                           \
    }                                                       \
    .StayOpaque                                             \
    {                                                       \
        opacity:            1 !important;                   \
    }                                                       \
   '
);

etc.

Upvotes: 2

Related Questions