Adds item image to SW & SSW results
当前为
// ==UserScript==
// @name Neopets: Shop Wizard & Super Shop Wizard Results Image
// @namespace kmtxcxjx
// @version 1.0.0
// @description Adds item image to SW & SSW results
// @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.toLowerCase() !== '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 = '';
img.alt = itemId;
}
const imgSrc = await getImgSrc(itemId);
img.src = imgSrc;
}
async function handleNewSSW() {
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];
console.error(itemId);
}
async function handleOldSSW() {
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];
console.error(itemId);
}
// 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 });
const oldSSW = document.querySelector('div#ssw_tabs_pane');
if (oldSSW) new MutationObserver(handleOldSSW).observe(oldSSW, { childList: true, subtree: true });
})();