Search code examples
jquerycssgreasemonkeyinline-styles

Modify Inline Styling, Multi-Elements Deep


I'm attempting to modify the coloring of a CometChat on a website which will remain anonymous due to having a non-modified CometChat. I just want to change the coloring, but prefer to use Greasemonkey over a User Styling extension due to not wanting to install a single extension for a single page.

I've changed the background color so that it is black, which is fine until I go to modify the default text color (Black), to be white.
The system appears to style text based on the color in a Cookie, but it uses an Inline Style on a span.

I can't figure out how to modify only the Inline Style where it is "color:#000000" (No, they don't have an appending ";") without changing all other colors, and removing a feature from the chat.

I tried:

  • Using jQuery's .attr to find the value, then apply a class which has an !important color.
  • Using .attr to change the color directly.

I Also tried span[style="color:#000000"] and a few other methods, nothing has worked so far.

One thing I noticed is that while trying to alert() to see if it's working at all (In some attempts during .each), it wouldn't create the alert. The only reason that I can think of is that it is several Div's deep into the document. (html > body > div#container > div#currentroom > div#currentroom_left > div#currentroom_convo > div#currentroom_convotext > div > div.cometchat_chatboxmessage > span.cometchat_chatboxmessagecontent > span (The one I wish to Edit) )


Solution

  • I'm guessing that that/those span(s) is/are added by AJAX (Can't tell from the question). Whether they are or not, you can use waitForKeyElements() to change it/them:

    // ==UserScript==
    // @name     YOUR_SCRIPT_NAME
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    waitForKeyElements (
        "#currentroom_convotext div.cometchat_chatboxmessage > span.cometchat_chatboxmessagecontent > span", 
        changeChatMessageCSS
    );
    
    function changeChatMessageCSS (jNode) {
        jNode.css ("color", "white");
    }
    



    Indicate whether the page resets the inline styles or uses !important in its CSS. (It probably does not do either.)