Haxball Avatar Hotkey

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

// ==UserScript==

// @name haxball hotkeys avatar


// @namespace Violentmonkey Scripts
// @match https://www.haxball.com/play*
// @grant none
// @version 1.0
// @author -
// @description 3/30/2024, 8:26:49 PM
// ==/UserScript==
// ==UserScript==
// @name Hax Emotion Avatars HOTKEYS
// @version 0.6
// @description Tap a button to show your emotion!
// @author You
// @match https://www.haxball.com/play*
// @grant none
// ==/UserScript==

(function() {
"use strict";
// SET DURATION FOR AVATAR TO APPEAR
var defaultDuration = 800;
// SET DEFAULT AVATAR & change emojis - can be extended
// and changed by following the same syntax.
var avatars = {
'default': "🤠",
'w': "👏",
'a': "👋",
's': "👀",
'd': "🤬",
'x': "😨"
}

function process(key) {
var avatar = avatars[key];
var duration = defaultDuration;
if (avatar) {
setAvatar(avatar);
if (reset != undefined) {
clearTimeout(reset);
}
reset = setTimeout(function() {
setAvatar(avatars['default']);
}, duration);
}
}

var reset;

// code to change the avatar


function setAvatar(avatar) {
console.log("avatar: " + avatar);
var inputElement = iframe.body.querySelectorAll("[data-hook='input']")[0];
inputElement.value = "/avatar " + avatar;

// Simulate pressing Enter key


var enterKeyEvent = new KeyboardEvent("keydown", {
key: "Enter",
keyCode: 13,
code: "Enter",
which: 13,
bubbles: true
});
inputElement.dispatchEvent(enterKeyEvent);

var notices = iframe.body.getElementsByClassName("notice");


for (var i = 0; i < notices.length; i++) {
var notice = notices[i];
if (notice.innerHTML == "Avatar set") {
notice.parentNode.removeChild(notice);
}
}
}

// listens to key presses.


var listener = function(event) {
if (iframe.activeElement != iframe.querySelectorAll("[data-hook='input']")[0])
{
const key = event.key.toLowerCase(); // Convert to lowercase for uniformity
process(key);
}
};

//document.activeElement
var iframe;
setTimeout(function() {
iframe = document.querySelector("iframe").contentWindow.document;
iframe.body.addEventListener("keydown", listener, true);
console.log("Setup complete");
}, 3000);
})();

You might also like