astr.io linesplit overlay

Linescript overlay for astr.io by Hixe

  1. // ==UserScript==
  2. // @name astr.io linesplit overlay
  3. // @namespace eeWynell#4980
  4. // @version 1.3
  5. // @description Linescript overlay for astr.io by Hixe
  6. // @author eeWynell#4980
  7. // @match https://astr.io/
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11.  
  12. 'use strict';
  13.  
  14. let $ = window.$;
  15. const KEY_TABLE = { 0: "", 8: "BACKSPACE", 9: "TAB", 12: "CLEAR", 13: "ENTER", 16: "SHIFT", 17: "CTRL", 18: "ALT", 19: "PAUSE", 20: "CAPSLOCK", 27: "ESC", 32: "SPACE", 33: "PAGEUP", 34: "PAGEDOWN", 35: "END", 36: "HOME", 37: "LEFT", 38: "UP", 39: "RIGHT", 40: "DOWN", 44: "PRTSCN", 45: "INS", 46: "DEL", 65: "A", 66: "B", 67: "C", 68: "D", 69: "E", 70: "F", 71: "G", 72: "H", 73: "I", 74: "J", 75: "K", 76: "L", 77: "M", 78: "N", 79: "O", 80: "P", 81: "Q", 82: "R", 83: "S", 84: "T", 85: "U", 86: "V", 87: "W", 88: "X", 89: "Y", 90: "Z", 91: "WIN", 92: "WIN", 93: "CONTEXTMENU", 96: "NUM 0", 97: "NUM 1", 98: "NUM 2", 99: "NUM 3", 100: "NUM 4", 101: "NUM 5", 102: "NUM 6", 103: "NUM 7", 104: "NUM 8", 105: "NUM 9", 106: "NUM *", 107: "NUM +", 109: "NUM -", 110: "NUM .", 111: "NUM /", 144: "NUMLOCK", 145: "SCROLLLOCK" };
  16. class Settings {
  17. constructor(name, default_value) {
  18. this.settings = {};
  19. this.name = name;
  20. this.load(default_value);
  21. }
  22. load(default_value) {
  23. let raw_settings = localStorage.getItem(this.name);
  24. this.settings = Object.assign({}, default_value, this.settings, JSON.parse(raw_settings));
  25. }
  26. save() {
  27. localStorage.setItem(this.name, JSON.stringify(this.settings));
  28. }
  29. set(key, value) {
  30. if (value === undefined) {
  31. this.settings = key;
  32. } else {
  33. this.settings[key] = value;
  34. }
  35. this.save();
  36. }
  37. get(key) {
  38. if (key === undefined) {
  39. return this.settings;
  40. } else {
  41. return this.settings[key];
  42. }
  43. }
  44. }
  45.  
  46. let settings = new Settings("linesplit_overlay", {
  47. enabled: true,
  48. binding: null
  49. });
  50. let current_key = false, pressing = false;
  51.  
  52. $("head").append(`<style>
  53. .hotkey-input-2.selected {
  54. background-color: #ff4;
  55. color: #444;
  56. }
  57. .hotkey-input-2:hover:not(.selected) {
  58. background-color: #f1a02d;
  59. }
  60. .hotkey-input-2 {
  61. background-color: #df901c;
  62. color: #fff;
  63. cursor: pointer;
  64. text-align: center;
  65. min-width: 40px;
  66. max-width: 60px;
  67. height: 18px;
  68. line-height: 18px;
  69. vertical-align: middle;
  70. border-radius: 9px;
  71. right: 5px;
  72. position: absolute;
  73. display: inline-block;
  74. padding: 0 5px;
  75. overflow: hidden;
  76. opacity: 1;
  77. }
  78. </style>`);
  79.  
  80. let [w, h] = [, window.innerHeight];
  81. $("body").append(`<div id="linesplit_overlay" style="z-index: 9999;display:${settings.get("enabled") ? "block" : "none"}">
  82. <div id="point-top" style="border: 2px solid white; border-radius: 50%; width: 10px; height: 10px; position: fixed; left: ${window.innerWidth / 2}px; top: ${0}px; transform: translate(-50%, -50%);"></div>
  83. <div id="point-right" style="border: 2px solid white; border-radius: 50%; width: 10px; height: 10px; position: fixed; left: ${window.innerWidth}px; top: ${window.innerHeight / 2}px; transform: translate(-50%, -50%);"></div>
  84. <div id="point-bottom" style="border: 2px solid white; border-radius: 50%; width: 10px; height: 10px; position: fixed; left: ${window.innerWidth / 2}px; top: ${window.innerHeight}px; transform: translate(-50%, -50%);"></div>
  85. <div id="point-left" style="border: 2px solid white; border-radius: 50%; width: 10px; height: 10px; position: fixed; left: ${0}px; top: ${window.innerHeight / 2}px; transform: translate(-50%, -50%);"></div>
  86. </div>`);
  87.  
  88. $(".dash-tab-settings").click(function(e) {
  89. $("#roleSettings").css("display", "block");
  90. $("#cLinesplitOverlay").removeAttr("disabled").parent().parent().css("display", "block");
  91. });
  92.  
  93. $(".hotkey-col").eq(1).append(`Linesplit overlay <div id="keyLinesplitOverlay" class="hotkey-input-2"></div><br>`);
  94. $("#roleSettings").append(`<div class="role-setting"><label><input id="cLinesplitOverlay" type="checkbox"}><span> Linesplit overlay</span></label><br></div>`);
  95.  
  96. $("#cLinesplitOverlay").change(function(e) {
  97. let enabled = $(this).is(':checked');
  98. settings.set("enabled", enabled);
  99. $("#linesplit_overlay").css("display", enabled ? "block" : "none");
  100. });
  101.  
  102. if (settings.get('enabled')) $("#cLinesplitOverlay").attr("checked", "");
  103.  
  104. $("#keyLinesplitOverlay").html(KEY_TABLE[settings.get("binding")]);
  105.  
  106. $("#keyLinesplitOverlay").click(function(e) {
  107. $(this).addClass("selected");
  108. current_key = true;
  109. });
  110.  
  111. $("#keyLinesplitOverlay").contextmenu(function(e) {
  112. $(this).addClass("selected");
  113. settings.set("binding", null);
  114. $(this).html("");
  115. setTimeout(() => {
  116. $(this).removeClass("selected");
  117. }, 50);
  118. current_key = false;
  119. return false;
  120. });
  121.  
  122. $(window).keyup(function(e) {
  123. if (e.keyCode == settings.get("binding")) {
  124. $("#linesplit_overlay").css("display", settings.get("enabled") ? "block" : "none");
  125. pressing = false;
  126. }
  127. });
  128.  
  129. $(window).keydown(function(e) {
  130. if (current_key) {
  131. $("#keyLinesplitOverlay").html(KEY_TABLE[e.keyCode]);
  132. settings.set("binding", e.keyCode);
  133. setTimeout(() => {
  134. $("#keyLinesplitOverlay").removeClass("selected");
  135. }, 50);
  136. current_key = false;
  137. } else {
  138. if (e.keyCode == settings.get("binding") && document.activeElement == document.body) {
  139. $("#linesplit_overlay").css("display", !settings.get("enabled") ? "block" : "none");
  140. pressing = true;
  141. }
  142. }
  143. });
  144.  
  145. $(window).resize(function(e) {
  146. let [w, h] = [window.innerWidth, window.innerHeight];
  147. $("#point-top").css("left", `${window.innerWidth / 2}px`).css("top", `${0}px`);
  148. $("#point-right").css("left", `${window.innerWidth}px`).css("top", `${window.innerHeight / 2}px`);
  149. $("#point-bottom").css("left", `${window.innerWidth / 2}px`).css("top", `${window.innerHeight}px`);
  150. $("#point-left").css("left", `${0}px`).css("top", `${window.innerHeight / 2}px`);
  151. });