Neopets: Inventory Pet Preference

Sets your preferred pet as the first option when available

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Neopets: Inventory Pet Preference
// @namespace    Nyu@Clraik
// @version      1.0.0
// @description  Sets your preferred pet as the first option when available
// @author       Nyu
// @match        *://*.neopets.com/inventory.phtml
// @icon         https://www.google.com/s2/favicons?sz=64&domain=neopets.com
// @license      MIT
// ==/UserScript==

(async () => {
    const preferredPet = 'PETNAME' // YOUR PREFERED PET NAME HERE


    while (document.getElementById('invDesc').style.display === 'none') {
        await wait(1000)
    }
    await waitForLoadingToComplete()

    const items = Array.from(document.querySelectorAll('.grid-item'))
    for (const item of items) {
        item.addEventListener('click', async () => {
            await waitForLoadingToComplete()
            setPreferredPet()
        })
    }

    function setPreferredPet() {
        const select = document.querySelector('select[name="action"]')
        let petOption;
        for (let i = 0; i < select.options.length; i++) {
            if (select.options[i].value.includes(preferredPet)) {
                petOption = select.options[i]
                break
            }
        }

        if (petOption) {
            select.removeChild(petOption)
            select.insertBefore(petOption, select.options[1])
        }
    }

    async function waitForLoadingToComplete() {
        let isLoading = document.querySelector('.inv-loading-static')
        while (isLoading) {
            await wait(1000)
            isLoading = document.querySelector('.inv-loading-static')
        }
    }


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