Skip to content

Instantly share code, notes, and snippets.

@Shmiddty
Created February 17, 2015 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shmiddty/703b268721c779e74d5d to your computer and use it in GitHub Desktop.
Save Shmiddty/703b268721c779e74d5d to your computer and use it in GitHub Desktop.
SO Chat color parser
code .color-hightlight{color:inherit !important;}
.content > .color-highlight span{
color:#ddd;
margin-left:.25em;
}
.content > .color-highlight::before{
content:'';
display:inline-block;
width:16px;
height:16px;
background:currentColor;
vertical-align:text-bottom;
border-radius:2px;
box shadow: 1px 1px rgba(0,0,0,.3);
}
var chat = document.getElementById('chat');
function parseNode(node) {
if (node.classList && node.classList.contains('message') && !node.classList.contains('pending')) {
[].forEach.call(node.childNodes, function(child) {
if (/#(?:[0-9a-f]{3}){1,2}/ig.test(child.textContent)) {
child.innerHTML = child.innerHTML.replace(/#(?:[0-9a-f]{3}){1,2}/ig, function(match) {
return '<span class=\"color-highlight\" style=\"color:'+match+';\"><span>' + match + '</span></span>';
});
}
});
}
}
[].forEach.call(chat.querySelectorAll('.user-container .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