Skip to content

Commit 383be5e

Browse files
committed
Fill imarket item to max on tap when max-input button is hidden by torn
1 parent f029797 commit 383be5e

File tree

1 file changed

+77
-49
lines changed

1 file changed

+77
-49
lines changed

userscripts/Bazaar Auto Price (Torn PDA).js

+77-49
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name Item Market Auto Price
33
// @namespace dev.kwack.torn.imarket-auto-price
4-
// @version 1.0.1
4+
// @version 1.0.2
55
// @description Automatically set the price of items relative to the current market
66
// @author Kwack [2190604]
77
// @match https://www.torn.com/page.php?sid=ItemMarket
@@ -10,6 +10,13 @@
1010

1111
// @ts-check
1212

13+
const inputSelector = `
14+
div[class*=itemRowWrapper] div[class*=priceInputWrapper]
15+
> div.input-money-group > input.input-money:not([type=hidden]):not(.kw--price-set),
16+
div[class*=itemRowWrapper] div[class*=amountInputWrapper][class*=hideMaxButton]
17+
> div.input-money-group > input.input-money:not([type=hidden]):not(.kw--price-set)
18+
`;
19+
1320
/**
1421
* @type {number}
1522
* @readonly
@@ -35,22 +42,22 @@ const key = "###PDA-APIKEY###";
3542
* @returns {Promise<number>} the lowest price for the item
3643
*/
3744
function getLowestPrice(itemId) {
38-
const baseURL = "https://api.torn.com/v2/market";
39-
const searchParams = new URLSearchParams({
40-
selections: "itemmarket",
41-
key,
42-
id: itemId,
43-
offset: "0",
44-
});
45-
const url = new URL(`?${searchParams.toString()}`, baseURL);
46-
return fetch(url)
47-
.then((res) => res.json())
48-
.then((data) => {
49-
if ("error" in data) throw new Error(data.error.error);
50-
const price = data?.itemmarket?.listings?.[0]?.price;
51-
if (typeof price === "number" && price >= 1) return price;
52-
throw new Error(`Invalid price: ${price}`);
53-
});
45+
const baseURL = "https://api.torn.com/v2/market";
46+
const searchParams = new URLSearchParams({
47+
selections: "itemmarket",
48+
key,
49+
id: itemId,
50+
offset: "0",
51+
});
52+
const url = new URL(`?${searchParams.toString()}`, baseURL);
53+
return fetch(url)
54+
.then((res) => res.json())
55+
.then((data) => {
56+
if ("error" in data) throw new Error(data.error.error);
57+
const price = data?.itemmarket?.listings?.[0]?.price;
58+
if (typeof price === "number" && price >= 1) return price;
59+
throw new Error(`Invalid price: ${price}`);
60+
});
5461
}
5562

5663
/**
@@ -61,48 +68,69 @@ function getLowestPrice(itemId) {
6168
* @see https://github.com/Mephiles/torntools_extension/blob/54db1d1dbe2dc84e3267d56815e0dedce36e4bf1/extension/scripts/global/functions/torn.js#L1573
6269
*/
6370
function updateInput(input, value) {
64-
input.value = `${value}`;
65-
// Needed to trigger React to update its state
66-
input.dispatchEvent(new Event("input", { bubbles: true }));
71+
input.value = `${value}`;
72+
// Needed to trigger React to update its state
73+
input.dispatchEvent(new Event("input", { bubbles: true }));
6774
}
6875

6976
/**
7077
* Takes an input and sets the price to the current lowest price minus the diff
7178
* @param {HTMLInputElement} input
7279
*/
7380
async function addPrice(input) {
74-
if (!(input instanceof HTMLInputElement)) throw new Error("Input is not an HTMLInputElement");
75-
const row = input.closest("div[class*=itemRowWrapper]");
76-
const image = row?.querySelector("img");
77-
if (!image) throw new Error("Could not find image element");
78-
if (image.parentElement?.matches("[class*='glow-']")) throw new Warning("Skipping a glowing RW item");
79-
const itemId = image.src?.match(/\/images\/items\/([\d]+)\//)?.[1];
80-
if (!itemId) throw new Error("Could not find item ID");
81-
const currentLowestPrice = await getLowestPrice(itemId);
82-
if (!currentLowestPrice) throw new Error("Could not get lowest price");
83-
// Sets price to either 1 or the current lowest price minus 5, whichever is higher. This prevents negative prices
84-
const priceToSet = Math.max(1, currentLowestPrice - diff);
85-
updateInput(input, priceToSet);
86-
input.classList.add("kw--price-set");
81+
if (!(input instanceof HTMLInputElement))
82+
throw new Error("Input is not an HTMLInputElement");
83+
const row = input.closest("div[class*=itemRowWrapper]");
84+
const image = row?.querySelector("img");
85+
if (!image) throw new Error("Could not find image element");
86+
if (image.parentElement?.matches("[class*='glow-']"))
87+
throw new Warning("Skipping a glowing RW item");
88+
const itemId = image.src?.match(/\/images\/items\/([\d]+)\//)?.[1];
89+
if (!itemId) throw new Error("Could not find item ID");
90+
const currentLowestPrice = await getLowestPrice(itemId);
91+
if (!currentLowestPrice) throw new Error("Could not get lowest price");
92+
// Sets price to either 1 or the current lowest price minus 5, whichever is higher. This prevents negative prices
93+
const priceToSet = Math.max(1, currentLowestPrice - diff);
94+
updateInput(input, priceToSet);
95+
input.classList.add("kw--price-set");
96+
}
97+
98+
/**
99+
* Fills quantity inputs with the maximum quantity possible when
100+
* @param {HTMLInputElement} input
101+
*/
102+
async function addQuantity(input) {
103+
if (!(input instanceof HTMLInputElement))
104+
throw new Error("Input is not an HTMLInputElement");
105+
updateInput(input, "max");
106+
input.classList.add("kw--price-set");
87107
}
88108

89109
function main() {
90-
$(document).on(
91-
"click",
92-
"div[class*=itemRowWrapper] div[class*=priceInputWrapper] > div.input-money-group > input.input-money:not([type=hidden]):not(.kw--price-set)",
93-
(e) => {
94-
const input = e.target;
95-
addPrice(input).catch((e) => {
96-
if (e instanceof Warning) {
97-
console.warn(e);
98-
input.style.outline = "2px solid yellow";
99-
} else {
100-
console.error(e);
101-
input.style.outline = "2px solid red";
102-
}
103-
});
104-
}
105-
);
110+
$(document).on("click", inputSelector, (e) => {
111+
const input = e.target;
112+
if (input.getAttribute("placeholder") === "Qty") {
113+
addQuantity(input).catch((e) => {
114+
if (e instanceof Warning) {
115+
console.warn(e);
116+
input.style.outline = "2px solid yellow";
117+
} else {
118+
console.error(e);
119+
input.style.outline = "2px solid red";
120+
}
121+
});
122+
} else {
123+
addPrice(input).catch((e) => {
124+
if (e instanceof Warning) {
125+
console.warn(e);
126+
input.style.outline = "2px solid yellow";
127+
} else {
128+
console.error(e);
129+
input.style.outline = "2px solid red";
130+
}
131+
});
132+
}
133+
});
106134
}
107135

108136
main();

0 commit comments

Comments
 (0)