BackpackDecalSpotter/script.js

117 lines
4.0 KiB
JavaScript

var checkable_accounts = { '76561198311319887': 'Weapon Crusher' };
decaled_objects = { 'Conscientious Objector': '3.66', 'Clan Pride': '2.11', 'Flair!': '4.22', 'Photo Badge': '2.44' }
backpack_page_limit = 3;
function fetchAccounts() {
var deferred = new $.Deferred();
for (const [item, price] of Object.entries(decaled_objects)) {
handleItem(item, price);
}
deferred.resolve(checkable_accounts);
return deferred.promise()
}
function handleItem(item, price) {
for (let i = 1; i <= backpack_page_limit; i++) {
$.ajax({
dataType: 'html',
url: 'https://backpack.tf/classifieds',
data: 'page=' + i + '&item=' + encodeURIComponent(item) + '&quality=6&tradable=1&craftable=1&australium=-1&killstreak_tier=0&numeric=price&comparison=lt&value=' + price + '&low=' + price,
async: false,
tryCount: 0,
retryLimit: 3,
success: function (msg) {
if ($('.col-md-6:first .alert', msg).length > 0) {
return false;
}
$('.col-md-6:first .user-link', msg).each(function () {
checkable_accounts[$(this).attr('data-id')] = $(this).attr('data-name');
});
},
error: function (response, status, error) {
if (response.status == 429) {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
setTimeout(() => {
$.ajax(this);
}, 3000);
return;
}
}
}
});
}
}
function handleAccounts() {
for (const [steamid64, name] of Object.entries(checkable_accounts)) {
handleAccount(steamid64, name);
}
}
function handleAccount(steamid64, name) {
var url = "https://backpack.tf/_inventory/" + steamid64;
$.ajax({
dataType: 'json',
url: url,
async: false,
tryCount: 0,
retryLimit: 3,
success: function (msg) { handleHTML(steamid64, name, msg['html']); },
error: function (response, status, error) {
if (response.status == 429) {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
setTimeout(() => {
$.ajax(this);
}, 3000);
return;
}
}
}
});
}
function handleHTML(steamid64, name, html_string) {
html = $.parseHTML(html_string);
var decaled_items = $('div.decal', html);
if (decaled_items.length == 0) {
var empty_accounts = $('#empty-accounts');
empty_accounts.append('<a href="https://next.backpack.tf/profiles/' + steamid64 + '">' + name + '</a>; ');
empty_accounts.fadeIn();
return false;
}
var inventory = $('<div class="inventory"></div>');
inventory.append('<a href="https://next.backpack.tf/profiles/' + steamid64 + '"><h3>' + name + '</h3></a>');
decaled_items.each(function () {
var url = "https://next.backpack.tf/item/" + $(this).parent().attr('data-id');
var a = $('<a href="' + url + '"></a>');
a.append($(this).parent());
inventory.append(a);
});
var inventories = $('.inventory');
if (inventories.length > 0) {
var child_count = inventory.children().length;
inventory.attr('data-items', child_count);
var last_div = null;
inventories.each(function () {
if (child_count > $(this).children().length) {
return false;
}
last_div = $(this);
});
if (last_div) {
last_div.after(inventory);
} else {
$('body').prepend(inventory);
}
} else {
$('#empty-accounts').before(inventory);
}
}
$(document).ready(function () {
$.when(fetchAccounts()).done(function () {
handleAccounts();
});
});