Skip to content

Commit 30b5d26

Browse files
authored
Merge branch 'master' into develop
2 parents 3406785 + 7ee9b04 commit 30b5d26

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

userscripts/Bazaar Auto Price (Torn PDA).js

Lines changed: 40 additions & 19 deletions
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.0
4+
// @version 1.0.1
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
@@ -36,20 +36,27 @@ const key = "###PDA-APIKEY###";
3636
*/
3737
function getLowestPrice(itemId) {
3838
const baseURL = "https://api.torn.com/v2/market";
39-
const searchParams = new URLSearchParams({ selections: "itemmarket", key, id: itemId, offset: "0" });
40-
const url = new URL(`?${searchParams.toString()}`, baseURL);
41-
return fetch(url).then((res) => res.json()).then((data) => {
42-
if ("error" in data) throw new Error(data.error.error);
43-
const price = data?.itemmarket?.listings?.[0]?.price;
44-
if (typeof price === "number" && price >= 1) return price;
45-
throw new Error(`Invalid price: ${price}`);
39+
const searchParams = new URLSearchParams({
40+
selections: "itemmarket",
41+
key,
42+
id: itemId,
43+
offset: "0",
4644
});
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+
});
4754
}
4855

4956
/**
5057
* Updates the input field directly and then emits the event to trick React into updating its state. Pinched from TornTools.
51-
* @param {HTMLInputElement} input
52-
* @param {string | number} value
58+
* @param {HTMLInputElement} input
59+
* @param {string | number} value
5360
* @returns {void}
5461
* @see https://github.com/Mephiles/torntools_extension/blob/54db1d1dbe2dc84e3267d56815e0dedce36e4bf1/extension/scripts/global/functions/torn.js#L1573
5562
*/
@@ -61,30 +68,44 @@ function updateInput(input, value) {
6168

6269
/**
6370
* Takes an input and sets the price to the current lowest price minus the diff
64-
* @param {HTMLInputElement} input
71+
* @param {HTMLInputElement} input
6572
*/
6673
async function addPrice(input) {
6774
if (!(input instanceof HTMLInputElement)) throw new Error("Input is not an HTMLInputElement");
6875
const row = input.closest("div[class*=itemRowWrapper]");
6976
const image = row?.querySelector("img");
7077
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");
7179
const itemId = image.src?.match(/\/images\/items\/([\d]+)\//)?.[1];
7280
if (!itemId) throw new Error("Could not find item ID");
7381
const currentLowestPrice = await getLowestPrice(itemId);
7482
if (!currentLowestPrice) throw new Error("Could not get lowest price");
7583
// Sets price to either 1 or the current lowest price minus 5, whichever is higher. This prevents negative prices
7684
const priceToSet = Math.max(1, currentLowestPrice - diff);
7785
updateInput(input, priceToSet);
86+
input.classList.add("kw--price-set");
7887
}
7988

8089
function main() {
81-
$(document).on("click", "div[class*=itemRowWrapper] div[class*=priceInputWrapper] > div.input-money-group > input.input-money:not([type=hidden])", (e) => {
82-
const input = e.target;
83-
addPrice(input).catch((e) => {
84-
console.error(e);
85-
input.style.outline = "2px solid red";
86-
})
87-
});
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+
);
88106
}
89107

90-
main();
108+
main();
109+
110+
// Custom error class, used to display a warning outline (yellow) instead of an error outline (red)
111+
class Warning extends Error {}

0 commit comments

Comments
 (0)