Skip to content

Instantly share code, notes, and snippets.

@LeonTheDev-io
Forked from rebane2001/discord-colored-text-generator.html
Last active May 22, 2022 22:47
Show Gist options
  • Save LeonTheDev-io/70f9f891337499535392ea618ec23b5f to your computer and use it in GitHub Desktop.
Save LeonTheDev-io/70f9f891337499535392ea618ec23b5f to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" type="image/x-icon" href="icon.png">
<title>Discord Colored Text Generator</title>
<meta charset="UTF-8">
<meta name="description" content="Discord Colored Text Generator">
<style>
/*
This license applies to the entire page, not just the style block.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
html {
font-family: sans-serif;
background-color: #36393F;
text-align: center;
color: #FFF;
}
.container {
max-width: 500px;
margin: auto;
}
.flex {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
#textarea {
width: 600px;
height: 200px;
border-radius: 5px;
resize: both;
overflow: auto;
text-align: left;
font-family: monospace;
background-color: #2F3136;
color: #B9BBBE;
border: #202225 1px solid;
padding: 5px;
display: inline-block;
}
.button {
min-height: 32px;
min-width: 32px;
border: none;
border-radius: 3px;
color: #fff;
background-color: #4f545c;
font-size: 14px;
padding: 2px 16px;
cursor: pointer;
transition: background-color 250ms linear;
}
a,
a:visited {
color: #00AFF4
}
.ansi-1 {
font-weight: 700;
text-decoration: none;
}
.ansi-4 {
font-weight: 500;
text-decoration: underline;
}
.ansi-30 {
color: #4f545c;
}
.ansi-31 {
color: #dc322f;
}
.ansi-32 {
color: #859900;
}
.ansi-33 {
color: #b58900;
}
.ansi-34 {
color: #268bd2;
}
.ansi-35 {
color: #d33682;
}
.ansi-36 {
color: #2aa198;
}
.ansi-37 {
color: #ffffff;
}
.ansi-30-bg {
background-color: #4f545c;
}
.ansi-31-bg {
background-color: #dc322f;
}
.ansi-32-bg {
background-color: #859900;
}
.ansi-33-bg {
background-color: #b58900;
}
.ansi-34-bg {
background-color: #268bd2;
}
.ansi-35-bg {
background-color: #d33682;
}
.ansi-36-bg {
background-color: #2aa198;
}
.ansi-37-bg {
background-color: #ffffff;
}
/* .ansi-0-bg { background-color: #0000003a; } */
.ansi-40 {
background-color: #002b36;
}
.ansi-41 {
background-color: #cb4b16;
}
.ansi-42 {
background-color: #586e75;
}
.ansi-43 {
background-color: #657b83;
}
.ansi-44 {
background-color: #839496;
}
.ansi-45 {
background-color: #6c71c4;
}
.ansi-46 {
background-color: #93a1a1;
}
.ansi-47 {
background-color: #fdf6e3;
}
/* .ansi-0 { background-color: #0000003a; } */
</style>
</head>
<body>
<h1>Discord <span style="color:#5865F2">Colored</span> Text Generator</h1>
<h2>Create your text</h2>
<strong>FG</strong>
<button data-ansi="30" class="button style-button ansi-30-bg">&nbsp;</button>
<button data-ansi="31" class="button style-button ansi-31-bg">&nbsp;</button>
<button data-ansi="32" class="button style-button ansi-32-bg">&nbsp;</button>
<button data-ansi="33" class="button style-button ansi-33-bg">&nbsp;</button>
<button data-ansi="34" class="button style-button ansi-34-bg">&nbsp;</button>
<button data-ansi="35" class="button style-button ansi-35-bg">&nbsp;</button>
<button data-ansi="36" class="button style-button ansi-36-bg">&nbsp;</button>
<button data-ansi="37" class="button style-button ansi-37-bg">&nbsp;</button>
<!-- <button data-ansi="0" class="button style-button ansi-0-bg">&nbsp;</button> -->
<br><br>
<strong>BG</strong>
<button data-ansi="40" class="button style-button ansi-40">&nbsp;</button>
<button data-ansi="41" class="button style-button ansi-41">&nbsp;</button>
<button data-ansi="42" class="button style-button ansi-42">&nbsp;</button>
<button data-ansi="43" class="button style-button ansi-43">&nbsp;</button>
<button data-ansi="44" class="button style-button ansi-44">&nbsp;</button>
<button data-ansi="45" class="button style-button ansi-45">&nbsp;</button>
<button data-ansi="46" class="button style-button ansi-46">&nbsp;</button>
<button data-ansi="47" class="button style-button ansi-47">&nbsp;</button>
<!-- <button data-ansi="0" class="button style-button ansi-0">&nbsp;</button> -->
<br><br>
<button data-ansi="1" class="button style-button ansi-1">Bold</button>
<button data-ansi="4" class="button style-button ansi-4">Line</button>
<button data-ansi="0" class="button style-button">Reset All</button>
<br><br>
<div class="flex">
<div id="textarea" contenteditable="true">I&nbsp;<span class="ansi-35">love</span>&nbsp;to use&nbsp;<span
class="ansi-31">C</span><span class="ansi-32">o</span><span class="ansi-33">l</span><span
class="ansi-34">o</span><span class="ansi-35">r</span><span class="ansi-36">e</span><span
class="ansi-37">d</span>&nbsp;Text On&nbsp;<span class="ansi-45"><span
class="ansi-37">Discord</span></span></div>
</div>
<br>
<button class="button copy">Copy text as Discord formatted</button>
<button class="button clear">Clear Input</button>
<br>
<br>
<div class="container">
<div>
<h3>About</h3>
<p>This is a simple app that creates colored Discord messages using the ANSI color codes available on the
latest Discord desktop versions.</p>
<p>To use this, write your text, select parts of it and assign colors to them, then copy it using the button
below, and send in a Discord message.</p>
</div>
<div>
<h3>Source Code</h3>
<p>The source code is freely available on <a
href="https://gist.github.com/LeonTheDev-io/70f9f891337499535392ea618ec23b5f">GitHub</a>.</p>
<p>Made with the help of <a href="https://gist.github.com/kkrypt0nn/a02506f3712ff2d1c8ca7c9e0aed7c06">ANSI
Color Guide</a> by kkrypt0nn.</p>
</div>
</div>
<small>This is an unofficial tool, it is not made or endorsed by Discord.</small>
<script type="text/javascript">
const textarea = document.querySelector("#textarea");
const copybtn = document.querySelector(".button.copy");
const clearboxbtn = document.querySelector(".button.clear")
// Some basic escaping of pasted HTML tags, not ideal but good enough for this situation.
textarea.oninput = () => {
const base = textarea.innerHTML.replace(/<(\/?(br|span|span class="ansi-[0-9]*"))>/g, "[$1]");
if (base.includes("<") || base.includes(">")) textarea.innerHTML = base.replace(/<.*?>/g, "").replace(/[<>]/g, "").replace(/\[(\/?(br|span|span class="ansi-[0-9]*"))\]/g, "<$1>");
};
// https://stackoverflow.com/a/61237402
document.addEventListener('keydown', event => {
if (event.key === 'Enter') {
document.execCommand('insertLineBreak')
event.preventDefault()
}
});
document.querySelectorAll(".style-button").forEach((btn) => {
btn.onclick = () => {
if (!btn.dataset.ansi) {
textarea.innerText = textarea.innerText;
return;
}
const selection = window.getSelection();
const text = window.getSelection().toString();
const span = document.createElement("span");
span.innerText = text;
span.classList.add(`ansi-${btn.dataset.ansi}`);
const range = selection.getRangeAt(0);
range.deleteContents();
range.insertNode(span);
range.selectNodeContents(span);
selection.removeAllRanges();
selection.addRange(range);
};
});
function nodesToANSI(nodes, states) {
let text = ""
for (const node of nodes) {
if (node.nodeType === 3) {
text += node.textContent;
continue;
}
if (node.nodeName === "BR") {
text += "\n";
continue;
}
const ansiCode = +(node.className.split("-")[1]);
const newState = Object.assign({}, states.at(-1));
if (ansiCode < 30) newState.st = ansiCode;
if (ansiCode >= 30 && ansiCode < 40) newState.fg = ansiCode;
if (ansiCode >= 40) newState.bg = ansiCode;
states.push(newState)
text += `\x1b[${newState.st};${(ansiCode >= 40) ? newState.bg : newState.fg}m`;
text += nodesToANSI(node.childNodes, states);
states.pop()
text += `\x1b[0m`;
if (states.at(-1).fg !== 2) text += `\x1b[${states.at(-1).st};${states.at(-1).fg}m`;
if (states.at(-1).bg !== 2) text += `\x1b[${states.at(-1).st};${states.at(-1).bg}m`;
}
return text;
}
let copyCount = 0;
let copyTimeout = null;
let clearboxCount = 0;
let clearboxTimeout = null;
copybtn.onclick = () => {
const toCopy = "```ansi\n" + nodesToANSI(textarea.childNodes, [{ fg: 2, bg: 2, st: 2 }]) + "\n```";
navigator.clipboard.writeText(toCopy).then(() => {
if (copyTimeout) clearTimeout(copyTimeout);
const funnyCopyMessages = copybtn.innerText = ["Copied!", "Double Copy!", "Triple Copy!", "Dominating!!", "Rampage!!", "Mega Copy!!", "Unstoppable!!", "Wicked Sick!!", "Monster Copy!!!", "GODLIKE!!!", "BEYOND GODLIKE!!!!", Array(16).fill(0).reduce(p => p + String.fromCharCode(Math.floor(Math.random() * 65535)), "")];
copybtn.style.backgroundColor = (copyCount <= 8) ? "#3BA55D" : "#ED4245";
copybtn.innerText = funnyCopyMessages[copyCount];
copyCount = Math.min(11, copyCount + 1);
copyTimeout = setTimeout(() => {
copyCount = 0;
copybtn.style.backgroundColor = null;
copybtn.innerText = "Copy text as Discord formatted";
}, 2000)
}, (err) => {
// We don't need to stop the users if they get a little too excited about the button
if (copyCount > 2) return;
alert("Copying failed for some reason, let's try showing an alert, maybe you can copy it instead.");
alert(toCopy);
});
}
clearboxbtn.onclick = () => {
textarea.innerHTML = "";
if (clearboxTimeout) clearTimeout(clearboxTimeout);
const funnyClearMessages = clearboxbtn.innerText = ["Cleared!", "Double Clear!", "Triple Clear!", "Dominating!!", "Rampage!!", "Mega Clear!!", "Unstoppable!!", "Wicked Sick!!", "Monster Clear!!!", "GODLIKE!!!", "BEYOND GODLIKE!!!!", Array(16).fill(0).reduce(p => p + String.fromCharCode(Math.floor(Math.random() * 65535)), "")];
clearboxbtn.style.backgroundColor = (clearboxCount <= 8) ? "#3BA55D" : "#ED4245";
clearboxbtn.innerText = funnyClearMessages[clearboxCount];
clearboxCount = Math.min(11, clearboxCount + 1);
clearboxTimeout = setTimeout(() => {
clearboxCount = 0;
clearboxbtn.style.backgroundColor = null;
clearboxbtn.innerText = "Clear Input";
}, 2000)
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment