Skip to content

Instantly share code, notes, and snippets.

@kirkegaard kirkegaard/ddd.js
Created Apr 6, 2016

Embed
What would you like to do?
var flairColor = [
'#e50000', // red
'#db8e00', // orange
'#ccc100', // yellow
'#02be01', // green
'#0083c7', // blue
'#820080' // purple
];
function colorFromName(name) {
sanitizedName = name.toLowerCase().replace(/[^a-z0-9]/g, "");
flairNum = parseInt(sanitizedName, 36) % 6;
return flairColor[flairNum];
}
// Initial pass to color names in user list
$('#robinUserList').find('.robin--username').each(function(){
this.style.color = colorFromName(this.textContent);
});
// When a user's status changes, they are removed from the user list and re-added with new status classes,
// so here we watch for names being added to the user list to re-color
var myUserListObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length > 0) {
var usernameSpan = mutation.addedNodes[0].children[1];
usernameSpan.style.color = colorFromName(usernameSpan.innerHTML);
}
});
});
myUserListObserver.observe(document.getElementById("robinUserList"), { childList: true });
// Color current user's name in chat and darken post backgrounds
var currentUserColor = colorFromName(currentUsersName);
$('<style>.robin--user-class--self .robin--username { color: ' + currentUserColor + ' !important; }</style>').appendTo('body');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.