This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbptf-bot-utilities.user.js
423 lines (365 loc) · 15 KB
/
bptf-bot-utilities.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// ==UserScript==
// @name Backpack.tf - Bot Utilities
// @namespace https://github.com/Bonfire
// @version 1.0.18
// @description A script to provide various TF2Autobot utilities on backpack.tf
// @author Bon
// @downloadURL https://github.com/Bonfire/bptf-bot-utilities/raw/master/bptf-bot-utilities.user.js
// @updateURL https://github.com/Bonfire/bptf-bot-utilities/raw/master/bptf-bot-utilities.meta.js
// @include /^https?:\/\/backpack\.tf\/.*
// @grant GM_setClipboard
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @require https://code.jquery.com/jquery-3.6.0.slim.min.js
// @run-at document-end
// ==/UserScript==
(async () => {
let fetchedData = await GM_getValue("keyData");
// Fetch the price of a key in refined and store it
// Only fetch this if the user has no keyData stored or it's been 30 minutes since the last fetch
if (fetchedData) {
let keyData = JSON.parse(fetchedData);
let elapsedMillis = new Date() - keyData["timeStamp"];
if (elapsedMillis >= 1_800_000) {
fetchKeyPrice();
} else {
let KEY_PRICE = keyData["keyPrice"];
console.log(
"Key price fetched from storage. Current key price: " +
KEY_PRICE +
" ref"
);
console.log(
"Key price will next be fetched remotely in " +
((1_800_000 - elapsedMillis) / 60_000).toFixed(2) +
" mins"
);
}
} else {
fetchKeyPrice();
}
// Stock item def index mappings
const stockMap = new Map();
stockMap
.set("0", "190") // Bat
.set("1", "191") // Bottle
.set("2", "192") // Fireaxe
.set("3", "193") // Club
.set("4", "194") // Knife
.set("5", "195") // Fists
.set("6", "196") // Shovel
.set("7", "197") // Wrench
.set("8", "198") // Bonesaw
.set("9", "199") // Shotgun - Engineer (Primary)
.set("10", "199") // Shotgun - Soldier
.set("11", "199") // Shotgun - Heavy
.set("12", "199") // Shotgun - Pyro
.set("13", "200") // Scattergun
.set("14", "201") // Sniper Rifle
.set("15", "202") // Minigun
.set("16", "203") // SMG
.set("17", "204") // Syringe Gun
.set("18", "205") // Rocket Launcher
.set("19", "206") // Grenade Launcher
.set("20", "207") // Stickybomb Launcher
.set("21", "208") // Flamethrower
.set("22", "209") // Pistol - Engineer
.set("23", "209") // Pistol - Scout
.set("24", "210") // Revolver
.set("25", "737") // Construction PDA
.set("29", "211") // Medigun
.set("30", "212") // Invis Watch
.set("735", "736"); // Sapper
// Modify popover
$(document).on("mouseover", ".item", function () {
let hoveredItem = this;
let itemElement = $(hoveredItem)[0];
let popoverLoad = setInterval(function () {
// Add the "bot utility elements row"
if ($(hoveredItem).next().hasClass("popover")) {
if (!$("#bot-utility-elements").length) {
let botUtilityElements = document.createElement("dd");
botUtilityElements.className = "popover-btns";
botUtilityElements.id = "bot-utility-elements";
$("#popover-search-links")[0].before(botUtilityElements);
}
let addUtilitiesLoad = setInterval(function () {
let itemSKU = itemLookup(itemElement);
// Add the "<sku>" button
if (!$("#sku-item-button").length) {
let skuItemButton = document.createElement("a");
skuItemButton.id = "sku-item-button";
skuItemButton.className = "btn btn-default btn-xs";
let skuIcon = document.createElement("i");
skuIcon.className = "fa fa-book";
let itemCommand = `${itemSKU}`;
$(skuItemButton).data("itemCommand", itemCommand);
$(skuIcon).data("itemCommand", itemCommand);
skuItemButton.prepend(skuIcon);
$("#bot-utility-elements").append(skuItemButton);
$("#sku-item-button").on("click", (event) => {
GM_setClipboard(
$(event.target).data("itemCommand"),
"text/plain"
);
});
}
// Add the "Add Item" button
if (!$("#add-item-button").length) {
let addItemButton = document.createElement("a");
addItemButton.id = "add-item-button";
addItemButton.className = "btn btn-default btn-xs";
addItemButton.textContent = " Add";
let plusIcon = document.createElement("i");
plusIcon.className = "fa fa-plus";
let itemCommand = `!add sku=${itemSKU}`;
$(addItemButton).data("itemCommand", itemCommand);
$(plusIcon).data("itemCommand", itemCommand);
addItemButton.prepend(plusIcon);
$("#bot-utility-elements").append(addItemButton);
$("#add-item-button").on("click", (event) => {
GM_setClipboard(
$(event.target).data("itemCommand"),
"text/plain"
);
});
}
// Add the "Update Item" button
if (!$("#update-item-button").length) {
let updateItemButton = document.createElement("a");
updateItemButton.id = "update-item-button";
updateItemButton.className = "btn btn-default btn-xs";
updateItemButton.textContent = " Update";
let editIcon = document.createElement("i");
editIcon.className = "fa fa-edit";
let itemCommand = `!update sku=${itemSKU}`;
$(updateItemButton).data("itemCommand", itemCommand);
$(editIcon).data("itemCommand", itemCommand);
updateItemButton.prepend(editIcon);
$("#bot-utility-elements").append(updateItemButton);
$("#update-item-button").on("click", (event) => {
GM_setClipboard(
$(event.target).data("itemCommand"),
"text/plain"
);
});
}
// Add the "Remove Item" button
if (!$("#remove-item-button").length) {
let removeItemButton = document.createElement("a");
removeItemButton.id = "remove-item-button";
removeItemButton.className = "btn btn-default btn-xs";
removeItemButton.textContent = " Remove";
let minusIcon = document.createElement("i");
minusIcon.className = "fa fa-minus";
let itemCommand = `!remove sku=${itemSKU}`;
$(removeItemButton).data("itemCommand", itemCommand);
$(minusIcon).data("itemCommand", itemCommand);
removeItemButton.prepend(minusIcon);
$("#bot-utility-elements").append(removeItemButton);
$("#remove-item-button").on("click", (event) => {
GM_setClipboard(
$(event.target).data("itemCommand"),
"text/plain"
);
});
}
// Add the "Pricecheck Item" button
if (!$("#pricecheck-item-button").length) {
let pricecheckItemButton = document.createElement("a");
pricecheckItemButton.id = "pricecheck-item-button";
pricecheckItemButton.className = "btn btn-default btn-xs";
pricecheckItemButton.textContent = " PC";
let priceIcon = document.createElement("i");
priceIcon.className = "fa fa-tags";
let itemCommand = `!pricecheck sku=${itemSKU}`;
$(pricecheckItemButton).data("itemCommand", itemCommand);
$(priceIcon).data("itemCommand", itemCommand);
pricecheckItemButton.prepend(priceIcon);
$("#bot-utility-elements").append(pricecheckItemButton);
$("#pricecheck-item-button").on("click", (event) => {
GM_setClipboard(
$(event.target).data("itemCommand"),
"text/plain"
);
});
}
// Add the "Match Listing" button
if (
$(hoveredItem).data("listing_intent") &&
!$("#match-listing-button").length
) {
let matchListingButton = document.createElement("a");
matchListingButton.id = "match-listing-button";
matchListingButton.className = "btn btn-default btn-xs";
matchListingButton.textContent = " Match";
let matchIcon = document.createElement("i");
matchIcon.className = "fa fa-arrow-circle-down";
let itemCommand = "";
let [keyPrice, metalPrice] = extractPrice($(hoveredItem));
if ($(hoveredItem).data("listing_intent") === "buy") {
itemCommand = `!update sku=${itemSKU}${
keyPrice != null ? "&buy.keys=" + keyPrice : ""
}${metalPrice != null ? "&buy.metal=" + metalPrice : ""} `;
} else {
itemCommand = `!update sku=${itemSKU}${
keyPrice != null ? "&sell.keys=" + keyPrice : ""
}${metalPrice != null ? "&sell.metal=" + metalPrice : ""} `;
}
$(matchListingButton).data("itemCommand", itemCommand);
$(matchIcon).data("itemCommand", itemCommand);
matchListingButton.prepend(matchIcon);
$("#bot-utility-elements").append(matchListingButton);
$("#match-listing-button").on("click", (event) => {
GM_setClipboard(
$(event.target).data("itemCommand"),
"text/plain"
);
});
}
clearInterval(addUtilitiesLoad);
}, 50);
setTimeout(function () {
clearInterval(addUtilitiesLoad);
}, 1000);
clearInterval(popoverLoad);
}
}, 50);
setTimeout(function () {
clearInterval(popoverLoad);
}, 750);
});
function itemLookup(itemElement) {
let item = $(itemElement);
let tempDefIndex = item.attr("data-defindex");
let itemName = item.attr("data-original-title");
let itemDefIndex = stockMap.has(tempDefIndex)
? stockMap.get(tempDefIndex)
: tempDefIndex;
let itemQuality = item.attr("data-quality");
let isUncraftable = item.attr("data-craftable") !== "1";
let itemEffectID = item.attr("data-effect_id");
let itemSkinInfo = item.find(".item-icon");
let itemWear, itemSkin;
if (itemSkinInfo.length > 0) {
itemSkinInfo = itemSkinInfo
.css("background-image")
.match(/warpaint\/[(?!_)\S]+_[0-9]+_[0-9]+_[0-9]+\.png/g);
if (itemSkinInfo !== null) {
itemSkin = itemSkinInfo[0].split("_")[1];
itemWear = itemSkinInfo[0].split("_")[2];
}
}
let isStrange = item.attr("data-quality_elevated") === "11";
let itemKillstreak = item.attr("data-ks_tier");
let isFestivized =
item.attr("data-original-title")?.toLowerCase().indexOf("festivized") !==
-1;
let isAustralium = item.attr("data-australium") === "1";
// Other item attributes
let crateSeries = item.attr("data-crate");
let itemTarget, itemOutput, itemOutputQuality;
const priceIndex = item.attr("data-priceindex").split("-");
if (priceIndex[0] !== "0") {
switch (item.attr("data-base_name")) {
case "Chemistry Set":
// Only change defindex if it's a buy listing or it's the item on the stats page.
// So it won't change anything if it's from someones inventory or a sell listing.
if (!item.attr("data-original_id")) {
if (itemName.includes("Festive")) itemDefIndex = "20007";
else if (itemName.includes("Collector's")) itemDefIndex = "20006";
// Unsure about this one couldn't find any items might be unused.
else if (itemName.includes("Strange")) itemDefIndex = "20008";
// Assume all strangifier's are series 2 which they are definetly not :(
// Used series 2 since it's the one with the biggest volume (Don't quote me on that).
// Available defindexes; Series 1: 20000, Series 1 Rare: 20001, Series 2: 20005, Series 3: 20009.
else if (itemName.includes("Strangifier")) itemDefIndex = "20005";
}
case "Fabricator":
[itemOutput, itemOutputQuality, itemTarget] = priceIndex;
break;
case "Kit":
itemTarget = priceIndex[1];
break;
case "Strangifier":
case "Unusualifier":
itemTarget = priceIndex[0];
}
}
if (itemDefIndex == "9536") {
itemDefIndex =
(Math.floor(itemSkin / 100) % 2 === 0 ? "17" : "16") + itemSkin;
}
// Get the full item SKU, and be sure to remove any pesky whitespaces
let itemSKU = `${itemDefIndex};\
${itemQuality}\
${itemEffectID ? `;u${itemEffectID}` : ""}\
${isAustralium ? ";australium" : ""}\
${isUncraftable ? ";uncraftable" : ""}\
${itemSkinInfo ? `;w${itemWear};pk${itemSkin}` : ""}\
${isStrange ? ";strange" : ""}\
${itemKillstreak ? `;kt-${itemKillstreak}` : ""}\
${itemTarget ? `;td-${itemTarget}` : ""}\
${isFestivized ? ";festive" : ""}\
${crateSeries ? `;c${crateSeries}` : ""}\
${itemOutput ? `;od-${itemOutput}` : ""}\
${itemOutputQuality ? `;oq-${itemOutputQuality}` : ""}`;
return itemSKU.replace(/\s/g, "");
}
function extractPrice(hoveredItem) {
let listingPrice = $(hoveredItem).data("listing_price");
// Split the string in the event that the item has a key, ref price
let splitString = listingPrice.split(",");
// If we are working with a key, ref price...
if (splitString.length > 1) {
return [
splitString[0].replace("keys", "").replace("key", "").trim(),
splitString[1].replace("ref", "").trim(),
];
} else {
if (splitString[0].includes("key")) {
// If the item is priced in the "X keys" format
let keyPrice = splitString[0]
.replace("keys", "")
.replace("key", "")
.trim();
let splitKey = keyPrice.split(".");
if (splitKey.length > 1) {
return [splitKey[0], KEY_PRICE * (splitKey[1] / 10)];
} else {
return [keyPrice, null];
}
} else {
// If the item is priced in the "X ref" format
return [null, splitString[0].replace("ref", "").trim()];
}
}
}
function fetchKeyPrice() {
// Reach out to Prices.TF to grab the current key selling price
GM_xmlhttpRequest({
method: "GET",
url: "https://autobot.tf/json/items/5021;6",
onload: async function (response) {
// Parse the sell price of the key in refined
var keyResponse = JSON.parse(response.responseText);
// Log to the user the current key price
console.log(
"Fetched key price from remote source: " +
keyResponse["sell"]["metal"] +
" ref"
);
// Set the KEY_PRICE
KEY_PRICE = keyResponse["sell"]["metal"];
let keyData = {
keyPrice: KEY_PRICE,
timeStamp: new Date().getTime(),
};
// Store the KEY_PRICE and the current timestamp
await GM_setValue("keyData", JSON.stringify(keyData));
console.log("Stored key data for 30 minutes");
},
});
}
})();