TornBuddy

Display your favorite user as a button in player input selector.

当前为 2024-06-05 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         TornBuddy
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Display your favorite user as a 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==

(async function() {
    'use strict';

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

    // Get localStorage stored value for time
    function getLocalStorage() {
        let user = localStorage.getItem("upscript_tornbuddy");
        if (user === null)
            localStorage.setItem("upscript_tornbuddy", JSON.stringify({"btnName": "Upsilon", "userId": "Upsilon [3212478]"}))
        return JSON.parse(user);
    }

    // Change element in localStorage
    function changeLocalStorage(item) {
        localStorage.setItem("upscript_tornbuddy", JSON.stringify(item));
    }

    function modifyBtnValue() {
        let btnName = prompt('Enter what you want your button to be named', 'Value');
        let userId = prompt('Enter the user id', 'Username [UserId]')
        changeLocalStorage({"btnName": btnName, "userId": userId});
        alert('Refresh your page and you are good to go!');;
    }

    // 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 favorite.
    function changeInputSize(input, searchContainer) {
        let localStorage = getLocalStorage();
        if (searchContainer.firstChild.textContent.includes(localStorage.btnName))
            return;
        let searchList = searchContainer.firstChild;
        let searchChild;
        let wifeOption = document.createElement("li");

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

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

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

    // Create the modify value button
    async function createButtonModifyValue() {
        await sleep(250);
        waitForElm('.tutorial-switcher').then((elm) => {
            let btn = document.createElement("a");
            let tutorialBtn = document.getElementsByClassName("tutorial-switcher")[0];

            btn.textContent = "Modify Favorite Value";
            btn.classList.add("back-to", "t-clear", "h", "c-pointer", "m-icon", "line-h24", "right", "last");
            btn.style.height = "28px";
            btn.id = "mod-favorite";
            tutorialBtn.after(btn);
            btn.addEventListener('click', function () {
                modifyBtnValue();
            });
        });
    }

    // Listen to switch in url for message
    if (window.location.href.includes("https://www.torn.com/messages.php")) {
        getLocalStorage();
        if (window.location.href.includes("compose")) {
            createButtonModifyValue();
        }
        window.addEventListener('popstate', function(event) {
            if (window.location.href.includes("compose")) {
                createButtonModifyValue();
            }
            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);
    }
})();