Elethor General Purpose

Provides some general additions to Elethor

目前为 2021-01-08 提交的版本。查看 最新版本

// ==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.`);
    })();
})();