Spouse Selector in User selection

Display Spouse button in player input selector.

目前為 2024-06-04 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Spouse Selector in User selection
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Display Spouse button in player input selector.
// @author       Upsilon [3212478]
// @match        https://www.torn.com/messages.php
// @match        https://www.torn.com/item.php
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Enter your spouse Profile like this:
    const spouseProfile = "NameOfSpouse [000000]"



    // Sleep
    const sleep = ms => new Promise(r => setTimeout(r, ms));

    // Listen until element is found
    function waitForElm(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector))
                return resolve(document.querySelector(selector));

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

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

    // Change input for player to create another space for spouse.
    function changeInputSize(input, searchContainer) {
        if (searchContainer.firstChild.textContent.includes("Spouse"))
            return;
        let searchList = searchContainer.firstChild;
        let searchChild;
        let wifeOption = document.createElement("li");

        wifeOption.classList.add("ac-spouse");
        input.style.width = "300.5px";
        wifeOption.textContent = "Spouse";
        searchList.prepend(wifeOption);
        searchChild = searchList.children;
        for (let child of searchChild)
            child.style.width = "20%";

        wifeOption.addEventListener("click", () => input.value = spouseProfile);
    }

    // Loop over all inputs
    async function checkInventory() {
        await sleep(200);
        let input = document.getElementsByClassName("user-id");
        let searchContainer = document.getElementsByClassName("autocomplete-wrap");
        for (let index = 0; index < input.length; index++)
            changeInputSize(input[index], searchContainer[index]);
    }

    // Check if new send options available
    function updateSendOptions() {
        waitForElm('.option-send').then((elm) => {
            let sendOptions = document.getElementsByClassName("option-send");
            for (let sendOption of sendOptions)
                sendOption.addEventListener("click", () => checkInventory());
        });
    }

    // Listen to switch in url for message
    if (window.location.href.includes("https://www.torn.com/messages.php")) {
        window.addEventListener('popstate', function (event) {
            waitForElm('#ac-search-0').then((elm) => {
                let input = document.getElementsByClassName("user-id")[0];
                let searchContainer = document.getElementsByClassName("autocomplete-wrap")[0];
                changeInputSize(input, searchContainer);
            });
        });
        waitForElm('#ac-search-0').then((elm) => {
            let input = document.getElementsByClassName("user-id")[0];
            let searchContainer = document.getElementsByClassName("autocomplete-wrap")[0];
            changeInputSize(input, searchContainer);
        });
    }

    // Update send options for inventory lazy loading
    if (window.location.href.includes("https://www.torn.com/item.php")) {
        updateSendOptions();
        setInterval(() => updateSendOptions(), 1000);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址