-
Notifications
You must be signed in to change notification settings - Fork 0
/
Steam_Card_Exchange_Watchlist_Synchronizer.user.js
235 lines (203 loc) · 10.3 KB
/
Steam_Card_Exchange_Watchlist_Synchronizer.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// ==UserScript==
// @name Steam Card Exchange Watchlist Synchronizer
// @namespace Steam Card Exchange Watchlist Synchronizer
// @author Laurvin
// @description Synchs with actual Steam Inventory
// @version 4.0
// @icon http://i.imgur.com/XYzKXzK.png
// @downloadURL https://github.com/Laurvin/Steam-Card-Exchange-Watchlist-Synchronizer/raw/master/Steam_Card_Exchange_Watchlist_Synchronizer.user.js
// @updateURL https://github.com/Laurvin/Steam-Card-Exchange-Watchlist-Synchronizer/raw/master/Steam_Card_Exchange_Watchlist_Synchronizer.user.js
// @match http://www.steamcardexchange.net/index.php?userlist
// @match https://www.steamcardexchange.net/index.php?userlist
// @grant GM_xmlhttpRequest
// @run-at document-idle
// ==/UserScript==
/* globals jQuery, $ */
var Steamids = [];
var InventoryAmounts = {};
var CardAmounts = {};
var myVar;
var IncompleteLoad = false;
$(document).ready(function() {
init();
});
function init() {
addHTMLElements();
}
function addHTMLElements() {
$("<style type='text/css'> .needed1 {color: #d30000;} .needed2 {color: #f56600;} .needed3 {color: #f59e00;} </style>").appendTo("head");
$('span[class="tracking-wider font-league-gothic"]').eq(1).after(' <a class="btn-primary lg:w-min" id="SynchIt">SYNCH</a>');
$('#SynchIt').on("click", SynchLists);
}
function SynchLists() {
$('div[class="flex items-center p-2 mx-auto mt-0.5 leading-none bg-black"]').after('<div class="content-box-normal" style="line-height: 20px;" id="SynchDiv"><p>Synching, please be patient and keep in mind you need to be logged into Steam on this browser for this to work.</p></div>');
$('#SynchDiv').append('<p>Loading Steam Inventory in 2,000 item chunks. <span id="SteamInvLoading">Loading from 0 onwards.</span>');
$('#SynchDiv').append('<p>Number of games with cards in Steam Inventory: <strong><span id="SteamInvTotals">0</span></strong></p>');
loadInventory('https://steamcommunity.com/my/inventory/json/753/6');
}
function monkeyRequest(url) {
return new Promise(function(resolve, reject) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
timeout: 25000,
onload: function(response) {
var InvJSON = JSON.parse(response.responseText);
resolve(InvJSON);
},
onerror: function(response) {
console.log(response.statusText);
reject(response.statusText);
},
ontimeout: function(response) {
reject("Timed out!");
}
});
});
}
function loadInventory(url) {
monkeyRequest(url).then(function(response) {
parseInvJSON(response);
}, function(error) {
console.log(error);
IncompleteLoad = true;
$('#SynchDiv').append('<p>FAILED to load (part of) Steam Inventory! See if this link works: <a href="http://steamcommunity.com/my/inventory/json/753/6">http://steamcommunity.com/my/inventory/json/753/6</a>, if not then Steam is down or things have changed. Reload this page to try again.</p>');
})
}
function parseInvJSON(InvJSON) {
$.each(InvJSON.rgInventory, function(index, item) {
if (hasOwnProperty(InventoryAmounts, item.classid + "_" + item.instanceid)) {
InventoryAmounts[item.classid + "_" + item.instanceid] += 1;
} else {
InventoryAmounts[item.classid + "_" + item.instanceid] = 1;
}
});
$.each(InvJSON.rgDescriptions, function(index, item) {
if (!item.type.includes("Foil Trading Card") && item.type.includes("Trading Card")) {
if (Steamids.includes(item.market_fee_app)) {
CardAmounts[item.market_fee_app] += InventoryAmounts[item.classid + "_" + item.instanceid];
} else {
Steamids.push(item.market_fee_app);
CardAmounts[item.market_fee_app] = InventoryAmounts[item.classid + "_" + item.instanceid];
}
}
});
$('#SteamInvTotals').text(Steamids.length);
if (InvJSON.more === true) {
$('#SteamInvLoading').text('Loading from ' + InvJSON.more_start + ' onwards.');
loadInventory('http://steamcommunity.com/my/inventory/json/753/6?start=' + InvJSON.more_start);
} else {
console.log('IncompleteLoad', IncompleteLoad, 'success', InvJSON.success);
if (IncompleteLoad === false && InvJSON.success === true)
{
makeChanges();
}
else
{
$('#SynchDiv').append('<p>FAILURE loading (part of) Steam Inventory! See if this link works: <a href="http://steamcommunity.com/my/inventory/json/753/6">http://steamcommunity.com/my/inventory/json/753/6</a>, if not then Steam is down or things have changed. Reload this page to try again.</p>');
}
}
}
function makeChanges() {
$('#SteamInvLoading').text('All loaded!');
var SCEids = $('table.dataTable a').map(function() // Filling array with all games in SCE Watchlist.
{
var SteamID = $(this).attr('href');
SteamID = SteamID.substring(SteamID.lastIndexOf('-') + 1);
return SteamID;
}).get();
$('#SynchDiv').append('<p>Number of games in SCE Watchlist (table below): <strong>' + SCEids.length + '</strong></p>');
var SCEids2 = new Set(SCEids);
var InSteamInvNotInSCE = [...new Set([...Steamids].filter(x => !SCEids2.has(x)))];
$('#SynchDiv').append('<p><br />Games in Steam Inventory but not in Watchlist: <strong>' + InSteamInvNotInSCE.length + '</strong></p>');
if (InSteamInvNotInSCE.length > 0) {
$.each(InSteamInvNotInSCE, function(index, item) {
$('#SynchDiv').append('<a id="id' + item + '" href="https://www.steamcardexchange.net/index.php?inventorygame-appid-' + item + '" style="display:inline-block;min-width:50px;margin: 4px 3px 4px 0;" target="_blank">' + item + '</a>');
});
AddRemoveFromSCEWatchlist("add", InSteamInvNotInSCE);
}
var Steamids2 = new Set(Steamids);
var InSCENotInSteamInv = [...new Set([...SCEids].filter(x => !Steamids2.has(x)))];
$('#SynchDiv').append('<p><br />Games in Watchlist but not in Steam Inventory: <strong>' + InSCENotInSteamInv.length + '</strong><br /></p>');
if (InSCENotInSteamInv.length > 0) {
$.each(InSCENotInSteamInv, function(index, item) {
$('#SynchDiv').append('<a id="id' + item + '" href="https://www.steamcardexchange.net/index.php?inventorygame-appid-' + item + '" style="display:inline-block;min-width:50px;margin: 4px 3px 4px 0;" target="_blank">' + item + '</a>');
});
AddRemoveFromSCEWatchlist("remove", InSCENotInSteamInv);
}
$('#SynchDiv').append('<p><br />Working... AppIDs should turn green or red one by one, if not, rate limiting might have borked it. It pays to check results; bugs are always possible.</p>');
$('#SynchIt').remove();
console.log("Starting Table Additions!");
var MyRows = $('#private_watchlist').find('tbody').find('tr');
for (var i = 0; i < MyRows.length; i++) {
var appID = $(MyRows[i]).find('a').attr('href');
appID = appID.substring(appID.lastIndexOf('-') + 1);
var SetSize = $(MyRows[i]).find('td').eq(3).text();
if (CardAmounts[appID] === undefined) CardAmounts[appID] = 0; // If no cards in Inventory this throws up a problem.
var BadgesAbleToCreate = Math.floor(CardAmounts[appID] / SetSize);
var RemainingCards = CardAmounts[appID] - (BadgesAbleToCreate * SetSize);
var CardsNeeded = SetSize - RemainingCards;
$(MyRows[i]).append('<td>' + CardAmounts[appID] + '</td>');
$(MyRows[i]).append('<td>' + BadgesAbleToCreate + '</td>');
$(MyRows[i]).append('<td class="needed' + CardsNeeded + '">' + CardsNeeded + '</td>');
$(MyRows[i]).append('<td>' + RemainingCards + '</td>');
}
var table = $('#private_watchlist').DataTable();
table.destroy(); // Need to destroy the table and then add headers before initiating new table.
$("#private_watchlist thead tr").append('<th title="Cards Owned" class="w-14">C O</th>');
$("#private_watchlist thead tr").append('<th title="Possible Badges to be created" class="w-14">P B</th>');
$("#private_watchlist thead tr").append('<th title="Cards needed for another badge" class="w-14">C N</th>');
$("#private_watchlist thead tr").append('<th title="Cards remaining after crafting badges" class="w-14">C R</th>');
$('#private_watchlist').dataTable({
dom: 'rt<"dataTables_footer"ip>',
"searching": false,
pageLength: -1,
autoWidth: false,
stateSave: true,
"order": [[4, 'asc'],[7, 'asc']]
});
$('#SCEFilter').trigger('click'); // After synch we sort on worth and cards needed and show only green.
console.log("Finished Table Additions!");
CardAmounts = null;
}
function AddRemoveFromSCEWatchlist(add_or_remove, appIDs) {
var IDcolor = (add_or_remove == "add") ? "#1DAF07" : "#B52426";
if (appIDs.length > 0) {
var current_id = appIDs[0];
console.log("current_id", current_id);
appIDs.shift();
$.ajax({
method: 'POST',
url: 'https://www.steamcardexchange.net/index.php?inventorygame-appid-' + current_id,
headers: {
"Content-type": "application/x-www-form-urlencoded"
},
data: encodeURI(add_or_remove + "=true"),
timeout: 6000,
statusCode: {
503: function(response) {
console.log("503'ed!");
$('#SynchDiv').append('<p>Ran into a 503 error! Rate limiting stops us from processing more for now.</p>');
return;
}
},
success: function(html, textStatus) {
console.log(add_or_remove, current_id);
$("#id" + current_id).css("color", IDcolor);
myVar = setTimeout(AddRemoveFromSCEWatchlist, 1500, add_or_remove, appIDs);
},
error: function(xhr, status, errorThrown) {
console.log("Error! " + status + errorThrown);
alert("Error! " + status + errorThrown);
}
});
} else {
console.log("Done adding/removing.");
$('#SynchDiv').append('<p>Processed all of <span style="color:' + IDcolor + ';">' + add_or_remove + '</span> list.</p>');
}
}
function hasOwnProperty(obj, prop) {
var proto = obj.__proto__ || obj.constructor.prototype;
return (prop in obj) &&
(!(prop in proto) || proto[prop] !== obj[prop]);
}