Afk script

made with much love

  1. // ==UserScript==
  2. // @name Afk script
  3. // @description made with much love
  4. // @version 0.1.5
  5. // @author Cazka#1820
  6. // @match *://diep.io/*
  7. // @grant GM_addStyle
  8. // @namespace https://greasyfork.org/users/541070
  9. // ==/UserScript==
  10. /*
  11. * C L A S S E S
  12. */
  13. class Gui {
  14. constructor(title) {
  15. this._colors = ['#E8B18A', '#E666EA', '#9566EA', '#6690EA', '#E7D063', '#EA6666', '#92EA66', '#66EAE6'];
  16. this._buttons = [];
  17. this._title = title;
  18. this._gui;
  19. this._guiHead;
  20. this._guiBody;
  21. this._init();
  22. this._enableShortcuts();
  23. }
  24. _init() {
  25. const nonce = `a${(Math.random() * 1e5) | 0}`;
  26. GM_addStyle(
  27. `.${nonce} button{display:block;font-family:Ubuntu;color:#fff;text-shadow:-.1em -.1em 0 #000,0 -.1em 0 #000,.1em -.1em 0 #000,.1em 0 0 #000,.1em .1em 0 #000,0 .1em 0 #000,-.1em .1em 0 #000,-.1em 0 0 #000;opacity:.8;border:0;padding:.3em .5em;width:100%;transition:all .15s}.${nonce}{top:0;left:0;position:absolute}.${nonce} button:active:not([disabled]){filter:brightness(.9)}.${nonce} button:hover:not([disabled]):not(:active){filter:brightness(1.1)}`
  28. );
  29. this._gui = document.createElement('div');
  30. this._guiHead = document.createElement('div');
  31. this._guiBody = document.createElement('div');
  32. this._gui.className = `${nonce}`;
  33. this._guiBody.style.display = 'block';
  34. document.body.appendChild(this._gui);
  35. this._gui.appendChild(this._guiHead);
  36. this._gui.appendChild(this._guiBody);
  37. this._addButton(this._guiHead, this._title, () => {
  38. if (this._guiBody.style.display === 'block') {
  39. this._guiBody.style.display = 'none';
  40. } else {
  41. this._guiBody.style.display = 'block';
  42. }
  43. });
  44. }
  45. addButton(text, onclick, keyCode) {
  46. return this._addButton(this._guiBody, text, onclick, keyCode);
  47. }
  48. removeButton(button) {
  49. button.remove();
  50. button.active = false;
  51. }
  52. reset() {
  53. const head = this._buttons[0];
  54. this._buttons.forEach((x, i) => {
  55. if (i === 0) return;
  56. this.removeButton(x);
  57. });
  58. this._buttons = [head];
  59. }
  60. _addButton(parent, text, onclick, keyCode) {
  61. const button = document.createElement('button');
  62. button.innerHTML = text;
  63. button.keyCode = keyCode;
  64. button.onclick = onclick;
  65. button.style['background-color'] = this._colors[this._buttons.length % this._colors.length];
  66. button.addEventListener('contextmenu', (e) => e.preventDefault());
  67. parent.appendChild(button);
  68. this._buttons.push(button);
  69. return button;
  70. }
  71. _enableShortcuts() {
  72. document.addEventListener('keydown', (event) => {
  73. if (document.getElementById('textInputContainer').style.display === 'block') return;
  74. this._buttons.forEach((button) => {
  75. if (button.keyCode === event.code) button.onclick();
  76. });
  77. });
  78. }
  79. }
  80. class Minimap {
  81. constructor() {
  82. this._minimapWidth;
  83. this._minimapHeight;
  84. this._x00;
  85. this._y00;
  86. this._pointX;
  87. this._pointY;
  88. this._viewportWidth;
  89. this._viewportHeight;
  90. this._minimapHook();
  91. this._arrowHook();
  92. }
  93. get x() {
  94. return (this._pointX - this._x00) / this._minimapWidth;
  95. }
  96. get y() {
  97. return (this._pointY - this._y00) / this._minimapHeight;
  98. }
  99. _minimapHook() {
  100. let setTransformArgs;
  101. const onsetTransform = (args) => {
  102. if (args[0] === args[3]) setTransformArgs = args;
  103. };
  104. const onstrokeRect = () => {
  105. if (setTransformArgs) {
  106. this._minimapWidth = setTransformArgs[0];
  107. this._minimapHeight = setTransformArgs[3];
  108. this._x00 = setTransformArgs[4];
  109. this._y00 = setTransformArgs[5];
  110. setTransformArgs = undefined;
  111. }
  112. };
  113. this._ctxHook('setTransform', onsetTransform);
  114. this._ctxHook('strokeRect', onstrokeRect);
  115. }
  116. _arrowHook() {
  117. let index = 0;
  118. const stack = Array(4);
  119. let pointA;
  120. let pointB;
  121. let pointC;
  122. const calculatePos = () => {
  123. const side1 = ((pointA[0] - pointB[0]) ** 2 + (pointA[1] - pointB[1]) ** 2) ** 0.5;
  124. const side2 = ((pointA[0] - pointC[0]) ** 2 + (pointA[1] - pointC[1]) ** 2) ** 0.5;
  125. const side3 = ((pointB[0] - pointC[0]) ** 2 + (pointB[1] - pointC[1]) ** 2) ** 0.5;
  126. if (~~side1 == ~~side2 && ~~side2 == ~~side3) return;
  127. this._pointX = (pointA[0] + pointB[0] + pointC[0]) / 3;
  128. this._pointY = (pointA[1] + pointB[1] + pointC[1]) / 3;
  129. };
  130. const onbeginPath = () => {
  131. index = 0;
  132. stack[index++] = 0;
  133. };
  134. const onmoveTo = (args) => {
  135. if (index == 1 && stack[index - 1] == 0) {
  136. stack[index++] = 1;
  137. pointA = args;
  138. return;
  139. }
  140. index = 0;
  141. };
  142. const onlineTo = (args) => {
  143. if (index == 2 && stack[index - 1] == 1) {
  144. stack[index++] = 2;
  145. pointB = args;
  146. return;
  147. }
  148. if (index == 3 && stack[index - 1] == 2) {
  149. stack[index++] = 2;
  150. pointC = args;
  151. return;
  152. }
  153. index = 0;
  154. };
  155. const onfill = () => {
  156. if (index == 4 && stack[index - 1] == 2) {
  157. calculatePos();
  158. return;
  159. }
  160. index = 0;
  161. };
  162. this._ctxHook('beginPath', onbeginPath);
  163. this._ctxHook('moveTo', onmoveTo);
  164. this._ctxHook('lineTo', onlineTo);
  165. this._ctxHook('fill', onfill);
  166. }
  167. _ctxHook(method, callback) {
  168. const target = window.CanvasRenderingContext2D.prototype;
  169. target[method] = new Proxy(target[method], {
  170. apply(target, thisArg, args) {
  171. callback(args);
  172. return target.apply(thisArg, args);
  173. },
  174. });
  175. }
  176. }
  177. class Player {
  178. constructor() {
  179. this._minimap = new Minimap();
  180. this._mouse = { x: NaN, y: NaN };
  181. }
  182. get x() {
  183. return this._minimap.x;
  184. }
  185. get y() {
  186. return this._minimap.y;
  187. }
  188. goto(x, y) {
  189. const dX = x - this.x;
  190. const dY = y - this.y;
  191. const len = Math.sqrt(dX ** 2 + dY ** 2);
  192. if (dX > 0) {
  193. unsafeWindow.input.keyDown('68');
  194. unsafeWindow.input.keyUp('65');
  195. } else {
  196. unsafeWindow.input.keyDown('65');
  197. unsafeWindow.input.keyUp('68');
  198. }
  199. if (dY > 0) {
  200. unsafeWindow.input.keyDown('83');
  201. unsafeWindow.input.keyUp('87');
  202. } else {
  203. unsafeWindow.input.keyDown('87');
  204. unsafeWindow.input.keyUp('83');
  205. }
  206. }
  207. }
  208. /*
  209. * H E L P E R F U N C T I O N S
  210. */
  211. function onbtnAfk() {
  212. this.active = !this.active;
  213. if (this.active) {
  214. this.x = player.x;
  215. this.y = player.y;
  216. this.innerHTML = 'AFK: ON';
  217. } else {
  218. unsafeWindow.input.keyUp('65');
  219. unsafeWindow.input.keyUp('68');
  220. unsafeWindow.input.keyUp('87');
  221. unsafeWindow.input.keyUp('83');
  222. this.innerHTML = 'AFK: OFF';
  223. }
  224. }
  225. function onbtnFreezeMouse() {
  226. this.active = !this.active;
  227. if (this.active) {
  228. this.innerHTML = 'Freeze Mouse: ON';
  229. } else {
  230. this.innerHTML = 'Freeze Mouse: OFF';
  231. }
  232. }
  233. function onbtnRepelNecro() {
  234. this.active = !this.active;
  235. if (this.active) {
  236. let repelTime = 25 * 1000;
  237. unsafeWindow.input.keyDown('16');
  238. this.repelInterval = setInterval(() => {
  239. unsafeWindow.input.keyDown('16');
  240. setTimeout(() => unsafeWindow.input.keyUp('16'), repelTime);
  241. }, 2 * repelTime + 1300);
  242. this.innerHTML = 'Repel Necro: ON';
  243. } else {
  244. clearInterval(this.repelInterval);
  245. unsafeWindow.input.keyUp('16');
  246. this.innerHTML = 'Repel Necro: OFF';
  247. }
  248. }
  249. function onbtnRepelOverlord() {
  250. this.active = !this.active;
  251. if (this.active) {
  252. let repelTime = 60 * 1000;
  253. unsafeWindow.input.keyDown('16');
  254. this.repelInterval = setInterval(() => {
  255. unsafeWindow.input.keyUp('16');
  256. setTimeout(() => unsafeWindow.input.keyDown('16'), 3000);
  257. }, repelTime);
  258. this.innerHTML = 'Repel Overlord: ON';
  259. } else {
  260. clearInterval(this.repelInterval);
  261. unsafeWindow.input.keyUp('16');
  262. this.innerHTML = 'Repel Overlord: OFF';
  263. }
  264. }
  265. function onbtnUpAndDown() {
  266. this.active = !this.active;
  267. if (this.active) {
  268. this.down = true;
  269. this.x = player.x;
  270. this.innerHTML = 'Up and Down: ON';
  271. } else {
  272. unsafeWindow.input.keyUp('65');
  273. unsafeWindow.input.keyUp('68');
  274. unsafeWindow.input.keyUp('87');
  275. unsafeWindow.input.keyUp('83');
  276. this.innerHTML = 'Up and Down: OFF';
  277. }
  278. }
  279. function onbtnDiscord() {
  280. window.open('https://discord.gg/5q2E3Sx');
  281. }
  282. /*
  283. * M A I N
  284. */
  285. const gui = new Gui('AFK by Cazka');
  286. const player = new Player();
  287. let btnAfk = gui.addButton('AFK: OFF', onbtnAfk, 'KeyQ');
  288. let btnFreezeMouse = gui.addButton('Freeze Mouse: OFF', onbtnFreezeMouse, 'KeyG');
  289. let btnRepelNecro = gui.addButton('Repel Necro: OFF', onbtnRepelNecro, 'KeyR');
  290. let btnRepelOverlord = gui.addButton('Repel Overlord: OFF', onbtnRepelOverlord, 'KeyT');
  291. let btnUpAndDown = gui.addButton('Up and Down: OFF', onbtnUpAndDown);
  292. let btnDiscord = gui.addButton('Discord', onbtnDiscord);
  293. unsafeWindow.requestAnimationFrame = new Proxy(unsafeWindow.requestAnimationFrame, {
  294. apply: function (target, thisArg, args) {
  295. if (btnAfk.active) player.goto(btnAfk.x, btnAfk.y);
  296. else if (btnUpAndDown.active) {
  297. if (btnUpAndDown.down) {
  298. player.goto(btnUpAndDown.x, 1);
  299. if (player.y >= 0.95) btnUpAndDown.down = false;
  300. } else {
  301. player.goto(btnUpAndDown.x, 0);
  302. if (player.y <= 1 - 0.95) btnUpAndDown.down = true;
  303. }
  304. }
  305. return target.apply(thisArg, args);
  306. },
  307. });
  308. (function freezeMouse() {
  309. const canvas = document.getElementById('canvas');
  310. canvas.onmousemove = new Proxy(canvas.onmousemove, {
  311. apply(target, thisArg, args) {
  312. if (btnFreezeMouse?.active) return;
  313. target.apply(thisArg, args);
  314. },
  315. });
  316. })();