Last active
October 27, 2023 10:18
-
-
Save Rainyan/275de6c59005945804110f661685e5d8 to your computer and use it in GitHub Desktop.
For YouTube A/B testing victims. Removes the unhelpful clickable fake links, when trying to highlight text in YT live stream chat.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name De-hyperlink YouTube chat | |
// @description Remove the unhelpful clickable fake links when trying to highlight YT live stream chat | |
// @namespace YoutubePlsStop | |
// @version 0.1.1 | |
// @match https://www.youtube.com/* | |
// @updateURL https://gist.github.com/Rainyan/275de6c59005945804110f661685e5d8/raw | |
// @run-at document-idle | |
// @noframes | |
// ==/UserScript== | |
"use strict"; | |
const PRINT_DEBUG_LOG = false; | |
let storedHref = ""; | |
// Used for tracking the found state, so that we can only parse the dom when needed. | |
let found_chat_field = false; | |
let findInterval = null; | |
findChatField(); | |
function log(text) { | |
if (PRINT_DEBUG_LOG) { | |
console.log(text); | |
} | |
} | |
// Can't use mutation observer for Youtube's in-place page loading voodoo, so just loop like a madman | |
function youtubeSucks() { | |
if(storedHref !== location.href) { | |
log("URL Changed", storedHref, location.href); | |
findChatField(); | |
storedHref = location.href; | |
} | |
} | |
setInterval(youtubeSucks, 1000); | |
let LIVE_PAGE = { | |
getChatField: () => { | |
const chatFrame = document.getElementById("chatframe"); | |
const contentDoc = chatFrame ? chatFrame.contentDocument : null; | |
// Can be null if chat window is minimized by user, which would throw an error, so we check. | |
if (contentDoc !== null) { | |
return contentDoc.querySelector("#items.style-scope.yt-live-chat-item-list-renderer"); | |
} | |
return null; | |
} | |
}; | |
function findChatField() { | |
let FindCount = 1; | |
clearInterval(findInterval); | |
findInterval = setInterval(function() { | |
if (found_chat_field && LIVE_PAGE.getChatField() === null) { | |
found_chat_field = false; | |
} | |
if (found_chat_field) { | |
return; | |
} | |
++FindCount; | |
if (FindCount > 180) { | |
log("The chat element cannot be found"); | |
clearInterval(findInterval); | |
FindCount = 0; | |
} | |
if (document.getElementById("chatframe")) { | |
if (LIVE_PAGE.getChatField() !== null) { | |
log("Found the chat element: "); | |
log(LIVE_PAGE.getChatField()); | |
found_chat_field = true; | |
initialize(); | |
// Don't clearInterval, because we wanna keep on tracking | |
// for any new chat fields in case user closed/minimized the chat. | |
//clearInterval(findInterval); | |
FindCount = 0; | |
} | |
} | |
}, 250); | |
} | |
function initialize() { | |
log("initialize..."); | |
if (LIVE_PAGE.getChatField() !== null) { | |
ChatFieldObserver.disconnect(); | |
ChatFieldObserver.observe(LIVE_PAGE.getChatField(), {childList: true}); | |
} | |
} | |
const ChatFieldObserver = new MutationObserver(function(mutations) { | |
mutations.forEach(function(e) { | |
let addedChats = e.addedNodes; | |
for (let i = 0; i < addedChats.length; ++i) { | |
addedChats[i].removeAttribute("whole-message-clickable"); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment