Elethor General Purpose

Provides some general additions to Elethor

当前为 2021-01-08 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Elethor General Purpose
// @description  Provides some general additions to Elethor
// @namespace    https://www.elethor.com/
// @version      1.0.3
// @author       Anders Morgan Larsen (Xortrox)
// @match        https://elethor.com/*
// @match        https://www.elethor.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    const moduleName = 'Elethor General Purpose';


    const playerHealthBarElementDefinition = '.progress.is-medium.is-danger';
    const playerHealthBarTextElementDefinition = '.is-fight-health';

    function getBattleHealthPercentage() {
        const playerHealth = document.querySelector(playerHealthBarElementDefinition);

        if (!playerHealth) {
            return 0;
        }

        return playerHealth.value / playerHealth.max * 100;
    }

    function initializeUpdateBattleHealthPercentage() {
        if (window.elethorExtrasInterval) {
            clearInterval(window.elethorExtrasInterval);
        }

        window.elethorExtrasInterval = setInterval(() => {
            const playerHealthText = document.querySelector(playerHealthBarTextElementDefinition);
            const healthPercentage = getBattleHealthPercentage();

            if (playerHealthText && healthPercentage && !isNaN(healthPercentage)) {
                let percentageDisplay;
                if (playerHealthText.children.length === 0) {
                    percentageDisplay = document.createElement('span');

                    playerHealthText.appendChild(percentageDisplay);
                } else {
                    percentageDisplay = playerHealthText.children[0];
                    
                }
                
                if (percentageDisplay) {
                    percentageDisplay.innerText = ` (${healthPercentage.toFixed(3)}%)`;
                }
            }
        }, 500);

        
        console.log(`[${moduleName}] Battle Percentage initialized.`);
    }

    function initializeToastKiller() {
        document.addEventListener('click', function(e) {
            if (e.target && e.target.className && e.target.className.includes('toasted')) {
                e.target.remove();
            }
        });
        
        console.log(`[${moduleName}] Toast Killer initialized.`);
    }

    function initializeXHRHook() {
        let rawOpen = XMLHttpRequest.prototype.open;

        XMLHttpRequest.prototype.open = function() {
            if (!this._hooked) {
                this._hooked = true;
                setupHook(this);
            }
            rawOpen.apply(this, arguments);
        }

        function setupHook(xhr) {
            if (window.elethorGeneralPurposeOnUserLoad) {
                const e = new Event('EGPXHR');
                e.xhr = xhr;

                window.elethorGeneralPurposeOnUserLoad.dispatchEvent(e);
            }
        }
        window.elethorGeneralPurposeOnUserLoad = new EventTarget();

        elethorGeneralPurposeOnUserLoad.addEventListener('EGPXHR', function (e) {
            console.log('xhr event:', e);
        });
        console.log(`[${moduleName}] XHR Hook initialized.`);
    }

    (function run() {
        initializeUpdateBattleHealthPercentage();
        initializeToastKiller();
        initializeXHRHook();
        
        console.log(`[${moduleName}] Loaded.`);
    })();
})();