Skip to content

Instantly share code, notes, and snippets.

@CS1000
Forked from rlemon/colornames.js
Last active Aug 29, 2015
Embed
What would you like to do?
For use on Miaou (add "color: <#hexcolor || colorname>" somewhere in your profile->about me)
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');
var username = user.firstChild.textContent;
user.style.color = userColors[username] || '';
}
}
var userColors = {}
var chat = document.getElementById('messages');
new MutationObserver(function(records) {
records.forEach(function(record) {
[].forEach.call(record.addedNodes, parseNode);
});
}).observe(chat, {
childList: true,
subtree: true
});
miaou.chat.on('incoming_message', function(m){
if (userColors[m.authorname]) return;
$.get( "publicProfile", { user: m.author + '', room: m.room + ''} )
.done(function( data ) {
var u = m.authorname;
var c = data.match(/cou?l[oe]u?r: #((?:[0-9a-f]{3}){1,2}|[a-z]{3,21})/i);
if (c) userColors[u] = '#' + c[1];
else userColors[u] = colorCode(hashCode(u));
[].forEach.call(chat.querySelectorAll('.message'), parseNode);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment