Advertisement
Guest User

ytcn.js

a guest
Jan 10th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        yt-colored-names
  3. // @namespace   Violentmonkey Scripts
  4. // @grant       none
  5. // @version     1.0
  6. // @author      -
  7. // @description 12/26/2020, 9:47:32 PM
  8. // @include     https://www.youtube.com/*
  9. // @include     https://www.youtube.com/embed/*
  10. // @include     https://www.youtube-nocookie.com/embed/*
  11. // ==/UserScript==
  12.  
  13. (() => {
  14.     let defaultColors = [
  15.         "#FF0000",
  16.         "#00FF00",
  17.         "#B22222",
  18.         "#FF7F50",
  19.         "#9ACD32",
  20.         "#FF4500",
  21.         "#2E8B57",
  22.         "#DAA520",
  23.         "#D2691E",
  24.         "#5F9EA0",
  25.         "#1E90FF",
  26.         "#FF69B4",
  27.         "#8A2BE2",
  28.         "#00FF7F",
  29.     ];
  30.  
  31.     function getDefaultSettings() {
  32.         return {
  33.             youtube: {
  34.                 enabledColors: true,
  35.             },
  36.         };
  37.     }
  38.  
  39.     function getUserChatColor(str) {
  40.         str = str.toLowerCase().trim();
  41.         let hash = 0;
  42.         for (let i = 0; i < str.length; i++) {
  43.             hash = str.charCodeAt(i) + ((hash << 5) - hash);
  44.         }
  45.  
  46.         return defaultColors[Math.abs(hash % defaultColors.length)];
  47.     }
  48.  
  49.     let settings = getDefaultSettings();
  50.  
  51.     const YouTube = {
  52.         handleMessage(node) {
  53.             if (settings.youtube.enabledColors) {
  54.                 let author = node.querySelector("#author-name");
  55.                 if (author) {
  56.                     author.style.color = getUserChatColor(author.innerText);
  57.                 }
  58.             }
  59.         },
  60.         init() {
  61.             const chatQuerySelector = "#items.yt-live-chat-item-list-renderer";
  62.             const init = (documentElement, target) => {
  63.                 if (target !== null) {
  64.                     this.document = documentElement;
  65.  
  66.                     const observer = new MutationObserver((mutations) => {
  67.                         for (let mutation of mutations) {
  68.                             for (let node of mutation.addedNodes) {
  69.                                 this.handleMessage(node);
  70.                             }
  71.                         }
  72.                     });
  73.  
  74.                     for (let element of this.document.querySelectorAll(
  75.                         "yt-live-chat-text-message-renderer"
  76.                     )) {
  77.                         this.handleMessage(element);
  78.                     }
  79.  
  80.                     const config = {
  81.                         attributes: true,
  82.                         childList: true,
  83.                         characterData: true,
  84.                     };
  85.                     observer.observe(target, config);
  86.                 }
  87.             };
  88.  
  89.             let target = document.querySelector(chatQuerySelector);
  90.             // normal stream chat
  91.             if (target === null) {
  92.                 let interval = setInterval(() => {
  93.                     let chatFrame = document.querySelector("#chatframe");
  94.                     if (chatFrame) {
  95.                         let documentElement = chatFrame.contentDocument;
  96.                         target = documentElement.querySelector(
  97.                             chatQuerySelector
  98.                         );
  99.  
  100.                         if (target !== null) {
  101.                             clearInterval(interval);
  102.                             init(documentElement, target);
  103.                         }
  104.                     }
  105.                 }, 250);
  106.             }
  107.             // popout stream chat
  108.             else {
  109.                 init(document, target);
  110.             }
  111.         },
  112.     };
  113.     if (location.hostname.toLowerCase().includes(".")) {
  114.         YouTube.init();
  115.     } else {
  116.         document.addEventListener("DOMContentLoaded", YouTube.init);
  117.     }
  118. })();
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement