-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbptf-listing-reconcile.user.js
225 lines (204 loc) · 7.14 KB
/
bptf-listing-reconcile.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
// ==UserScript==
// @name bptf-listing-reconcile
// @namespace https://github.com/joekiller
// @version 0.4
// @description fixes all those broke ass listings
// @author joekiller
// @match https://backpack.tf/classifieds?*
// @icon https://www.google.com/s2/favicons?sz=64&domain=backpack.tf
// @downloadURL https://github.com/joekiller/bptf-listing-reconcile/raw/main/bptf-listing-reconcile.user.js
// @updateURL https://github.com/joekiller/bptf-listing-reconcile/raw/main/bptf-listing-reconcile.meta.js
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @run-at document-idle
// ==/UserScript==
const params = new URLSearchParams(window.location.search);
const steamid = params.get('steamid');
var liveAssets = localStorage.getItem('liveAssets');
var fixing = localStorage.getItem('fixing');
var live = localStorage.getItem('live') === 'true';
function reset() {
liveAssets = null;
localStorage.removeItem('liveAssets');
localStorage.removeItem('fixing');
localStorage.removeItem('live');
live = false;
fixing = null;
}
function fixAssets() {
let candidates = [];
if(candidates.length === 0) {
let panel = document.evaluate(
'//div[contains(@class, "panel-main") and descendant::span[text() = "Sell Orders"]]',
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue
const rows = panel.getElementsByClassName("listing");
for (let i = 0; i < rows.length; i++) {
let listing = rows[i];
if(listing.getElementsByClassName("tag bottom-right")[0].getElementsByTagName("span")[0].textContent.includes('$')) {
continue;
}
let listingId = listing.id.split('_')[1]
if(!liveAssets.includes(listingId)) {
candidates.push(listing.id);
}
}
if(!live && candidates.length > 0) {
window.alert( `${live ? 'Will delete' : 'Would have deleted'} ` + candidates.join() )
}
}
const hadCandidates = live && candidates.length > 0;
while(live && candidates.length > 0) {
const id = candidates.shift()
const listing = document.getElementById(id);
listing.getElementsByClassName('listing-remove')[0].click();
listing.getElementsByClassName('listing-remove-prompt')[0].click();
}
if(hadCandidates) {
return location.assign(location.href);
}
if(onLastPage()) {
localStorage.removeItem('fixing');
fixing = null;
if(live) {
reset();
}
window.alert( "Finished" );
openFirstPage();
run();
} else {
if(live && candidates.length > 0) {
fixAssets();
} else {
openNextPage();
}
}
}
// create fix button
var fixBtn = document.createElement( 'input' );
with( fixBtn ) {
setAttribute( 'value', 'Fix this shit' );
setAttribute( 'type', 'button' );
setAttribute( 'id', 'btn-fix' );
setAttribute( 'class', 'btn btn-default btn-fix' );
}
// create get inventory button
var inventoryBtn = document.createElement( 'a' );
with( inventoryBtn ) {
setAttribute( 'href', 'https://steamcommunity.com/inventory/'+ steamid +'/440/2?l=english&count=3000' );
setAttribute( 'target', '_blank' );
setAttribute( 'id', 'btn-load-inventory' );
setAttribute( 'class', 'btn btn-default btn-load-inventory' );
}
inventoryBtn.appendChild(document.createTextNode("Load Raw Inventory In New Tab"));
var inventoryInput = document.createElement( 'input' );
with( inventoryInput ) {
setAttribute( 'id', 'inventory' );
setAttribute( 'placeholder', 'Paste your inventory here' );
setAttribute( 'name', 'inventory' );
setAttribute( 'type', 'text' );
setAttribute( 'class', 'form-control inventory-input' );
}
// reset
var resetBtn = document.createElement( 'input' );
with( resetBtn ) {
setAttribute( 'value', 'Reset Reconcile Plugin' );
setAttribute( 'id', 'btn-reset' );
setAttribute( 'type', 'button' );
setAttribute( 'class', 'btn btn-default btn-reset' );
}
var liveBtn = document.createElement( 'input' );
with( liveBtn ) {
setAttribute( 'type', 'button' );
setAttribute( 'id', 'btn-live' );
setAttribute( 'class', 'btn btn-default btn-live' );
}
function openNextPage() {
let nextButton = document.getElementsByClassName('fa fa-angle-right')[0];
nextButton.click();
}
function openFirstPage() {
let firstPageButton = document.getElementsByClassName('fa fa-angle-double-left')[0];
firstPageButton.click();
}
function onFirstPage() {
return document.getElementsByClassName('fa fa-angle-left')[0].parentElement.parentElement.className == "disabled";
}
function onLastPage() {
return document.getElementsByClassName('fa fa-angle-right')[0].parentElement.parentElement.className == "disabled";
}
document.getElementById( 'search-crumbs' ).appendChild( fixBtn );
document.getElementById( 'search-crumbs' ).appendChild( liveBtn );
document.getElementById( 'search-crumbs' ).appendChild( resetBtn );
document.getElementById( 'search-crumbs' ).appendChild( inventoryBtn );
document.getElementById( 'search-crumbs' ).appendChild( inventoryInput );
function visibility() {
if(!liveAssets) {
inventoryBtn.style.display = "inline";
inventoryInput.style.display = "inline";
} else {
inventoryBtn.style.display = "none";
inventoryInput.style.display = "none";
}
if(liveAssets) {
fixBtn.style.display = "inline";
liveBtn.style.display = "inline";
resetBtn.style.display = "inline";
if(fixing) {
fixBtn.value = 'Fixing';
fixBtn.disabled = true;
} else {
fixBtn.value = 'Fix this shit';
fixBtn.disabled = false;
}
} else {
fixBtn.style.display = "none";
resetBtn.style.display = "none";
liveBtn.style.display = "none";
}
document.getElementById("btn-live").value = live ? 'Live Mode' : 'Test Mode';
}
function run() {
if(!fixing && !liveAssets && onFirstPage()) {
reset();
}
visibility();
if(fixing && liveAssets) {
fixAssets();
}
}
inventoryInput.addEventListener('change', (event) => {
if(!liveAssets) {
try {
liveAssets = JSON.parse(event.target.value).assets.map((i) => i.assetid);
localStorage.setItem('liveAssets', liveAssets);
} catch (e) {
reset();
window.alert( "There was an error: " + e)
console.error(e);
}
var input = document.getElementById("inventory");
if (input.value !== "") {
input.value = "";
}
run();
}
});
fixBtn.addEventListener('click', () => {
fixing = 'true'
localStorage.setItem('fixing', fixing);
run()
});
liveBtn.addEventListener('click', () => {
localStorage.setItem('live', !live ? 'true' : 'false');
live = !live;
document.getElementById("btn-live").value = live ? 'Live Mode' : 'Test Mode';
});
resetBtn.addEventListener('click', () => {
reset()
run()
});
run()