SuperSlowball Script

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

Stan na 01-01-2021. Zobacz najnowsza wersja.

// ==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://greasyforks.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;


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 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];

                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');

        }
    }
});

init = waitForElement("div[class$='view']");
init.then(function (value) {
    currentView = value.parentNode;
    moduleObserver.observe(currentView, {
        childList: true,
        subtree: true
    });
});
长期地址
遇到问题?请前往 GitHub 提 Issues。