Neopets: Inventory Pet Preference

Sets your preferred pet as the first option when available

  1. // ==UserScript==
  2. // @name Neopets: Inventory Pet Preference
  3. // @namespace Nyu@Clraik
  4. // @version 1.0.0
  5. // @description Sets your preferred pet as the first option when available
  6. // @author Nyu
  7. // @match *://*.neopets.com/inventory.phtml
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=neopets.com
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (async () => {
  13. const preferredPet = 'PETNAME' // YOUR PREFERED PET NAME HERE
  14.  
  15.  
  16. while (document.getElementById('invDesc').style.display === 'none') {
  17. await wait(1000)
  18. }
  19. await waitForLoadingToComplete()
  20.  
  21. const items = Array.from(document.querySelectorAll('.grid-item'))
  22. for (const item of items) {
  23. item.addEventListener('click', async () => {
  24. await waitForLoadingToComplete()
  25. setPreferredPet()
  26. })
  27. }
  28.  
  29. function setPreferredPet() {
  30. const select = document.querySelector('select[name="action"]')
  31. let petOption;
  32. for (let i = 0; i < select.options.length; i++) {
  33. if (select.options[i].value.includes(preferredPet)) {
  34. petOption = select.options[i]
  35. break
  36. }
  37. }
  38.  
  39. if (petOption) {
  40. select.removeChild(petOption)
  41. select.insertBefore(petOption, select.options[1])
  42. }
  43. }
  44.  
  45. async function waitForLoadingToComplete() {
  46. let isLoading = document.querySelector('.inv-loading-static')
  47. while (isLoading) {
  48. await wait(1000)
  49. isLoading = document.querySelector('.inv-loading-static')
  50. }
  51. }
  52.  
  53.  
  54. function wait(ms) {
  55. return new Promise(resolve => setTimeout(resolve, ms));
  56. }
  57. })()