Skip to content

Instantly share code, notes, and snippets.

@awsr
Last active April 25, 2024 02:50
Show Gist options
  • Save awsr/ad422b3212a08813977c61e7d61ae94f to your computer and use it in GitHub Desktop.
Save awsr/ad422b3212a08813977c61e7d61ae94f to your computer and use it in GitHub Desktop.
Decode ROT13 messages in Twitch chat via CTRL + double-click
// ==UserScript==
// @name Twitch Chat ROT13 Messages
// @namespace https://gist.github.com/awsr
// @version 2.0.0
// @description Decode ROT13 messages in Twitch chat via CTRL + double-click
// @author Tenosis
// @downloadURL https://gist.github.com/awsr/ad422b3212a08813977c61e7d61ae94f/raw/deRot13.user.js
// @match https://www.twitch.tv/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// INFO:
// Use CTRL + double-click to toggle ROT13 state.
// CSS Styles ====
const scriptCSS = document.createElement("style");
scriptCSS.innerText = ".userscript-rot13>div:first-child::before{display:block;width:4px;height:100%;background-color:#debb1f;content:'';position:absolute;left:-8px;border-radius:3px 0 0 3px;}";
document.head.append(scriptCSS);
// Functions =====
function rot13(text) {
return text.replace(/[a-z]/gi, letter => String.fromCharCode(letter.charCodeAt(0) + (letter.toLowerCase() <= 'm' ? 13 : -13)));
}
function decoder(event) {
// Only check if control key is pressed
if (event.ctrlKey) {
const chatLine = event.target.closest(".chat-line__message");
if (chatLine) {
const textTarget = chatLine.querySelectorAll(".text-fragment");
if (textTarget.length) {
// Process multiple segments in case of mid-sentence special formatting
textTarget.forEach((segment) => { segment.textContent = rot13(segment.textContent) });
// Mark line to indicate it has been changed
chatLine.classList.toggle("userscript-rot13");
// Remove any text selection from double-clicking
getSelection().removeAllRanges();
// De-focus chat line
chatLine.blur();
}
}
}
}
// Start =========
document.documentElement.addEventListener("dblclick", decoder);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment