Skip to content

Instantly share code, notes, and snippets.

@nfaltermeier
Last active May 25, 2023 00:03
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 nfaltermeier/f12120a6fc9c06e99160cf586c910a3d to your computer and use it in GitHub Desktop.
Save nfaltermeier/f12120a6fc9c06e99160cf586c910a3d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Twitch game title shortener
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Removes The Legend of Zelda from game titles on the sidebar of Twitch
// @author nfaltermeier
// @match https://www.twitch.tv/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
(function() {
'use strict';
setTimeout(() => {
const find = 'The Legend of Zelda: ';
const scanElements = (/** @type Element */ root) => {
const containers = Array.from(root.getElementsByClassName('side-nav-card__metadata'));
const textElems = containers.map(e => e.firstChild);
textElems.forEach(e => {
const innerText = e.innerText;
if (innerText.startsWith(find)) {
e.innerText = innerText.substring(find.length);
}
});
}
// Do the initial scan
scanElements(document);
// Scan for future changes
const navSections = Array.from(document.getElementsByClassName('side-nav-section'));
const callback = (/** @type MutationRecord[] */ changes) => {
changes.forEach((mutation) => {
if (mutation.type === 'characterData') {
const text = mutation.target.data;
if (text.startsWith(find)) {
mutation.target.data = text.substring(find.length);
}
} else if (mutation.type === 'childList') {
mutation.addedNodes.forEach((a) => {
scanElements(a);
});
}
});
};
/** @type MutationObserverInit */
const options = { subtree: true, childList: true, characterData: true };
const observer = new MutationObserver(callback);
navSections.forEach(n => {
observer.observe(n, options);
});
}, 3000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment