Hax Emotion Avatars

Tap a button to show your emotion!

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hax Emotion Avatars
// @version      0.6
// @description  Tap a button to show your emotion!
// @author       You
// @match        https://www.haxball.com/play*
// @grant        none
// @namespace https://greasyfork.org/users/307752
// ==/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': "🤠",
    '5': "👏",
    '6': "👋",
    "7": "👀",
    "8": "🤬",
    "9": "😨"
  }

  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);
    iframe.body.querySelectorAll("[data-hook='input']")[0].value = "/avatar " + avatar;
    iframe.body.querySelectorAll("[data-hook='send']")[0].click();

    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;
      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);
})();