Elethor General Purpose

Provides some general additions to Elethor

目前為 2021-01-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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.`);
    })();
})();