Greasy Fork

TankTrouble Development Library

Shared library for TankTrouble userscript development

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

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

// ==UserScript==
// @name        TankTrouble Development Library
// @author      commander
// @namespace   https://github.com/asger-finding
// @version     0.0.2
// @license     GPL-3.0
// @description Shared library for TankTrouble userscript development
// @match       *://*.tanktrouble.com/*
// @grant       none
// @run-at      document-start
// @noframes
// ==/UserScript==

/* jshint esversion: 8 */

/**
 * Fires when the document is readyState `interactive` or `complete`
 * @returns Promise that resolves upon content loaded
 */
const whenContentLoaded = () => new Promise(promiseResolve => {
	if (document.readyState === 'interactive' || document.readyState === 'complete') promiseResolve();
	else document.addEventListener('DOMContentLoaded', () => promiseResolve());
});

/**
 * Fires when the `main()` function is done on TankTrouble.
 * @returns Promise that resolves when Content.init() finishes
 */
const whenContentInitialized = () => new Promise(promiseResolve => {
	contentLoaded().then(() => {
		const contentInitHook = Content.init;
		Reflect.defineProperty(Content, 'init', {
			/**
			 * Resolve after Content.init call finishes
			 *
			 * @param args Arguments to pass
			 */
			value: (...args) => {
				contentInitHook(...args);

				promiseResolve();
			}
		});
	});
});