-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNeopets - Inventory+.user.js
178 lines (156 loc) · 7.24 KB
/
Neopets - Inventory+.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// ==UserScript==
// @name Neopets - Inventory Plus <MettyNeo>
// @version 1.1
// @description Improves the Inventory page by allowing multiple actions to be performed without a page refresh and saves selected options for items.
// @author Metamagic
// @match *://*.neopets.com/inventory.phtml*
// @match *://neopets.com/inventory.phtml*
// @icon https://i.imgur.com/RnuqLRm.png
// @downloadURL https://github.com/Mettymagic/np-userscripts/raw/main/Neopets%20-%20Inventory%2B.user.js
// @updateURL https://github.com/Mettymagic/np-userscripts/raw/main/Neopets%20-%20Inventory%2B.user.js
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
// Trans rights are human rights ^^
// metty says hi
//================
// script settings
//================
(function($) {
//lets you bypass the refresh by clicking outside the box
const DISABLE_REFRESH = true
//updates inventory contents when actions are performed
const UPDATE_ON_ACTION = true
//remembers the last selected option for each item
const REMEMBER_SELECTION = true
//defaults to unstacked view. for fans of the old neopets layout. ^^
const UNSTACK_VIEW_DEFAULT = false
//============
// main script
//============
$("#container__2020 > div.page-title__2020 > div.inv-title-container > h1")[0].innerHTML += "+"
if(DISABLE_REFRESH) $("#refreshshade__2020")[0].remove()
if(UNSTACK_VIEW_DEFAULT) setStackView("unstack")
//watches for item description popup
const descObs = new MutationObserver((mutations) => {
if(REMEMBER_SELECTION) {
if($("#iteminfo_select_action").find("option:selected").val() == "Choose an Action") setRememberedSelection()
rememberSelections()
}
})
descObs.observe(document.getElementById("invDesc"), {childList: true, subtree: true})
//NOTE: script may not be compatible with certain selection-based capsules (eg essentials) - i don't have the nc to test this unfort
//watches for item results popup
let prevDisplay = 'none'
const resultObs = new MutationObserver((mutations) => {
//if not loading
if($("#invResult div.inv-loading-static").length == 0) {
//special case for gift box menu
if($("#invResult div.giftgram-img").length > 0 && $("#invResult").css('display') == 'block') {
//gift sent, refresh page
console.log($("#invResult > div.popup-body__2020 > p")?.[0])
if($("#invResult > div.popup-body__2020 > p")?.[0]?.innerHTML?.includes("Your gift has been delivered to")) {
if(UPDATE_ON_ACTION) refreshInventory()
if(DISABLE_REFRESH) disableRefresh()
}
else if(DISABLE_REFRESH) disableRefresh(false)
prevDisplay = 'block'
}
//now showing results
else if($("#invResult").css('display') == 'block' && prevDisplay == 'none') {
if(UPDATE_ON_ACTION) refreshInventory()
if(DISABLE_REFRESH) disableRefresh()
prevDisplay = 'block'
}
//no longer showing results
else if($("#invResult").css('display') == 'none' && prevDisplay == 'block') {
prevDisplay = 'none'
}
}
//adds unclickable shade if loading
else {
document.getElementById("invpopupshade__2020").setAttribute("style", "display: block;")
}
})
resultObs.observe(document.getElementById("invResult"), {attributes:true})
let reload = false
reloadImages()
//================
// functionalities
//================
function disableRefresh(addButton = true) {
document.getElementById("invpopupshade__2020").setAttribute("style", "display: none;")
document.getElementById("navpopupshade__2020").setAttribute("style", "display: block;")
//updates the button to not refresh
$("#invResult > div.popup-header__2020 > a")[0].removeAttribute("href")
$("#invResult > div.popup-header__2020 > a > div")[0].setAttribute("onclick", "togglePopup__2020()")
//adds refresh button
if(addButton) {
let a = getRefreshButton()
$("#invResult > div.popup-body__2020")[0].appendChild(a)
}
}
function getRefreshButton() {
let a = document.createElement("a")
a.href = "/inventory.phtml"
let button = document.createElement("div")
button.classList.add("button-default__2020", "button-yellow__2020", "btn-single__2020")
button.innerHTML = "Refresh Page"
a.appendChild(button)
return a
}
function rememberSelections() {
$("#iteminfo_select_action > select").change(() => {
let option = $("#iteminfo_select_action").find("option:selected").val()
let itemname = document.getElementById("invItemName").innerHTML
let list = GM_getValue("invselect", {})
list[itemname] = option
GM_setValue("invselect", list)
console.log(`[Inv+] Selection remembered for item '${itemname}'`)
})
}
function setRememberedSelection() {
let itemname = document.getElementById("invItemName").innerHTML
let memory = GM_getValue("invselect", {})[itemname]
if(memory != undefined) {
$("#iteminfo_select_action > select")[0].value = memory
$("#iteminfo_select_action > div.invitem-submit")[0].classList.remove("disabledButton")
console.log(`[Inv+] Selection set for item '${itemname}'`)
}
}
//you have to do this because neopets code put a lowercase instead of an uppercase in their code. incredible.
function reloadImages() {
let invObs = new MutationObserver((mutations) => {
if(reload && $(`#invDisplay > div.inv-display:not([style="display: none;"])`).length && $('.lazy').length) {
reload = false
$('.lazy').Lazy({
threshold: 50,
visibleOnly: true,
})
console.log("[Inv+] Item images force-loaded.")
}
})
invObs.observe($("#invDisplay")[0], {childList:true, subtree:true})
}
function refreshInventory() {
// Temporary Loading Image
$('#invDisplay' + 1).html("<br><br><br><div class='inv-loading-static'></div><p>Loading...</p>");
$.get("https://www.neopets.com/inventory.phtml", ()=>{
console.log("[Inv+] Refreshed inventory contents.")
$("#tabs > div.inv-tabs-container > ul.invTabs > li.inv-tab-label.invTab-selected")[0].click() //refreshes the inventory tab
reload = true
})
}
function setStackView(type) {
//if it's stacked and we want unstack
if(type == "unstack" && $("#invStack > div.stack-icon-container.invfilter-active").length > 0) {
document.getElementById("invStack").click()
}
//if it's unstacked and we want stacked
else if(type == "stack" && $("#invStack > div.unstack-icon-container.invfilter-active").length > 0) {
document.getElementById("invStack").click()
}
//simply toggles it
else document.getElementById("invStack").click()
}
})(jQuery)