TornBuddy

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

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

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

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

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

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

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