Time Control

Script allowing you to control time.

当前为 2024-03-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Time Control
  3. // @description Script allowing you to control time.
  4. // @icon https://parsefiles.back4app.com/JPaQcFfEEQ1ePBxbf6wvzkPMEqKYHhPYv8boI1Rc/ce262758ff44d053136358dcd892979d_low_res_Time_Machine.png
  5. // @namespace mailto:lucaszheng2011@outlook.com
  6. // @version 1.2.2
  7. // @author lucaszheng
  8. // @license MIT
  9. //
  10. // @match *://*/*
  11. // @grant unsafeWindow
  12. // @inject-into page
  13. // @run-at document-start
  14. // ==/UserScript==
  15.  
  16. (window => {
  17. "use strict";
  18. let scale = 1, pristine = true;
  19. /** @type {null | number} */
  20. let timeJump = null;
  21.  
  22. let timeSync = false;
  23. let debug = false;
  24.  
  25. const { isFinite,
  26. Reflect: {
  27. apply, construct,
  28. setPrototypeOf
  29. },
  30. Object: {
  31. defineProperty,
  32. freeze
  33. },
  34. console: {
  35. trace: log
  36. }
  37. } = window;
  38.  
  39. function update() {
  40. for (let idx = 0; idx < updaters.length; idx++) {
  41. updaters[idx]();
  42. }
  43. }
  44.  
  45. const time = freeze({
  46. /**
  47. * @param {number} newTime
  48. */
  49. jump(newTime) {
  50. if (newTime == null) return;
  51. pristine = false;
  52. timeJump = +newTime;
  53. update();
  54. timeJump = null;
  55. },
  56.  
  57. sync(resetScale = true) {
  58. if (pristine) return;
  59. if (resetScale) scale = 1;
  60. timeSync = true;
  61. update();
  62. timeSync = false;
  63. pristine = scale === 1;
  64. },
  65.  
  66. get debug() { return debug; },
  67. set debug(value) { debug = !!value; },
  68.  
  69. get now() { return apply(date.now, DateConstructor, []); },
  70. set now(value) { time.jump(value); },
  71.  
  72. get pristine() { return pristine; },
  73. set pristine(value) { if (value) time.sync(); },
  74.  
  75. get real() { return apply(date.realTime, DateConstructor, []); },
  76.  
  77. get scale() { return scale; },
  78. set scale(value) { pristine = false; update(); scale = +value; }
  79. });
  80. defineProperty(window, 'time', {
  81. value: time,
  82. writable: true,
  83. enumerable: false,
  84. configurable: true
  85. });
  86.  
  87. /** @type {(() => void)[]} */
  88. const updaters = [];
  89.  
  90. /**
  91. * @param {() => number} func
  92. * @param {any} self
  93. */
  94. function wrap_now(func, self, offset = 0) {
  95. let baseTime = 0;
  96. let contTime = baseTime;
  97.  
  98. /** @type {ProxyHandler<typeof func>} */
  99. const handler = {
  100. apply(target, self, args) {
  101. if (debug) log('apply(%o, %o, %o)', target, self, args);
  102. let time = apply(target, self, args);
  103. if (pristine || !isFinite(time)) return time;
  104. return ((time - baseTime) * scale) + contTime;
  105. }
  106. };
  107. setPrototypeOf(handler, null);
  108.  
  109. updaters[updaters.length] =
  110. function update() {
  111. contTime = timeJump == null ? handler.apply?.(func, self, []) : timeJump + offset;
  112. baseTime = apply(func, self, []);
  113. if (timeSync) contTime = baseTime;
  114. };
  115.  
  116. return new Proxy(func, handler);
  117. }
  118.  
  119. window.Performance.prototype.now = wrap_now(
  120. window.Performance.prototype.now,
  121. window.performance,
  122. window.performance.now() - window.Date.now()
  123. );
  124.  
  125. const DateConstructor = window.Date;
  126. /** @type {{ realTime: typeof Date.now, now: typeof Date.now, toString: typeof Date.prototype.toString, handler: ProxyHandler<DateConstructor> }} */
  127. const date = {
  128. realTime: window.Date.now,
  129. now: wrap_now(window.Date.now, window.Date),
  130. toString: DateConstructor.prototype.toString,
  131. handler: {
  132. apply(target, self, args) {
  133. if (debug) log('apply(%o, %o, %o)', target, self, args);
  134. if (!pristine) {
  135. args.length = 1;
  136. args[0] = apply(date.now, DateConstructor, []);
  137. } else return DateConstructor();
  138. return apply(date.toString, construct(DateConstructor, args), []);
  139. },
  140. construct(target, args, newTarget) {
  141. if (debug) log('construct(%o, %o, %o)', target, args, newTarget);
  142. if (!pristine && args.length < 1) {
  143. args[0] = apply(date.now, DateConstructor, []);
  144. }
  145. return construct(DateConstructor, args, newTarget);
  146. }
  147. }
  148. };
  149. setPrototypeOf(date, null);
  150. setPrototypeOf(date.handler, null);
  151. DateConstructor.now = date.now;
  152.  
  153. window.Date = new Proxy(DateConstructor, date.handler);
  154. window.Date.prototype.constructor = window.Date;
  155.  
  156. function noop() { }
  157.  
  158. /**
  159. * @param {(handler: TimerHandler, timeout?: number | undefined, ...args: any[]) => number} func
  160. */
  161. function wrap_timer(func) {
  162. /** @type {ProxyHandler<typeof func>} */
  163. const handler = {
  164. apply(target, self, args) {
  165. if (debug) log('apply(%o, %o, %o)', target, self, args);
  166. if (!pristine && args.length > 1) {
  167. args[1] = +args[1];
  168. if (args[1] && scale === 0)
  169. args[0] = noop;
  170. else if (args[1] && isFinite(args[1]))
  171. args[1] /= scale;
  172. }
  173. return apply(target, self, args);
  174. }
  175. };
  176. setPrototypeOf(handler, null);
  177. return new Proxy(func, handler);
  178. }
  179.  
  180. window.setTimeout = wrap_timer(window.setTimeout);
  181. window.setInterval = wrap_timer(window.setInterval);
  182. })(
  183. /** @type {Window & typeof globalThis} */
  184. (typeof unsafeWindow === 'object' ? unsafeWindow : window)
  185. );