Skip to content

Instantly share code, notes, and snippets.

@rooftopchicken
Last active January 21, 2021 02:11
Show Gist options
  • Select an option

  • Save rooftopchicken/a6f39d27c1e575713d3b8524e8fa6a1e to your computer and use it in GitHub Desktop.

Select an option

Save rooftopchicken/a6f39d27c1e575713d3b8524e8fa6a1e to your computer and use it in GitHub Desktop.
much needed QOL upgrades to trades
// ==UserScript==
// @name FV - QOL trades
// @version 0.3
// @description QOL to trades
// @author msjanny (#7302)
// @match https://www.furvilla.com/trade/new*
// @include /https:\/\/www.furvilla.com\/trade\/[0-9]+$/
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
/* globals $:false */
GM_addStyle(".modal-body > .text-center:first-child .input {\
float: left;\
width: 46%;\
margin: 0 2% 20px;\
}\
.items-div td:nth-child(2) .counter {\
display: none;\
}\
");
function loadScripts(counter) {
//check every 300ms if pagination has loaded before continuing
// (up to 10 times)
if (counter === undefined) {
loadScripts(0);
}
else if(! $(".pagination").length && counter < 10) {
setTimeout ( function() {
loadScripts(counter + 1);
}, 300);
}
else if (counter < 10) {
$(".pagination a").click(function() {
loadScripts(0);
});
addPageNav();
replaceSOD();
}
}
function addPageNav() {
//retrieve max # pages & current page #
var max = $(".pagination li:nth-last-child(2) > *").eq(0).text();
var curr = $(".pagination .active").eq(0).text();
//add dropdown
var dropdown = $('<select class="input"></select>');
for (var i = 1; i <= max; i++ ) {
if (i == curr) {
dropdown.append($(`<option value="${i}" selected>${i}</option>`));
}
else
dropdown.append($(`<option value="${i}">${i}</option>`));
}
dropdown.change(function() {
loadInventory("https://www.furvilla.com/trades/inventory?page=" + $(this).val());
loadScripts(0);
});
//add input
var input = $(`<input type="number" min="1" max="${max}" class="input" placeholder="Enter a page between 1-${max}.">`);
input.keypress(function(e ) {
var page = parseInt($(this).val(), 10);
if (isNaN(page) || page > max || page < 1) {
$(this).val('');
alert("Invalid number!");
}
else if (e.which === 13) {
loadInventory("https://www.furvilla.com/trades/inventory?page=" + $(this).val());
loadScripts(0);
}
});
$(".modal-body > .text-center:first-child").prepend(dropdown);
$(".modal-body > .text-center:first-child").prepend(input);
}
function replaceSOD() {
$(".inventory-table tr").each(function(i, v) {
//var stackID = $(this).attr("data-id");
var maxStack = $(this).find(".sod_option:last-child").text();
$(this).find("td:nth-child(2)").append($(`<span class="counter">${maxStack}</span>`));
//$(this).find(".sod_option:not(:first-child)").remove();
$(this).find("td[align=center]").hide();
var input = $(`<input type="number" min="0" max="${maxStack}" class="input" value="0">`);
input.change(function() {
var quantity = parseInt($(this).val(), 10);
if (isNaN(quantity) || quantity > maxStack || quantity < 0) {
$(this).val('0');
alert("Invalid number!");
}
else {
var tr = $(this).parent().parent();
//change data-value and value
tr.find(".sod_option").eq(0).attr("data-value", quantity);
tr.find("select.input option").eq(0).attr("value", quantity);
//select the altered option
tr.find(".sod_label").click();
setTimeout(function() {
tr.find(".sod_option").eq(0).click();
}, 300);
}
});
$(this).append($("<td>").append(input));
});
}
$(document).ready(function(){
$(".items-btn").click(function() {
loadScripts(0);
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment