Execute HP

Add Execute Under X HP to Secondary Weapon - Changes colour when ready

目前為 2025-01-31 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Execute HP
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Add Execute Under X HP to Secondary Weapon - Changes colour when ready
// @author       Stig [2648238]
// @match        https://www.torn.com/loader.php?*
// @grant        none
// ==/UserScript==

const executePercent = 29; // Change this to your Execute %

(function() {
    'use strict';

    function getPercentage(value) {
        var raw = value * (executePercent / 100);
        return Math.ceil(raw);
    }

    function waitForElement(selector, callback) {
        let el = document.querySelector(selector);
        if (el) {
            callback(el);
        } else {
            setTimeout(function() {
                waitForElement(selector, callback);
            }, 500); // checks every 500ms
        }
    }

    function getHealth() {
        var healthValueElements = document.querySelectorAll('[id^="player-health-value_"]');

        // Ensure we have at least two elements, and then use the second one
        if (healthValueElements.length >= 2) {
            var healthText = healthValueElements[1].textContent.trim();
            var currentHealth = healthText.split("/")[0].replace(",", "").trim(); // Extract current health value
            var maxHealth = healthText.split("/")[1].replace(",", "").trim(); // Extract maximum health value

            var current = parseFloat(currentHealth);
            var max = parseFloat(maxHealth);

            if (!isNaN(current) && !isNaN(max)) {
                return { current, max };
            }
        }
        return { current: 0, max: 0 }; // Default return if no valid health value found
    }

    function render() {
        var weaponSecond = document.getElementById('weapon_second');
        if (weaponSecond) {
            var targetDiv = weaponSecond.querySelector('.bottom___XSBgG');
            if (targetDiv) {
                var newDiv = document.createElement('div');
                newDiv.className = 'custom-execute-under';

                var { current, max } = getHealth();
                var underHP = getPercentage(max);
                newDiv.textContent = `Execute Under: ${underHP} HP`;

                // Add CSS
                var css = `
                    .custom-execute-under {
                        position: absolute;
                        top: 70px;
                        left: 24px;
                        font-size: 10px;
                        color: red; /* Default colour */
                        font-weight: normal; /* Default weight */
                    }
                `;
                var style = document.createElement('style');
                document.head.appendChild(style);
                style.appendChild(document.createTextNode(css));

                targetDiv.parentNode.insertBefore(newDiv, targetDiv.nextSibling);

                // Update colour dynamically based on health
                setInterval(function() {
                    var health = getHealth();
                    if (health.current <= underHP && health.current > 0) {
                        newDiv.textContent = 'Execute Now!';
                        newDiv.style.color = '#00FF00'; // Bright green
                        newDiv.style.fontWeight = 'bold';
                        newDiv.style.left = '45px';
                    } else {
                        newDiv.style.color = 'red';
                        newDiv.style.fontWeight = 'normal';
                    }
                }, 100); // Check every 500ms
            }
        }
    }

    waitForElement('.entry___m0IK_', function() {
        render();
    });
})();