HueHanaejistla's Diep.io Multiboxing Script

now you can control several tanks, very cool! (use shift for quick invert on sync keys, v to toggle sync mouse, and f to toggle sync keys) UPDATED VERSION

// ==UserScript==
// @name         HueHanaejistla's Diep.io Multiboxing Script
// @version      2.0
// @description  now you can control several tanks, very cool! (use shift for quick invert on sync keys, v to toggle sync mouse, and f to toggle sync keys) UPDATED VERSION
// @author       HueHanaejistla!
// @match        *://diep.io/*
// @grant        GM_setValue
// @grant        GM_getValue
// @namespace    https://greasyfork.org/en/users/303221-huehanaejistla-inc
// ==/UserScript==

var documentFocus = 0;
var press = []

var cancelK = false;
var cancelM = false;
var gUI = false;


var getKeysDown = function(event) {
    switch (event.keyCode) {
        case 16:
            cancelK = false;
            GM_setValue("GM_SYNCK", cancelK);
            break;
        case 86:
            cancelK = cancelK ^ true
            GM_setValue("GM_SYNCK", cancelK);
            break;
        case 70:
            cancelM = cancelM ^ true
            GM_setValue("GM_SYNCM", cancelM);
            break;
        case 82:
            gUI = gUI ^true;
            if (gUI) {
                mainDivElement.innerHTML = mainDiv;
            } else {
                mainDivElement.innerHTML = ``;
            }
            break;
    }
    var str = event.keyCode.toString()
    if (press.indexOf(str + ".1") === -1 && press.indexOf(str + ".0") === -1) {
            press.push(str + ".1");
    }
    if (press.indexOf(str + ".0") !== -1) {
        press[press.indexOf(str + ".0")] = str + ".1"
    }
    GM_setValue("GM_pressArray", press);

}

const getKeysUp = function(event) {
    switch (event.keyCode) {
        case 16:
            cancelK = true;
            GM_setValue("GM_SYNCK", cancelK);
            break;
    }
    var str = event.keyCode.toString()
    if (press.indexOf(str + ".1") !== -1) {
        press[press.indexOf(str + ".1")] = str + ".0";
    }
    GM_setValue("GM_pressArray", press);
}

var mouseArray = []

const getMouseDown = function(mouse) {
    switch (mouse.button) {
        case 1:
            mouseArray[1] = 1
            break;
        case 0:
            mouseArray[0] = 1
            break;
        case 2:
            mouseArray[2] = 1
            break;
    }
    GM_setValue("GM_clicks", mouseArray);
}

const getMouseUp = function(mouse) {
        switch (mouse.button) {
        case 1:
            mouseArray[1] = 0
            break;
        case 0:
            mouseArray[0] = 0
            break;
        case 2:
            mouseArray[2] = 0
            break;
    }
    GM_setValue("GM_clicks", mouseArray);
}

const getMouseInGeneral = function(event) {
    screenBoxCX = window.innerWidth / 2;
    screenBoxCY = window.innerHeight / 2;
    var mouseClientX = event.clientX - screenBoxCX;
    var mouseClientY = event.clientY - screenBoxCY;

    var ratioX = mouseClientX / screenBoxCX;
    var ratioY = mouseClientY / Math.abs(mouseClientX);
    GM_setValue("GM_mouseRatio", [ratioX, ratioY]);
}

document.addEventListener('keydown', getKeysDown);
document.addEventListener('keyup', getKeysUp);
document.addEventListener('mousedown', getMouseDown);
document.addEventListener('mouseup', getMouseUp);
document.addEventListener('mousemove', getMouseInGeneral);

function pressKey(code, upordown) {
    var eventObj;
    if (upordown == 1) {
        eventObj = document.createEvent("Events"); eventObj.initEvent("keydown", true, true); eventObj.keyCode = code; window.dispatchEvent(eventObj);
    } else {
        eventObj = document.createEvent("Events"); eventObj.initEvent("keyup", true, true); eventObj.keyCode = code; window.dispatchEvent(eventObj);
    }
}

var screenBoxCX = window.innerWidth / 2;
var screenBoxCY = window.innerHeight / 2;

function sendMouse(x, y) {
    canvas.dispatchEvent(new MouseEvent('mousemove', { 'clientX': x + screenBoxCX, 'clientY': y + screenBoxCY}));
}

function sendClick(x, y, button, ud) {
    if (ud == 1) {
        canvas.dispatchEvent(new MouseEvent('mousedown', { 'clientX': x + screenBoxCX, 'clientY': y + screenBoxCY, 'button': button }));
    } else if (ud == 0) {
        canvas.dispatchEvent(new MouseEvent('mouseup', { 'clientX': x + screenBoxCX, 'clientY': y + screenBoxCY, 'button': button }));
    }
}

var overlay = document.createElement("div");
document.body.appendChild(overlay);

var overlayHTML = `

<style>

.main {
pointer-events: none;
position: fixed;
top: 10px;
left: 10px;
font-family: 'Comic Sans MS', cursive, sans-serif;
color: #FFFFFF;
font-style: normal;
font-variant: normal;
}

</style>

<div class="main" id="all">
<p> HueHanaejistla's multiboxing script</p><p id="syncmouse"> Sync Mouse = ${string1} [F]</p><p id="synckey"> Sync Keys = ${string2} [V]</p><p> Overlay [R]</p>
</div>
`

var mainDiv = `<p> HueHanaejistla's multiboxing script</p><p id="syncmouse"> Sync Mouse = ${string1} [F]</p><p id="synckey"> Sync Keys = ${string2} [V]</p><p> Overlay [R]</p>`

overlay.innerHTML = overlayHTML;

var mainDivElement = document.getElementById('all');
var string1 = "nah"
var string2 = "nah"
var master = false;

const tabLoop = function() {
    screenBoxCX = window.innerWidth / 2;
    screenBoxCY = window.innerHeight / 2;
    var b = GM_getValue("GM_SYNCK");
    var a = GM_getValue("GM_SYNCM");
    if (a) {
        string1 = "yeah"
    } else {
        string1 = "nah"
    }

    if (b) {
        string2 = "yeah"
    } else {
        string2 = "nah"
    }

    var syncmouses = document.getElementById('syncmouse');
    var synckeys = document.getElementById('synckey');
    if (syncmouses != null && synckeys != null) {
        syncmouses.innerHTML = `Sync Mouse = ${string1} [F]`
        synckeys.innerHTML = `Sync Keys = ${string2} [V]`
    }

    var keysPress = GM_getValue("GM_pressArray");
    var mousesPress = GM_getValue("GM_clicks");
    var ratioArray = GM_getValue("GM_mouseRatio");

    master = Boolean(document.hasFocus());
    var i = 0;
    while (i <= keysPress.length - 1) {
        if (!master && b == true) {
            var placeholder = keysPress[i].split('.')
            pressKey(parseInt(placeholder[0]), parseInt(placeholder[1]));
        }
        i++
    }
    i = 0;

    if (!master && a == true) {
        sendMouse(ratioArray[0] * screenBoxCX, ratioArray[1] * Math.abs(ratioArray[0] * screenBoxCX));
        while (i < mousesPress.length) {
            sendClick(ratioArray[0] * screenBoxCX, ratioArray[1] * Math.abs(ratioArray[0] * screenBoxCX), i, mousesPress[i])
            i++
        }
    }
}
setInterval(tabLoop, 50);