-
-
Save CS1000/9be3fd9f7de925b4a428 to your computer and use it in GitHub Desktop.
For use on Miaou (add "color: <#hexcolor || colorname>" somewhere in your profile->about me)
This file contains hidden or 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'); | |
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