1
1
// ==UserScript==
2
2
// @name Item Market Auto Price
3
3
// @namespace dev.kwack.torn.imarket-auto-price
4
- // @version 1.0.1
4
+ // @version 1.0.2
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
10
10
11
11
// @ts -check
12
12
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
+
13
20
/**
14
21
* @type {number }
15
22
* @readonly
@@ -35,22 +42,22 @@ const key = "###PDA-APIKEY###";
35
42
* @returns {Promise<number> } the lowest price for the item
36
43
*/
37
44
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
+ } ) ;
54
61
}
55
62
56
63
/**
@@ -61,48 +68,69 @@ function getLowestPrice(itemId) {
61
68
* @see https://github.com/Mephiles/torntools_extension/blob/54db1d1dbe2dc84e3267d56815e0dedce36e4bf1/extension/scripts/global/functions/torn.js#L1573
62
69
*/
63
70
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 } ) ) ;
67
74
}
68
75
69
76
/**
70
77
* Takes an input and sets the price to the current lowest price minus the diff
71
78
* @param {HTMLInputElement } input
72
79
*/
73
80
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 ( / \/ i m a g e s \/ i t e m s \/ ( [ \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 ( / \/ i m a g e s \/ i t e m s \/ ( [ \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" ) ;
87
107
}
88
108
89
109
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
+ } ) ;
106
134
}
107
135
108
136
main ( ) ;
0 commit comments