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.4
  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. delete Object.prototype.max_players;
  54. that.max_players = value;
  55.  
  56. const { maxWidth, maxHeight } = CONFIG;
  57. maxWidth.key = findKey(that, 1824);
  58. maxHeight.key = findKey(that, 1026);
  59.  
  60. Object.defineProperty(that, maxWidth.key, { get: () => maxWidth.value })
  61. Object.defineProperty(that, maxHeight.key, { get: () => maxHeight.value })
  62. })
  63.  
  64. let wheels = 0;
  65. const scaleFactor = 150;
  66. window.addEventListener("wheel", function(event) {
  67. const { maxWidth, maxHeight } = CONFIG;
  68. if (event.target.id !== "game-canvas" || maxWidth.key === null || maxHeight.key === null) return;
  69.  
  70. // Used to create a small gap, so users could easily find the default scale
  71. if (maxWidth.value === 1824 && maxHeight.value === 1026) wheels += 1;
  72. if (wheels % 5 !== 0) return;
  73.  
  74. const zoom = event.deltaY > 0 ? -scaleFactor : scaleFactor;
  75. maxWidth.value += zoom;
  76. maxHeight.value += zoom;
  77. window.dispatchEvent(new Event("resize"));
  78. })
  79.  
  80. })();