1

I am trying to color some users' names on some forum (http://forum.hardware.fr/hfr/Discussions/politique/american-politics-president-sujet_71396_2268.htm)

so I've made a bookmarklet :

Array.prototype.forEach.call(document.getElementsByClassName('s2'), function(img) {
    if (['Theomede', 'doublebeurre'].indexOf(img.innerHTML) > -1) {
        img.style.color = 'Red';
    }
});

However, it doesn't work for the user "doublebeurre". I wanted to know why, so while debugging, I did :

img.innerHTML.length returns 13, while 'doublebeurre'.length returns 12.

img.innerHTML.split("") returns ["d", "o", "u", "b", "l", "e", "b", "e", "u", "r", "", "r", "e"]

What is this empty character ? How can I make my script work on this user ? (img.innerHTML.trim() doesn't solve the problem)

l0r3nz4cc10
  • 1,237
  • 6
  • 27
  • 50
  • `doublebeur​re` – mplungjan Feb 03 '17 at 12:59
  • Uhm. Is the user filling the data with a contenteditable component or something? Whatever. Try this workaround to see if works: var splitted = myString.split(""); splitted.splice(splitted.indexOf(""), 1); – Asier Paz Feb 03 '17 at 13:01
  • If @mplungjan is right, you could just use **innerText** property instead of innerHTML. – Asier Paz Feb 03 '17 at 13:02
  • http://www.codetable.net/decimal/8203 It's an empty character in the text – acesmndr Feb 03 '17 at 13:02
  • If you iterate through that array, and do a "typeof" on each of them, what traces out for that (what appears to be) blank? Also, see if you can trace out the character code for it. Straightforwardly, you'd just expect there to be an empty character in the text. But if you're looking right at the text and you don't see it, then run a couple of tests on the individual chars and see what you find. If you see anything other than what you expect, something may be peculiar with the character set used in the original string. – Tim Consolazio Feb 03 '17 at 13:02
  • http://stackoverflow.com/questions/2973698/whats-html-character-code-8203 – mplungjan Feb 03 '17 at 13:08

1 Answers1

0

You could use this code to clean up empty strings.

function cleanEmpty(username) {
    return username.split("")
        .filter(function(item){
            return item != ""
        })
        .join("")
}
Diego Faria
  • 8,805
  • 3
  • 26
  • 33