1
1
// ==UserScript==
2
2
// @name Item Market Auto Price
3
3
// @namespace dev.kwack.torn.imarket-auto-price
4
- // @version 1.0.0
4
+ // @version 1.0.1
5
5
// @description Automatically set the price of items relative to the current market
6
6
// @author Kwack [2190604]
7
7
// @match https://www.torn.com/page.php?sid=ItemMarket
@@ -36,20 +36,27 @@ const key = "###PDA-APIKEY###";
36
36
*/
37
37
function getLowestPrice ( itemId ) {
38
38
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" ,
46
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
+ } ) ;
47
54
}
48
55
49
56
/**
50
57
* 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
53
60
* @returns {void }
54
61
* @see https://github.com/Mephiles/torntools_extension/blob/54db1d1dbe2dc84e3267d56815e0dedce36e4bf1/extension/scripts/global/functions/torn.js#L1573
55
62
*/
@@ -61,30 +68,44 @@ function updateInput(input, value) {
61
68
62
69
/**
63
70
* Takes an input and sets the price to the current lowest price minus the diff
64
- * @param {HTMLInputElement } input
71
+ * @param {HTMLInputElement } input
65
72
*/
66
73
async function addPrice ( input ) {
67
74
if ( ! ( input instanceof HTMLInputElement ) ) throw new Error ( "Input is not an HTMLInputElement" ) ;
68
75
const row = input . closest ( "div[class*=itemRowWrapper]" ) ;
69
76
const image = row ?. querySelector ( "img" ) ;
70
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" ) ;
71
79
const itemId = image . src ?. match ( / \/ i m a g e s \/ i t e m s \/ ( [ \d ] + ) \/ / ) ?. [ 1 ] ;
72
80
if ( ! itemId ) throw new Error ( "Could not find item ID" ) ;
73
81
const currentLowestPrice = await getLowestPrice ( itemId ) ;
74
82
if ( ! currentLowestPrice ) throw new Error ( "Could not get lowest price" ) ;
75
83
// Sets price to either 1 or the current lowest price minus 5, whichever is higher. This prevents negative prices
76
84
const priceToSet = Math . max ( 1 , currentLowestPrice - diff ) ;
77
85
updateInput ( input , priceToSet ) ;
86
+ input . classList . add ( "kw--price-set" ) ;
78
87
}
79
88
80
89
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
+ ) ;
88
106
}
89
107
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