Neopets: Shop Wizard & Super Shop Wizard Results Image and SW link

Adds item image to SW & SSW results. If SSW cannot be used it also links to SW.

当前为 2025-11-04 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name             Neopets: Shop Wizard & Super Shop Wizard Results Image and SW link
// @namespace        kmtxcxjx
// @version          1.0.0
// @description      Adds item image to SW & SSW results. If SSW cannot be used it also links to SW.
// @match            *://www.neopets.com/*
// @grant            none
// @run-at           document-end
// @icon             https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png
// @license          MIT
// ==/UserScript==

(async function() {
    'use strict';

    async function getImgSrc(itemId) {
        const res = await fetch(`https://itemdb.com.br/api/v1/items/many?item_id[]=${itemId}`, {
            method: 'GET',
        });
        const data = await res.json();
        return data[itemId].image;
    }

    async function handleSW() {
        const a = document.querySelector('div.wizard-results-grid a');
        if (!a) return;
        const match = a.href.match(/buy_obj_info_id=(\d+)/);
        if (!match) return;
        const itemId = match[1];

        const h3 = document.querySelector('div.wizard-results-text h3');
        if (!h3) return;

        let img = h3.nextElementSibling;
        if (!img || img.tagName !== 'IMG') {
            img = document.createElement('img');
            img.src = 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png';
            img.alt = itemId;
            img.style.width = '50px';
            h3.insertAdjacentElement('afterend', img);
        } else if (img && img.alt !== itemId) {
            img.src = 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png';
            img.alt = itemId;
        }
        if (img.src === 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png') img.src = await getImgSrc(itemId);
    }

    async function handleNewSSW() {
        linkToSW();
        const tab1 = document.querySelector('div#ssw-tabs-1');
        const tab2 = document.querySelector('div#ssw-tabs-2');
        if (!tab1 || !tab2) return;

        if (getComputedStyle(tab1).display !== 'none') {
            const imgDiv = document.querySelector('div#imgResult');
            if (imgDiv) imgDiv.remove();
            const h3 = document.querySelector('div#ssw__2020 h3');
            if (h3) h3.style.paddingLeft = '0px';
            return;
        }

        const a = document.querySelector('div#sswresults a');
        if (!a) return;
        const match = a.href.match(/buy_obj_info_id=(\d+)/);
        if (!match) return;
        const itemId = match[1];

        const wizard = document.querySelector('div.ssw-char__2020');
        if (!wizard) return;

        let imgDiv = wizard.nextElementSibling;
        if (!imgDiv || imgDiv.id !== 'imgResult') {
            imgDiv = document.createElement('div');
            imgDiv.id = 'imgResult';
            imgDiv.style.position = 'absolute';
            imgDiv.style.left = '74px';
            imgDiv.style.top = '3px';
            imgDiv.style.width = '44px';
            imgDiv.style.height = '44px';
            imgDiv.style.boxSizing = 'border-box';
            imgDiv.style.background = '#d2d2d2';
            imgDiv.style.border = '1px solid #5e5e5e';
            imgDiv.style.borderRadius = '8px';
            imgDiv.style.display = 'flex';
            imgDiv.style.alignItems = 'center';
            imgDiv.style.justifyContent = 'center';

            const img = document.createElement('img');
            img.src = 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png';
            img.alt = itemId;
            img.style.width = '40px';
            img.style.border = '2px solid #282828';
            img.style.borderRadius = '8px';

            const h3 = document.querySelector('div#ssw__2020 h3');
            if (h3) h3.style.paddingLeft = '23px';


            imgDiv.appendChild(img);
            wizard.parentNode.insertBefore(imgDiv, wizard.nextSibling);
        }
        const img = imgDiv.firstElementChild;
        if (img.alt !== itemId) {
            img.src = 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png';
            img.alt = itemId;
        }

        if (img.src === 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png') img.src = await getImgSrc(itemId);
    }

    async function handleOldSSW() {
        linkToSW();
        const tab1 = document.querySelector('div#ssw-tabs-1');
        const tab2 = document.querySelector('div#ssw-tabs-2');
        if (!tab1 || !tab2) return;

        if (getComputedStyle(tab1).display !== 'none') {
            const imgDiv = document.querySelector('div#imgResult');
            if (imgDiv) imgDiv.remove();
            return;
        }

        const a = document.querySelector('table#results_table a');
        if (!a) return;

        const match = a.href.match(/buy_obj_info_id=(\d+)/);
        if (!match) return;
        const itemId = match[1];

        const shopkeeper = document.querySelector('div#shopkeeper');
        if (!shopkeeper) return;

        let imgDiv = shopkeeper.nextElementSibling;

        if (!imgDiv || imgDiv.id !== 'imgResult') {
            imgDiv = document.createElement('div');
            imgDiv.id = 'imgResult';
            imgDiv.style.position = 'absolute';
            imgDiv.style.left = '63px';
            imgDiv.style.top = '29px';
            imgDiv.style.width = '86px';
            imgDiv.style.height = '86px';
            imgDiv.style.boxSizing = 'border-box';
            imgDiv.style.background = '#d2d2d2';
            imgDiv.style.border = '1px solid #5e5e5e';
            imgDiv.style.borderRadius = '8px';
            imgDiv.style.display = 'flex';
            imgDiv.style.alignItems = 'center';
            imgDiv.style.justifyContent = 'center';

            const img = document.createElement('img');
            img.src = 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png';
            img.alt = itemId;
            img.style.width = '80px';
            img.style.border = '2px solid #282828';
            img.style.borderRadius = '8px';

            imgDiv.appendChild(img);
            shopkeeper.parentNode.insertBefore(imgDiv, shopkeeper.nextElementSibling);
        }

        const img = imgDiv.firstElementChild;
        if (img.alt !== itemId) {
            img.src = 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png';
            img.alt = itemId;
        }
        if (img.src === 'https://images.neopets.com/games/aaa/dailydare/2012/post/theme-icon.png') img.src = await getImgSrc(itemId);
    }

    function linkToSW() {
        let searchFor, parent, fontSize;
        if (document.querySelector('div#ssw_tabs_pane')) { // Old style
            const tab2 = document.querySelector('div#ssw-tabs-2');
            if (!tab2) return;
            if (getComputedStyle(tab2).display === 'none') return; // Results page not visible
            if (document.querySelector('table#results_table')) return; // Search was successful

            parent = document.querySelector('div#results > p.pmod');
            if (!parent) parent = document.querySelector('div#ssw_error_result');
            searchFor = document.querySelector('div#search_for');
            fontSize = '9px';
        } else if (document.querySelector('div#ssw__2020')) { // New style
            parent = document.querySelector('div#ssw_error_result');
            if (!parent) return;
            if (getComputedStyle(parent).display === 'none') return; // No error displayed

            searchFor = document.querySelector('div#search_for p');
            fontSize = '11pt';
        } else return; // No SSW present at all
        // Parent or search text doesn't exist, or we already inserted a link
        if (!parent || !searchFor || parent.querySelector('a.sw-link')) return;
        // Either the item doesn't exist or it's unbuyable - either way a link to SW is pointless
        if (parent.textContent.trim() === 'No items found.') return;

        const match = searchFor.textContent.match(/Searching for '(.*)', matching '/);
        if (!match) return;
        const query = match[1];

        const a = document.createElement('a');
        a.href = `https://www.neopets.com/shops/wizard.phtml?string=${encodeURIComponent(query)}`;
        a.textContent = 'here';
        a.className = 'sw-link';
        a.target = '_blank'; // Opens in new tab
        a.style.fontSize = fontSize;

        const sentence = document.createElement('span');
        sentence.style.fontSize = fontSize;
        sentence.appendChild(document.createTextNode('Or click '));
        sentence.appendChild(a);
        sentence.appendChild(document.createTextNode(' to search on the Shop Wizard instead.'));

        parent.appendChild(document.createElement('br'));
        parent.appendChild(document.createElement('br'));
        parent.appendChild(sentence);
    }

    // Add mutation observers for all three SW variants
    if (window.location.href.includes('://www.neopets.com/shops/wizard.phtml')) {
        const shopWizardFormResults = document.querySelector('div#shopWizardFormResults');
        if (shopWizardFormResults) new MutationObserver(handleSW).observe(shopWizardFormResults, { childList: true, subtree: true });
    }
    const newSSW = document.querySelector('div#ssw__2020');
    if (newSSW) {
        new MutationObserver(handleNewSSW).observe(newSSW, {
            childList: true,
            subtree: true,
            attributes: true,
            characterData: true
        });
    }
    const oldSSW = document.querySelector('div#ssw_tabs_pane');
    if (oldSSW) {
        new MutationObserver(handleOldSSW).observe(oldSSW, {
            childList: true,
            subtree: true,
            attributes: true,
            characterData: true
        });
    }
})();