Items Page Flower and Plushie Filter and Sort (PDA Optimized)

Grey out irrelevant flowers, move them to the bottom of the list, reorder flowers by quantity, and sort plushies by quantity on Torn's items page.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Items Page Flower and Plushie Filter and Sort (PDA Optimized)
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Grey out irrelevant flowers, move them to the bottom of the list, reorder flowers by quantity, and sort plushies by quantity on Torn's items page.
// @match        https://www.torn.com/item.php*
// @grant        none
// @license MIT
// ==/UserScript==
 
(function () {
    'use strict';
 
    // List of irrelevant flowers
    const irrelevantFlowers = [
        "Bunch of Carnations",
        "Bunch of Flowers",
        "Daffodil",
        "Bunch of Black Roses",
        "Dozen Roses",
        "Funeral Wreath",
        "Single Red Rose",
    ];
 
    function processItems() {
        // Handle Flowers
        const flowerItems = Array.from(document.querySelectorAll('#flowers-items > li'));
        if (flowerItems.length) {
            const relevantFlowers = [];
            const irrelevantFlowersList = [];
 
            flowerItems.forEach((item) => {
                const itemName = item.querySelector('.name-wrap .name')?.textContent.trim();
                const qtyElement = item.querySelector('.item-amount.qty');
                const qty = parseInt(qtyElement?.textContent.replace(/[^0-9]/g, '') || 0, 10);
 
                if (!itemName) return;
 
                if (irrelevantFlowers.includes(itemName)) {
                    // Grey out irrelevant flowers
                    item.style.opacity = "0.5";
                    item.style.backgroundColor = "#f9f9f9";
                    irrelevantFlowersList.push(item);
                } else {
                    relevantFlowers.push({ element: item, quantity: qty });
                }
            });
 
            // Sort relevant flowers by quantity (ascending)
            relevantFlowers.sort((a, b) => a.quantity - b.quantity);
 
            // Append items back to the DOM
            const flowerList = document.querySelector('#flowers-items');
            if (flowerList) {
                relevantFlowers.forEach(({ element }) => flowerList.appendChild(element));
                irrelevantFlowersList.forEach((item) => flowerList.appendChild(item));
            }
        }
 
        // Handle Plushies
        const plushieItems = Array.from(document.querySelectorAll('#plushies-items > li'));
        if (plushieItems.length) {
            const plushies = plushieItems.map((item) => {
                const qtyElement = item.querySelector('.item-amount.qty');
                const qty = parseInt(qtyElement?.textContent.replace(/[^0-9]/g, '') || 0, 10);
                return { element: item, quantity: qty };
            });
 
            // Sort plushies by quantity (ascending)
            plushies.sort((a, b) => a.quantity - b.quantity);
 
            // Append sorted plushies back to the DOM
            const plushieList = document.querySelector('#plushies-items');
            if (plushieList) {
                plushies.forEach(({ element }) => plushieList.appendChild(element));
            }
        }
    }
 
    // Throttled observer callback for DOM changes
    const throttle = (callback, delay) => {
        let lastCall = 0;
        return (...args) => {
            const now = Date.now();
            if (now - lastCall >= delay) {
                lastCall = now;
                callback(...args);
            }
        };
    };
 
    // Set up a MutationObserver for dynamic content updates
    const observer = new MutationObserver(
        throttle(() => {
            processItems();
        }, 500)
    );
 
    // Observe the list containers for changes
    const flowerTargetNode = document.querySelector('#flowers-items');
    const plushieTargetNode = document.querySelector('#plushies-items');
 
    if (flowerTargetNode) {
        observer.observe(flowerTargetNode, { childList: true, subtree: true });
    }
    if (plushieTargetNode) {
        observer.observe(plushieTargetNode, { childList: true, subtree: true });
    }
 
    // Initial run
    processItems();
})();