TankTrouble Development Library

Shared library for TankTrouble userscript development

目前为 2023-12-26 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/482092/1301905/TankTrouble%20Development%20Library.js

  1. // ==UserScript==
  2. // @name TankTrouble Development Library
  3. // @author commander
  4. // @namespace https://github.com/asger-finding
  5. // @version 0.0.4
  6. // @license GPL-3.0
  7. // @description Shared library for TankTrouble userscript development
  8. // @match *://*.tanktrouble.com/*
  9. // @grant none
  10. // @run-at document-start
  11. // @noframes
  12. // ==/UserScript==
  13.  
  14. /**
  15. * Apply a hook to the Content.init function which resolves when the promise ends
  16. * @returns Promise when Content.init has finished
  17. */
  18. const hookInit = () => new Promise(resolve => {
  19. const { init } = Content;
  20. Reflect.defineProperty(Content, 'init', {
  21. /**
  22. * Resolve after Content.init call finishes
  23. * @param args Arguments to pass
  24. * @returns Function call return value
  25. */
  26. value: (...args) => {
  27. const result = init(...args);
  28.  
  29. resolve();
  30. return result;
  31. }
  32. });
  33. });
  34.  
  35. /**
  36. * Fires when the document is readyState `interactive` or `complete`
  37. * @returns Promise that resolves upon content loaded
  38. */
  39. const whenContentLoaded = () => new Promise(promiseResolve => {
  40. if (document.readyState === 'interactive' || document.readyState === 'complete') promiseResolve();
  41. else document.addEventListener('DOMContentLoaded', () => promiseResolve());
  42. });
  43.  
  44. /**
  45. * Fires when the `main()` function is done on TankTrouble.
  46. * @returns Promise that resolves when Content.init() finishes
  47. */
  48. const whenContentInitialized = () => {
  49. if (GM.info.script.runAt !== 'document-start') return hookInit();
  50. return whenContentLoaded().then(() => hookInit);
  51. };