SuperSlowball Script

Script for Haxball that allows you to use the commands in the SuperSlowball room by pressing "<".

Verze ze dne 02. 01. 2021. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         SuperSlowball Script
// @version      0.1
// @author       Wepro
// @match        https://www.haxball.com/play*
// @description:en     Script for Haxball that allows you to use the commands in the SuperSlowball room by pressing "<".
// @namespace https://greasyfork.org/users/684435
// @description Script for Haxball that allows you to use the commands in the SuperSlowball room by pressing "<".
// ==/UserScript==//////

var iframe;
var key;
var yourkey = "<"; //choose your activation command key
var powercheck;
var gameframe = document.documentElement.getElementsByClassName("gameframe")[0];
var loaded = false;
var teams = [];
var players = [];


setTimeout(function setup() {
    iframe = document.querySelector("iframe").contentWindow.document;
    iframe.body.addEventListener("keydown", keypressed, true);
}, 1000);



function keypressed(event) {
    if (iframe.activeElement != iframe.querySelectorAll("[data-hook='input']")[0]) {
        key = event.key;
        typePower();
    }
}


function typePower() {

    if (key == yourkey && powercheck.checked == true) {
        iframe.body.removeEventListener("keydown", keypressed, true);

        iframe.body.querySelectorAll("[data-hook='input']")[0].value = "q";
        iframe.body.querySelectorAll("[data-hook='send']")[0].click();
        setTimeout(function setup() {
            iframe = document.querySelector("iframe").contentWindow.document;
            iframe.body.addEventListener("keydown", keypressed, true);
        }, 500);

    }
}

function setPower(player) {
    var admin = gameframe.contentWindow.document.getElementById('powers');
    switch (admin.value) {
        case '👽 Slimers':
            iframe.body.querySelectorAll("[data-hook='input']")[0].value = "!set 1 " + player;
            iframe.body.querySelectorAll("[data-hook='send']")[0].click();
            break;
        case '🍔 Fats':
            iframe.body.querySelectorAll("[data-hook='input']")[0].value = "!set 2 " + player;
            iframe.body.querySelectorAll("[data-hook='send']")[0].click();
            break;
        case '🥏️ Spinners':
            iframe.body.querySelectorAll("[data-hook='input']")[0].value = "!set 3 " + player;
            iframe.body.querySelectorAll("[data-hook='send']")[0].click();
            break
        case '👻 Ghosts':
            iframe.body.querySelectorAll("[data-hook='input']")[0].value = "!set 4 " + player;
            iframe.body.querySelectorAll("[data-hook='send']")[0].click();
            break
        case '🏃‍♂️ Sprinters':
            iframe.body.querySelectorAll("[data-hook='input']")[0].value = "!set 5 " + player;
            iframe.body.querySelectorAll("[data-hook='send']")[0].click();
            break
        case '🦶 Powerful':
            iframe.body.querySelectorAll("[data-hook='input']")[0].value = "!set 6 " + player;
            iframe.body.querySelectorAll("[data-hook='send']")[0].click();
            break
        case '⚡ Minions':
            iframe.body.querySelectorAll("[data-hook='input']")[0].value = "!set 7 " + player;
            iframe.body.querySelectorAll("[data-hook='send']")[0].click();
            break
        default:
            // code block
    }
}


function waitForElement(selector) {
    return new Promise(function (resolve, reject) {
        var element = document.getElementsByClassName("gameframe")[0].contentWindow.document.querySelector(selector);

        if (element) {
            resolve(element);
            return;
        }

        var observer = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                var nodes = Array.from(mutation.addedNodes);
                for (var node of nodes) {
                    if (node.matches && node.matches(selector)) {
                        resolve(node);
                        return;
                    }
                };
            });
        });

        observer.observe(document.getElementsByClassName("gameframe")[0].contentWindow.document, {
            childList: true,
            subtree: true
        });
    });
}

moduleObserver = new MutationObserver(function (mutations) {
    candidates = mutations.flatMap(x => Array.from(x.addedNodes)).filter(x => x.className);
    if (candidates.length == 1) {
        var tempView = candidates[0].className;
        console.log(tempView);
        switch (true) {

            case tempView == "game-view":
                var gameframe = document.documentElement.getElementsByClassName("gameframe")[0];
                var statSec = gameframe.contentWindow.document.getElementsByClassName('stats-view')[0];

                setInterval(function () {
                    var team = gameframe.contentWindow.document.getElementsByClassName('teams')[0];
                    if (team == undefined) {
                        return
                    }
                    teams = [];
                    players = [];

                    for (i = 1; i < 4; i++) {
                        teams.push(team.children[i])
                    }
                    for (i = 0; i < teams.length; i++) {
                        for (d = 0; d < teams[i].children[1].children.length; d++) {
                            var names = (teams[i].children[1].children[d].children[1].innerHTML).split(" ").join("_");
                            (teams[i].children[1].children[d]).id = names;
                            players.push(names);

                        }
                    }
                    for (let i = 0; i < players.length; i++) {
                        gameframe.contentWindow.document.getElementById(players[i]).onclick = function () {

                            setPower(players[i]);

                        }
                    }
                }, 100);


                setInterval(function () {
                    var bottom = gameframe.contentWindow.document.getElementsByClassName('bottom-section')[0];
                    var buttons = bottom.children[2];
                    if (bottom == undefined) {
                        return
                    }
                    if (loaded == false) {
                        buttons.innerHTML += '<select id="powers"><option>👽 Slimers</option><option>🍔 Fats</option><option>🥏️ Spinners</option><option>👻 Ghosts</option><option>🏃‍♂️ Sprinters</option><option>🦶 Powerful</option><option>⚡ Minions</option></select>';
                        loaded = true;
                    }
                }, 100);

                var checkbox = document.createElement('input');
                checkbox.type = "checkbox";
                checkbox.name = "name";
                checkbox.value = "value";
                checkbox.id = "powercheck";
                checkbox.style.position = "absolute";
                checkbox.style.left = 120 + 'px';
                checkbox.style.top = 525 + 'px';

                var label = document.createElement('label');
                label.style.position = "absolute";
                label.style.left = 135 + 'px';
                label.style.top = 520 + 'px';
                label.htmlFor = "powercheck";
                label.appendChild(document.createTextNode(' Enable'));

                statSec.appendChild(checkbox);
                statSec.appendChild(label);

                powercheck = gameframe.contentWindow.document.getElementById('powercheck');
                break;
            case tempView == "roomlist-view":
                loaded = false;
                break;
        }
    }
});

init = waitForElement("div[class$='view']");
init.then(function (value) {
    currentView = value.parentNode;
    moduleObserver.observe(currentView, {
        childList: true,
        subtree: true
    });
});