Skip to content

Instantly share code, notes, and snippets.

@rlemon
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rlemon/fe9cf56aeabecff1b546 to your computer and use it in GitHub Desktop.
Save rlemon/fe9cf56aeabecff1b546 to your computer and use it in GitHub Desktop.
color coded names for miaou
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