Status Icon Attack Link

Adds a quick attack link on the activity icon on the item market

当前为 2025-02-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         Status Icon Attack Link
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a quick attack link on the activity icon on the item market
// @author       ThatJimmyGuy [2924303]
// @license      MIT
// @run-at       document-end
// @match        https://www.torn.com/page.php?sid=ItemMarket*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant        unsafeWindow
// ==/UserScript==

(function() {
    'use strict';

    const targetWindow = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;


    function addAnchorToRow(node) {
        const attackLinkBase = "https://www.torn.com/loader.php?sid=attack&user2ID="

        let playerID = node.querySelector(".honorWrap___BHau4 a").getAttribute("href").split("=")[2];
        let userStatusElement = node.querySelector(".userStatusWrap___ljSJG");

        let newAttackAnchorElement = document.createElement("a");
        newAttackAnchorElement.setAttribute("href", attackLinkBase + playerID);
        newAttackAnchorElement.setAttribute("target", "_blank");

        node.insertBefore(newAttackAnchorElement, userStatusElement);
        newAttackAnchorElement.appendChild(userStatusElement);
    }

    function checkAddedElements(mutationList, observer)
    {
        for (const mutation of mutationList) {
            if (mutation.type === "childList" && mutation.addedNodes.length) {
                Array.from(mutation.addedNodes).forEach(node => {
                    try
                    {
                        if (node.className.includes("rowWrapper___me3Ox"))
                        {
                            addAnchorToRow(node.querySelector(".userInfoBox___LRjPl"));
                        }
                        if (node.className.includes("sellerList___kgAh_"))
                        {
                            node.childNodes.forEach(listItem => {
                                if (listItem.className.includes("rowWrapper___me3Ox"))
                                {
                                    addAnchorToRow(listItem.querySelector(".userInfoBox___LRjPl"));
                                }
                            })
                        }
                    } catch(error) {
                        console.error("Error: ", error);
                    }
                })
            }
        }
    }

    let observer = new MutationObserver(checkAddedElements);
    observer.observe(document.querySelector(".item-market"), {subtree: true, childList: true});
})();