Give survev.io better experience
// ==UserScript==
// @name Survev.io Counters
// @namespace http://tampermonkey.net/
// @license MIT
// @version 5.2.1
// @description Give survev.io better experience
// @author asultra
// @match https://survev.io/
// @icon https://images-ext-1.discordapp.net/external/gTz23R2UY6gWx4qaPvrRMFXecuHz3bSyf7S5BDaGM-I/https/i.ibb.co/tp2807PV/image-png-4.png
// @grant none
// ==/UserScript==
!function(){"use strict";let e,t,n,o,s,i=[],l=[],a=new Array(30).fill(0),r=0,d=[],c=0,p={in:{bytes:[],lastTime:0,pps:"0.0",lastSizeStr:"0B",kbps:"0.0"},out:{bytes:[],lastTime:0,pps:"0.0",kbps:"0.0"}},u=0,f=0,m=0,h=-1,y=-1,b=!1;const g=document.createElement("div");g.style.cssText="position: absolute; left: 10px; top: 40%; display: flex; flex-direction: column; gap: 5px; z-index: 10000; pointer-events: none;",document.body.appendChild(g);const w=e=>{const t=document.createElement("div");return t.style.cssText="color:white;font-size:14px;font-family:'roboto condensed', sans-serif;font-weight:bold;background-color:rgba(0,0,0,0.4);padding:3px 5px;border-radius:5px;white-space:nowrap;width:fit-content;line-height:1.2;",t.innerHTML=e,g.appendChild(t),t},x=w("0FPS | 0.1% 0FPS | 0ms"),k=w("0ms | ↑0% | lerp 0ms"),v=w(""),E=w("in: 0B | 0.0kbps | 0.0pps<br>out: 0.0kbps | 0.0pps"),S=document.createElement("canvas"),F=window.devicePixelRatio||1;S.width=150*F,S.height=40*F,S.style.width="150px",S.style.height="40px",v.appendChild(S);const T=S.getContext("2d");T.scale(F,F);const B=e=>`display:none;position:fixed;z-index:10000;top:50%;transform:translateY(-50%);mix-blend-mode:difference;font-weight:bold;font-size:large;pointer-events:none;-webkit-text-stroke:0.25px;${e}`,M=document.body.appendChild(document.createElement("span")),$=document.body.appendChild(document.createElement("span")),C=document.createElement("span"),I=document.createElement("span");M.style=B("left:calc(50% + 100vw/30);color:#ff7f00;text-align:left;"),$.style=B("right:calc(50% + 100vw/30);color:#00C3FF;text-align:right;"),C.style=I.style="display:none;position:fixed;z-index:2;margin:6px 0 0 0;mix-blend-mode:difference;font-weight:bold;font-size:large;",C.style.right=I.style.left="15px";const L=document.querySelector("#ui-health-container");L&&L.append(C,I);const z=e=>{const t=Math.min(Math.abs(e),30);let n,o;return t<=15?(n=Math.floor(t/15*255),o=255):(n=255,o=Math.floor(255-(t-15)/15*255)),`rgb(${n},${o},0)`},q=(e,t)=>{const n=d.reduce((e,t)=>e+t,0)/(d.length||1),o=e-n,s=o>=0?`↑${Math.abs(o/n*100).toFixed(0)}%`:`↓${Math.abs(o/n*100).toFixed(0)}%`;k.style.color=e>=120?"#ff0000":e>=90?"#ff7f00":e>=60?"#ffff00":"#ffffff",k.innerHTML=`${e}ms | ${s} | lerp ${t}ms`},H=()=>{E.innerHTML=`in: ${p.in.lastSizeStr} | ${p.in.kbps} kbps | ${p.in.pps} pps<br>out: ${p.out.kbps} kbps | ${p.out.pps} pps`},P=()=>{t&&(t.close(),t=null),clearTimeout(s),k.innerHTML="0ms | ↑0% | lerp 0ms",k.style.color="white",E.innerHTML="in: 0B | 0.0kbps | 0.0pps<br>out: 0.0kbps | 0.0pps",a.fill(0),T.clearRect(0,0,S.width,S.height),p.in.bytes=[],p.out.bytes=[],u=0,f=0,c=0},W=()=>{T.clearRect(0,0,S.width,S.height);const e=a,t=e.length,n=150/t;if(0===m)e.forEach((e,t)=>{let o=e>=120?"#ff0000":e>=90?"#ff7f00":e>=60?"#ffff00":"#00ff00";T.fillStyle=o;const s=Math.min(e,400)/400*40;T.fillRect(t*n,40-s,n-1,s)});else{T.lineWidth=1.5,T.lineJoin="miter",T.lineCap="butt";for(let o=0;o<t-1;o++){const t=e[o],s=e[o+1],i=o*n+n/2,l=40-Math.min(t,400)/400*40,a=(o+1)*n+n/2,r=40-Math.min(s,400)/400*40,d=Math.abs(s-t);T.beginPath(),T.strokeStyle=z(d),T.moveTo(i,l),T.lineTo(a,r),T.stroke()}}},D=window.WebSocket;function A(){t&&t.close();t=new D(`wss://${{na:"usr",eu:"eur",asia:"asr",sa:"sa",ru:"russia"}[e]}.mathsiscoolfun.com:8001/ptc`),t.onopen=()=>{clearTimeout(s),R()},t.onmessage=()=>{o=Date.now();const e=o-n;r=e,d.push(e),d.length>15&&d.shift(),a.push(e),a.shift(),W(),q(r,f),setTimeout(R,500)},t.onclose=e=>{1005===e.code?P():1006===e.code&&(t=null,s=setTimeout(A,1e3))}}function R(){1===t?.readyState&&(n=Date.now(),t.send(new ArrayBuffer(1)))}window.WebSocket=function(e,t){const n=new D(e,t);if(!e.includes("ptc")){n.addEventListener("message",e=>{const t=performance.now();u>0&&(f=Math.round(t-u),q(r,f),p.in.pps=(1e3/(t-u)).toFixed(1)),u=t;let n=e.data instanceof ArrayBuffer?e.data.byteLength:e.data.size||0;p.in.lastSizeStr=n>=1024?(n/1024).toFixed(1)+" KB":n+" B",p.in.bytes.push({t:Date.now(),v:n}),H()});const e=n.send;n.send=function(t){const n=performance.now();return p.out.lastTime>0&&(p.out.pps=(1e3/(n-p.out.lastTime)).toFixed(1)),p.out.lastTime=n,p.out.bytes.push({t:Date.now(),v:t.byteLength||t.size||0}),H(),e.apply(this,arguments)}}return n},window.WebSocket.prototype=D.prototype;const N=()=>{window.requestAnimationFrame(()=>{const e=performance.now(),t=i.length>0?(e-i[i.length-1]).toFixed(1):0;for(;i.length>0&&i[0]<=e-1e3;)i.shift();i.push(e);const n=i.length;n>c&&(c=n),l.push(n),l.length>300&&l.shift(),x.style.color=c>0&&n<.85*c?"#ff0000":"white",x.innerHTML=`${n}FPS | 0.1% ${[...l].sort((e,t)=>e-t)[0]||0}FPS | ${t}ms`,N()})};N(),setInterval(()=>{const e=Date.now(),n=t=>{for(;t.length>0&&e-t[0].t>1e3;)t.shift();return(8*t.reduce((e,t)=>e+t.v,0)/1e3).toFixed(1)};t&&(p.in.kbps=n(p.in.bytes),p.out.kbps=n(p.out.bytes),H())},500),setInterval(()=>{const e=document.getElementById("ui-health-actual");if(!e||e.offsetWidth<=0)return void(M.style.display=$.style.display=C.style.display=I.style.display="none");const t=e.style.width.slice(0,-1),n=(.25*(parseFloat(document.getElementById("ui-boost-counter-0")?.querySelector(".ui-bar-inner").style.width.slice(0,-1)||0)+parseFloat(document.getElementById("ui-boost-counter-1")?.querySelector(".ui-bar-inner").style.width.slice(0,-1)||0))+.375*parseFloat(document.getElementById("ui-boost-counter-2")?.querySelector(".ui-bar-inner").style.width.slice(0,-1)||0)+.125*parseFloat(document.getElementById("ui-boost-counter-3")?.querySelector(".ui-bar-inner").style.width.slice(0,-1)||0)).toFixed(1);M.style.display=$.style.display=b?"block":"none",C.style.display=I.style.display=b?"none":"block",h===t&&y===n||(C.textContent=M.textContent=Number(t).toFixed(1),I.textContent=$.textContent=y=n,h=t)});window.addEventListener("load",()=>{const n=document.getElementsByClassName("btn-green btn-darken menu-option"),o=()=>{e=document.getElementById("server-select-main").value,A()};if(n.length>=4){for(let e=0;e<3;e++)n[e].onclick=o;n[3].onclick=()=>{e=document.getElementById("team-server-select").value,A()}}document.body.addEventListener("click",e=>{("btn-game-quit"===e.target.id||"btn-spectate-quit"===e.target.id||e.target.classList.contains("ui-stats-restart"))&&P()}),window.addEventListener("keydown",e=>{const t=e.key.toLowerCase();"i"===t&&(m=0===m?1:0,W()),"o"===t&&(b=!b)}),setInterval(()=>{const n=document.getElementById("ui-health-actual");n&&n.offsetWidth>0&&!t&&(e=document.getElementById("server-select-main")?.value||document.getElementById("team-server-select")?.value,A())},1e3),setInterval(()=>{["#leaderboard-front","#adunit",".ad-block-header.hide-on-mobile",".ad-block-right-center.hide-on-mobile",".ad-block-left-center.hide-on-mobile","#publift-widget-sticky_footer-styles","#ui-stats-ad-container-desktop","#ui-spectate-video-ad-container-desktop","#ui-spectate-ad-container-desktop",".ad-block-left-bottom",".ad-block-med-rect",".ad-block-leaderboard-bottom"].forEach(e=>document.querySelector(e)?.remove())},1e3)})}();