Code:
// ==UserScript==
// @author Backslash
// @(you need an account to see links)cription Hides items that are not in the restock list
// @include (you need an account to see links)
// @(you need an account to see links) Neopets : Shops : Smart Block
// @(you need an account to see links)space (you need an account to see links)
// @version 1.1
// ==/UserScript==
/*
2018-11-14 Modified by Odd
- added version
- changed the "include" so that the script will only run in shops instead of on every page
- fixed issue with adding the listbox UI by targeting the specific cell that the author wanted it in instead of doing a page-wide find-and-replace
- fixed issue with "rsList" showing on initial load despite the "User the Smart Block Feature" checkbox not being checked
- replaced GM_getValue and GM_setValue with a quick and dirty localStorage getter and setter (also added a "smartBlock" prefix to each variable in order to avoid ambiguity issues when using shared storage)
*/
function getValue(name, defaultValue) {
var value = localStorage.getItem(name);
if (value != null) return value;
return defaultValue;
}
function setValue(name, value) {
if (value == null || value === undefined) localStorage.removeItem(name);
else localStorage.setItem(name, value);
}
var layout = '\
<div class="contentModule" style="height: 100%"><table cellpadding="3" cellspacing="0" border="0" class="contentModuleTable">\
<tr>\
<td class="contentModuleHeader"> Smart Block</td>\
</tr>\
<tr>\
<td align="left" valign="top" class="contentModuleContent">\
<div align="center">\
<input type="checkbox" name="team" id="team" value="team" '+ getValue('smartBlock.team', '') + '> <label for="team">Use the Smart Block Feature</label><br>\
<div id="rsList" style="' + ((getValue('smartBlock.team') == "checked") ? "" : "display: none;") + '">Enter a list of items that you don\'t want the script to block.<br><textarea id="listBox" style="width:80%;height:125px;">'+ getValue('smartBlock.rsList', 'No Items Listed') + '</textarea><br><Br></div><button id="saveButton">Save Settings</button>\
</div><br />\
</td>\
</tr></table></div>\
';
document.querySelector("td[width='304']").innerHTML += layout;
/*
2018-11-14 - Odd - no need to find-and-replace the entire body - target the cell instead
document.body.innerHTML = document.body.innerHTML.replace('<td align="left" valign="top" width="304">', '<td align="left" valign="top" width="304">' + layout);
*/
document.getElementById('team').addEventListener('click', sBlockClick, false);
function sBlockClick() {
if (document.getElementById('team').checked == true) {
document.getElementById('rsList').setAttribute("style", "");
}
else {
document.getElementById('rsList').setAttribute("style", "display:none;");
}
}
document.getElementById('saveButton').addEventListener('click', saveSettings, false);
function saveSettings() {
setValue('smartBlock.rsList', document.getElementById('listBox').value);
if (document.getElementById('team').checked == true) {
setValue('smartBlock.team', 'checked');
} else {
setValue('smartBlock.team');
}
window.location.reload();
}
function GetBetween(zStr, zStart, zEnd, zPos) {
var z1 = zStr.indexOf(zStart, (zPos === undefined ? 0 : zPos)); var z2 = zStr.indexOf(zEnd, z1);
return z2 > z1 && z1 > -1 ? zStr.substring(z1 + zStart.length, z2) : '';
}
function AdBlock(c) {
a = document.evaluate("//table[@align = 'center' and @cellpadding = '4']/tbody//td", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
b = a.snapshotLength;
h = new Array();
if (b > 0) {
hiddenItems = document.evaluate("//table[@align = 'center' and @cellpadding = '4']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0).innerHTML;
for (i = 0; i < b; i++) {
if (a.snapshotItem(i).getElementsByTagName('a')[0].getAttribute("onclick") != null) {
itemName = GetBetween(a.snapshotItem(i).getElementsByTagName('a')[0].getAttribute("onclick"), "you wish to purchase ", " at ");
for (j = 0; j < c.length; j++) {
if (c[j].toLowerCase() == itemName.toLowerCase()) {
h.push(a.snapshotItem(i));
break;
}
}
a.snapshotItem(i).parentNode.removeChild(a.snapshotItem(i));
}
}
if (h.length > 0) {
for (i = 0; i < (h.length / 6) + 1; i++) {
f = document.evaluate("//table[@align = 'center' and @cellpadding = '4']/tbody/tr", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(i);
for (j = 0; j < 6 ; j++) {
if (typeof h[0] != 'undefined') {
f.appendChild(h[0]);
h.splice(0, 1);
}
}
}
}
document.evaluate("//td[@class = 'contentModuleHeader']/b", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0).innerHTML = "Shop Inventory currently contains <b><font color=#3BB9FF>" + b + "</font></b> items (click <u><div style='cursor:pointer; display:inline;' id='hiddenItems'>here</div></u> to view full shop stock)";
document.getElementById('hiddenItems').addEventListener('click', function () { document.evaluate("//table[@align = 'center' and @cellpadding = '4']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0).innerHTML = hiddenItems; }, false);
}
}
if (getValue('smartBlock.team') == "checked") {
var items = getValue('smartBlock.rsList').split('\n');
AdBlock(items);
}