Sploop.io Zoom hack

Allows to change zoom of the game using mouse wheels

目前为 2022-06-22 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Sploop.io Zoom hack
  3. // @author Murka
  4. // @description Allows to change zoom of the game using mouse wheels
  5. // @icon https://sploop.io/img/ui/favicon.png
  6. // @version 0.3
  7. // @match *://sploop.io/*
  8. // @run-at document-start
  9. // @grant none
  10. // @license MIT
  11. // @namespace https://greasyfork.org/users/919633
  12. // ==/UserScript==
  13. /* jshint esversion:6 */
  14.  
  15. (function() {
  16. "use strict";
  17.  
  18. const log = console.log;
  19. function createHook(target, prop, callback) {
  20. const symbol = Symbol(prop);
  21. Object.defineProperty(target, prop, {
  22. get() {
  23. return this[symbol];
  24. },
  25. set(value) {
  26. callback(this, symbol, value);
  27. },
  28. configurable: true
  29. })
  30. }
  31.  
  32. function findKey(target, value) {
  33. for (const key in target) {
  34. if (target[key] === value) return key;
  35. }
  36. return null;
  37. }
  38.  
  39. const CONFIG = {
  40. maxWidth: {
  41. key: null,
  42. value: 1824 + 100 * 6
  43. },
  44. maxHeight: {
  45. key: null,
  46. value: 1026 + 100 * 6
  47. }
  48. };
  49.  
  50. // "max_players", is the only way to get access to the max_width and max_height properties
  51. // otherwise I would have to hook setTransform or Math.max
  52. createHook(Object.prototype, "max_players", function(that, symbol, value) {
  53. that[symbol] = value;
  54.  
  55. const { maxWidth, maxHeight } = CONFIG;
  56. maxWidth.key = findKey(that, 1824);
  57. maxHeight.key = findKey(that, 1026);
  58.  
  59. Object.defineProperty(that, maxWidth.key, { get: () => maxWidth.value })
  60. Object.defineProperty(that, maxHeight.key, { get: () => maxHeight.value })
  61. })
  62.  
  63. let wheels = 0;
  64. const scaleFactor = 150;
  65. window.addEventListener("wheel", function(event) {
  66. const { maxWidth, maxHeight } = CONFIG;
  67. if (event.target.id !== "game-canvas" || maxWidth.key === null || maxHeight.key === null) return;
  68.  
  69. // Used to create a small gap, so users could easily find the default scale
  70. if (maxWidth.value === 1824 && maxHeight.value === 1026) wheels += 1;
  71. if (wheels % 5 !== 0) return;
  72.  
  73. const zoom = event.deltaY > 0 ? -scaleFactor : scaleFactor;
  74. maxWidth.value += zoom;
  75. maxHeight.value += zoom;
  76. window.dispatchEvent(new Event("resize"));
  77. })
  78.  
  79. })();