colorize nicknames in Trovo chat
当前为
// ==UserScript==
// @name trovonicknamecolorizer
// @namespace http://tampermonkey.net/
// @version 0.2.5.6
// @description colorize nicknames in Trovo chat
// @author yyko
// @match https://trovo.live/*
// @icon https://www.google.com/s2/favicons?domain=trovo.live
// @run-at document-end
// @grant none
// ==/UserScript==
!function(){"use strict";const e=20,t=2e3,n=new Map([["red","#FF0000"],["blue","#0000FF"],["green","#008000"],["firebrick","#B22222"],["coral","#FF7F50"],["yellowgreen","#9ACD32"],["orangered","#FF4500"],["seagreen","#2E8B57"],["goldenrod","#DAA520"],["chocolate","#D2691E"],["cadetblue","#5F9EA0"],["dodgerblue","#1E90FF"],["hotpink","#FF69B4"],["blueviolet","#8A2BE2"],["springgreen","#00FF7F"]]),o='<svg aria-hidden="true" class="svg-icon btn-icon size24" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="24" height="24"><path d="M12 1.5C6.202 1.5 1.5 6.202 1.5 12S6.202 22.5 12 22.5a1.748 1.748 0 0 0 1.295-2.923 1.733 1.733 0 0 1-.437-1.16c0-.969.781-1.75 1.75-1.75h2.059a5.835 5.835 0 0 0 5.833-5.834C22.5 5.677 17.798 1.5 12 1.5zM5.583 12c-.968 0-1.75-.782-1.75-1.75s.782-1.75 1.75-1.75c.969 0 1.75.782 1.75 1.75S6.552 12 5.583 12zm3.5-4.667c-.968 0-1.75-.781-1.75-1.75 0-.968.782-1.75 1.75-1.75.969 0 1.75.782 1.75 1.75 0 .969-.781 1.75-1.75 1.75zm5.834 0c-.969 0-1.75-.781-1.75-1.75 0-.968.781-1.75 1.75-1.75.968 0 1.75.782 1.75 1.75 0 .969-.782 1.75-1.75 1.75zm3.5 4.667c-.969 0-1.75-.782-1.75-1.75s.781-1.75 1.75-1.75c.968 0 1.75.782 1.75 1.75S19.385 12 18.417 12z" style="stroke-width:.0546871"/></svg>';function s(e="users"){let t=localStorage.getItem(e);return t?(n=t,new Map(Object.entries(JSON.parse(n)))):null;var n}function a(e,t="users"){var n;localStorage.setItem(t,(n=e,JSON.stringify(Object.fromEntries(n))))}let l=s();l?localStorage.getItem("tncts_localUsers")&&localStorage.removeItem("tncts_localUsers"):l=new Map;let r,c,i=s("colorizerSettings");function g(e){let t=function(){let e=function(){if(!r){let e=n.keys();r=[];for(let t of e)r.push(t)}return r}();return e[Math.round(Math.random()*e.length)]}();return f(e,t,!0),t}function u(e){let t;return t=l.has(e)?l.get(e):g(e)}function f(e,t,o=!1){i.get("baseLock")&&!o||(l.set(e,n.get(t.toLocaleLowerCase())),a(l))}function m(){let e=document.getElementsByClassName("input-feature-box")[0];if(e)return e.getElementsByClassName("cat-button normal icon")[0]}function d(){i.get("interfaceState")?c.classList.remove("active"):c.classList.add("active"),function(){i.get("baseLock");i.set("baseLock",!i.get("baseLock"))}(),i.set("interfaceState",!i.get("interfaceState")),a(i,"colorizerSettings")}i||(i=new Map([["baseLock",!1],["interfaceState",!0]]));let h=!0;function b(e,t){h&&(h=!1,function(){let e=m();e&&((c=e.cloneNode(!1)).setAttribute("data-enclave","colorizer"),c.innerHTML=o,c.color="white",i.get("interfaceState")&&c.classList.add("active"),m().after(c),c.addEventListener("click",d))}());for(let t of e)for(let e of t.addedNodes){let t,o=e.getElementsByClassName("nickname-box")[0];if(o){t=o.getElementsByClassName("nick-name")[0].title;let a=e.getElementsByClassName("content")[0];if(a){let e=a.innerText.match(/^!color (.*)/);if(e){let o=e[1].split(" ");if(s=o[o.length-1],n.has(s))if(o.length>1)for(let e=0;e<o.length-1;e++)"@"==o[e][0]&&(o[e]=o[e].slice(1)),f(o[e],o[o.length-1]);else f(t,e[1]);else console.warn("color is not available")}}o.style.color=u(t)}}var s}let v,w,F=!1;const S={childList:!0};let p,E=e;function L(){(v=document.getElementsByClassName("chat-list")[0])?((w=new MutationObserver(b)).observe(v,S),console.warn("started"),F=!0):E>0?(console.warn("attempts left to start: ",E),E--,p=setTimeout(L,t)):console.warn("cant find chat element")}function k(){console.warn("restarted"),E=e,F&&(w.disconnect(),c&&(c.remove(),h=!0),F=!1,L())}!function(){L();let e=document.getElementsByClassName("base-container")[0];new MutationObserver(k).observe(e,S)}()}();