Last active
August 29, 2015 14:08
-
-
Save rlemon/fe9cf56aeabecff1b546 to your computer and use it in GitHub Desktop.
color coded names for miaou
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hashCode(str) { | |
var hash = 0; | |
str += '!'; | |
for (var i = 0; i < str.length; i++) { | |
hash = str.charCodeAt(i) + ((hash << 5) - hash); | |
} | |
return -hash; | |
} | |
function colorCode(i) { | |
return '#' + (Math.min((i >> 24) & 0xFF, 200).toString(16) + | |
Math.min((i >> 16) & 0xFF, 200).toString(16) + | |
Math.min((i >> 8) & 0xFF, 200).toString(16) + | |
Math.min(i & 0xFF, 200).toString(16)).slice(0, 6); | |
} | |
function parseNode(node) { | |
if (node.classList && node.classList.contains('message')) { | |
var user = node.querySelector('.user'); | |
user.style.color = colorCode(hashCode(user.firstChild.textContent)); | |
} | |
} | |
var chat = document.getElementById('messages'); | |
[].forEach.call(chat.querySelectorAll('.message'), parseNode); | |
new MutationObserver(function(records) { | |
records.forEach(function(record) { | |
[].forEach.call(record.addedNodes, parseNode); | |
}); | |
}).observe(chat, { | |
childList: true, | |
subtree: true | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment