Skip to content

Instantly share code, notes, and snippets.

@awsr
Last active August 30, 2023 23:48
Show Gist options
  • Save awsr/eb3c6c16f3aaf62e87e345acf1bbff3a to your computer and use it in GitHub Desktop.
Save awsr/eb3c6c16f3aaf62e87e345acf1bbff3a to your computer and use it in GitHub Desktop.
Copy your current chat input to clipboard as rot13-encoded text in Twitch
// ==UserScript==
// @name Rot13 Encode Chatbox to Clipboard
// @namespace https://gist.github.com/awsr
// @version 0.3.0
// @description Encode message in chatbox and copy to clipboard
// @author Tenosis
// @downloadURL https://gist.github.com/awsr/eb3c6c16f3aaf62e87e345acf1bbff3a/raw/rot13button.user.js
// @match https://www.twitch.tv/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
let startupLoops = 0;
const scriptCSS = document.createElement("style");
scriptCSS.textContent = `
.userscript-button13 {
position: relative;
display: flex;
flex-flow: column nowrap;
flex-grow: 1;
height: 100%;
color: var(--color-text-base);
}
.userscript-button13 button {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
user-select: none;
height: var(--button-size-default);
width: var(--button-size-default);
border-radius: var(--border-radius-medium);
background-color: var(--color-background-button-text-default);
color: var(--color-fill-button-icon);
font-size: var(--button-text-default);
font-weight: var(--font-weight-semibold);
text-decoration: none;
vertical-align: middle;
overflow: hidden;
}
.userscript-button13 button:active {
background-color: var(--color-background-button-text-active);
color: var(--color-fill-button-icon-active);
}
.userscript-button13 button:hover {
background-color: var(--color-background-button-text-hover);
color: var(--color-fill-button-icon-hover);
}
`;
document.head.append(scriptCSS);
function rot13(message) {
return message.replace(/[a-z]/gi, letter => String.fromCharCode(letter.charCodeAt(0) + (letter.toLowerCase() <= 'm' ? 13 : -13)));
}
function buttonHandler() {
let chatText = document.querySelector("[data-a-target=chat-input-text]")?.innerText;
if (chatText && !(/\n/.test(chatText) && chatText.length === 2)) {
chatText = rot13(chatText);
navigator.clipboard.writeText(chatText);
}
}
const rot13div = document.createElement("div");
rot13div.classList.add("userscript-button13");
const rot13button = document.createElement("button");
rot13button.innerText = "13";
rot13div.appendChild(rot13button);
rot13button.addEventListener("click", buttonHandler);
function startup() {
let chatButtons = document.querySelector(".chat-input__buttons-container > div:last-child");
if (chatButtons) {
clearInterval(startupChecker);
chatButtons.insertAdjacentElement("afterbegin", rot13div);
} else {
startupLoops++;
if (startupLoops > 10) {
clearInterval(startupChecker);
}
}
}
const startupChecker = setInterval(startup, 2000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment