Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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