Skip to content

Instantly share code, notes, and snippets.

@juvian
Created September 2, 2021 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juvian/a3ff1a0c8074be710857c6e5f4525cc3 to your computer and use it in GitHub Desktop.
Save juvian/a3ff1a0c8074be710857c6e5f4525cc3 to your computer and use it in GitHub Desktop.
Neopets Create Trade Helper
// ==UserScript==
// @name Neopets Create Trade Helper
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Juvian
// @match ://www.neopets.com/island/tradingpost*
// @grant none
// ==/UserScript==
let f = document.querySelector('form[action="/island/process_tradingpost.phtml"]');
let wishlist = document.querySelector('textarea[name="wishlist"]');
let tpPrices;
for (let checkbox of f.querySelectorAll('input[type="checkbox"]')) {
checkbox.addEventListener('change', (e) => {
let checked = f.querySelectorAll('input[type="checkbox"]:checked');
if (checked.length > 10) {
Array.from(checked).slice(10).forEach(c => c.checked = false);
}
if (f && wishlist) updateWishlist();
})
}
if (f && wishlist) {
tpPrices = JSON.parse(localStorage.tpPrices || "{}");
let table = f.closest("table").parentNode.closest("table");
table.setAttribute('width', 'auto');
table.style.textAlign ='center';
let div = table.parentNode;
div.style.marginLeft = 'auto';
div.style.marginRight = 'auto';
f.addEventListener('submit', savePrices);
for (let row of f.querySelectorAll('tbody tr')) {
let input = document.createElement('input');
let td = document.createElement('td');
td.setAttribute('bgcolor', '#ffffff');
input.value = getPrice(getId(row));
td.appendChild(input);
input.addEventListener('blur', updateWishlist);
row.prepend(td);
}
}
function getId(row) {
let img = row.querySelector('img');
let name = img.nextSibling.nodeValue;
let url = img.getAttribute('src').split('/').pop();
return name + '-' + url;
}
function setPrice(id, price) {
tpPrices[id] = price;
}
function savePrices() {
localStorage.tpPrices = JSON.stringify(tpPrices);
}
function getPrice(id) {
return tpPrices[id] || '';
}
function updateWishlist() {
let prices = [];
let checked = f.querySelectorAll('input[type="checkbox"]:checked');
let hasInfo = false;
for (let check of checked) {
let row = check.closest('tr');
let input = row.querySelector('input');
prices.push(input.value || (checked.length == 1 ? '' : "offer"));
setPrice(getId(row), input.value);
hasInfo = hasInfo || input.value;
}
if(hasInfo) wishlist.value = prices.join(' | ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment