Neopets: Inventory Rarity Display

Displays rarity on inventory items

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Neopets: Inventory Rarity Display
// @namespace    Nyu@Clraik
// @version      1.0.1
// @description  Displays rarity on inventory items
// @author       Nyu
// @match        *://*.neopets.com/inventory.phtml
// @icon         https://www.google.com/s2/favicons?sz=64&domain=neopets.com
// ==/UserScript==


(async () => {
    await addRarities()

    const toggleButton = document.querySelector('.nptoggle')
    toggleButton.addEventListener('click', async () => {
        await addRarities()
    })

    async function addRarities(){
        await waitForInventoryLoaded()
        const items = Array.from(document.querySelectorAll(`[class*="item-img"]`))

        for (const item of items) {
            const rarity = item.dataset.rarity
            const div = document.createElement('div')
            div.style.position = 'absolute'
            div.style.top = '0px'
            div.style.width = 'fit-content'
            div.style.color = 'white'
            div.style.fontFamily = 'MuseoSansRounded500'
            div.style.backgroundColor = getBackgroundColor(rarity)
            div.classList = 'inv-menulinks'
            div.innerText = 'r'+rarity
            item.append(div)

        }
    }

    async function waitForInventoryLoaded() {
        await sleep(100)
        while (document.querySelector('.inv-loading-static')) {
            await sleep(1000)
        }
    }

    function getBackgroundColor(rarity) {
        return rarity == 99 ? 'green' : rarity == 500 ? 'gold' : rarity == 180 ? '#666666' : rarity < 75 ? '#dda713' : rarity >= 75 && rarity < 101 ? '#7ba515' : rarity >= 101 && rarity <= 104 ? '#aa4455' : rarity <= 105 && rarity < 111 ? 'orange' : rarity >= 111 ? 'red' : '#000'
    }

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }
})();