trovonicknamecolorizer

colorize nicknames in Trovo chat

当前为 2021-08-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         trovonicknamecolorizer
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  colorize nicknames in Trovo chat
// @author       yyko
// @match        https://trovo.live/*
// @icon         https://www.google.com/s2/favicons?domain=trovo.live
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const maxAttemptsCount = 20;

    const colors=[
      "#FF0000",
      "#0000FF",
      "#008000",
      "#B22222",
      "#FF7F50",
      "#9ACD32",
      "#FF4500",
      "#2E8B57",
      "#DAA520",
      "#D2691E",
      "#5F9EA0",
      "#1E90FF",
      "#FF69B4",
      "#8A2BE2",
      "#00FF7F",
    ];

    // random color from colors list
    function getRandomColor(){
        return colors[Math.round(Math.random()*(colors.length-1))];
    }

    // cookie tools
    function getCookie(name) {
      let matches = document.cookie.match(new RegExp(
        "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
      ));
      return matches ? decodeURIComponent(matches[1]) : undefined;
    }

    function setCookie(name, value, options = {}) {
      options = {
        path: '/',
        ...options
      };

      if (options.expires instanceof Date) {
        options.expires = options.expires.toUTCString();
      }

      let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);

      for (let optionKey in options) {
        updatedCookie += "; " + optionKey;
        let optionValue = options[optionKey];
        if (optionValue !== true) {
          updatedCookie += "=" + optionValue;
        }
      }

      document.cookie = updatedCookie;
    }
    // --

    let users = getCookie('users');
    if(users){
        users = new Map(Object.entries(JSON.parse(users)));
        updateCookieAge();
    }else{
        users = new Map();
    }

    function addUser(username){
        let color = getRandomColor();
        users.set(username,color);
        setCookie('users',JSON.stringify(Object.fromEntries(users)),{'max-age':3600*24*30});
        return color;
    }

    function delUser(username){
        users.delete(username);
    }

    // костыль:
    function updateCookieAge(){
        let ucun = '#cookieUpdater#';
        addUser(ucun);
        delUser(ucun);
    }
    // --

    function getUserColor(username){
        let user;
        if(users.has(username)){
            user = users.get(username);
        }else{
            user = addUser(username);
        }
        return user;
    }

    function onmessage(mutations,observer){
        for(let mutation of mutations){
            for(let msgel of mutation.addedNodes){
                try{
                    let nameel=msgel.getElementsByClassName('nickname-box')[0];
                    let nickname=nameel.getElementsByClassName('nick-name')[0].title;

                    nameel.style.color=getUserColor(nickname);
                }catch(e){
                    //thereshouldbeanerrorhandlerherebutthereisnotonehere
                }
            }
        }
    }

    let chatElement;

    function initObserver(){
        let observer = new MutationObserver(onmessage);

        observer.observe(chatElement,{childList:true});
    }

    function setChatElement(){
        chatElement = document.getElementsByClassName('chat-list')[0];
        return chatElement;
    }

    let attemptsLeft = maxAttemptsCount;

    window.onload=function init(){
        if(setChatElement()){
            console.warn('started');
            initObserver();
        }else{
            if(attemptsLeft>0){
                console.warn('attempts left to start: ',attemptsLeft);
                attemptsLeft--;
                setTimeout(init,2000);
            }else{
                console.warn('cant find chat element');
            }
        }
    }
})();