Highlight Myself in Faction

Highlight myself in faction rows

目前为 2024-02-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         Highlight Myself in Faction
// @namespace    microbes.torn.highlight
// @version      0.1
// @description  Highlight myself in faction rows
// @author       You
// @match        https://www.torn.com/factions.php*
// @match        https://www.torn.com/war.php*
// @match        https://www.torn.com/preferences.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let username = $(`#sidebarroot a[href^='/profiles.php?XID=']`).first().text();
    let backgroundColor = localStorage.getItem(`highlight_bg`) || '#00ff00';
    let textColor = localStorage.getItem(`highlight_text`) || '#ffffff';

    if (GetPageName() == "preferences.php") {
        waitForElementToExist('.preferences-container').then(() => {
            $('.preferences-container').after(`
                <div style="margin-top: 20px; background-color: grey; color: white;">
                    <h1>Highlight Myself</h1>

                    <input type="color" id="background-color" name="favcolor" value="${backgroundColor}"> Background Color
                    <input type="color" id="text-color" name="favcolor" value="${textColor}"> Text Color
                </div>`);

            $("#background-color").change(() => {
                localStorage.setItem(`highlight_bg`, $("#background-color").val());
            });

            $("#text-color").change(() => {
                localStorage.setItem(`highlight_text`, $("#text-color").val());
            });
        });
    }
    else {
        waitForElementToExist(`img[alt="${username}"]`).then(() => {
            startWaiting();
        });
    }

    function startWaiting() {
        let selector = null;

        if (!window.location.href.includes("chainreport")) {
            selector = $(`img[alt="${username}"]`).parent().parent().parent().parent().parent().parent();
        }
        else {
            selector = $(`img[alt="${username}"]`).parent().parent().parent().parent();
        }

        if (selector) {
            selector.css("background-color", backgroundColor);
            selector.css("color", textColor);
        }

        setTimeout(startWaiting, 1000);
    }
})();

function waitForElementToExist(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(() => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            subtree: true,
            childList: true,
        });
    });
}

function GetPageName() {
    var path = window.location.pathname;
    var page = path.split("/").pop();

    return page;
}