Skip to content

Instantly share code, notes, and snippets.

@kaloncpu57
Last active May 5, 2017 03:42
Show Gist options
  • Save kaloncpu57/a9e420bee1856607195286843cc55d75 to your computer and use it in GitHub Desktop.
Save kaloncpu57/a9e420bee1856607195286843cc55d75 to your computer and use it in GitHub Desktop.
Wrap hex color codes in divs - BetterDiscord plugin piece
class hexColorPreview {
wrapAll() {
let messages = document.getElementsByClassName("markup");
for (var i = 0; i < messages.length; i++) {
let msg = messages[i];
if (msg.getElementsByClassName(".hex-value").length > 0) continue; //don't wrap multiple times
let html = msg.innerHTML;
html = html.replace(
/#(?:[0-9a-fA-F]{3}){1,2}/g, //find hex color values
'<div class="hex-value" style="color: $&;">$&<div class="hex-preview" style="background: $&;"></div></div>'
);
msg.innerHTML = html;
}
}
onMessage() {
this.wrapAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment