javascriptjqueryhtmluserscriptsremoveclass

Userscript to remove Html-Lines, class and Attributes


I want to create one short userscript because I hate this annoying yellow smileys!

There are two html lines witch turns the normal smiley ( :) ) into the yellow icon

 <span class="emoticon_text" aria-hidden="true"> :) </span>

 <span title=":)" class="emoticon emoticon_smile"></span>

So, in the first line I have to remove the class and the aria-hidden

And in the second the whole line, it can be class="emoticon emoticon_smile", but also something like class="emoticon emoticon_cool"

I tried with:

document.getElementsByClassName("emoticon_ text").removeAttribute("aria-hidden"); document.getElementsByClassName("emoticon_ text").className = "";

but it failed, so I hope you guys can help me, because my Javescript/jQuery skills are bad..

Thank you

sorry for my grammar mistakes


Solution

  • document.getElementsByClassName returns a HTMLCollection, which is basically a array of elements matched. You have to iterate trough that collection and run your code for each of the elements matched.

    Secondly, you'll need to find the emoticon itself and remove it, for that you need to get each emoticon's parent and tell it to remove the element. In the end, your code will look similar to this:

    //Finds all emoticon texts
    var emoticonTexts = document.getElementsByClassName("emoticon_text");
    
    //Iterate over the results and remove the desired attributes
    for (var i = emoticonTexts.length-1; i >= 0; i -= 1) {
        var element = emoticonTexts[i];
        element.removeAttribute("aria-hidden");
        element.className = "";
    }
    
    //Find all emoticon images
    var emoticons = document.getElementsByClassName("emoticon");
    
    //Iterate over the results and remove them from the page
    for (var i = emoticons.length-1; i >= 0; i -= 1) {
        var element = emoticons[i];
        element.parentNode.removeChild(element);
    }
    

    Also available as an example on JSFiddle