@@ -3,10 +3,16 @@ import '../components/trade_offer/trade_item_holder_metadata';
33import '../components/trade_offer/auto_fill' ;
44import { ClassIdAndInstanceId , rgDescription , rgInventoryAsset , TradeInventory } from '../types/steam' ;
55import { fetchRegisteredSteamAPIKey } from '../utils/key' ;
6+ import { deserializeForm } from '../utils/browser' ;
7+ import { AppId } from '../types/steam_constants' ;
8+ import { ClientSend } from '../bridge/client' ;
9+ import { AnnotateOffer } from '../bridge/handlers/annotate_offer' ;
10+ import { of } from 'rxjs' ;
611
712init ( 'src/lib/page_scripts/trade_offer.js' , main ) ;
813
914async function main ( ) {
15+ injectAnnotateOffer ( ) ;
1016 injectInventoryFallback ( ) ;
1117}
1218
@@ -117,3 +123,59 @@ function injectInventoryFallback() {
117123 ) ;
118124 } ;
119125}
126+
127+ interface JsonTradeofferAsset {
128+ appid : number ;
129+ contextid : string ;
130+ amount : number ;
131+ assetid : string ;
132+ }
133+
134+ interface JsonTradeoffer {
135+ me : {
136+ assets : JsonTradeofferAsset [ ] ;
137+ } ;
138+ them : {
139+ assets : JsonTradeofferAsset [ ] ;
140+ } ;
141+ version : number ;
142+ }
143+
144+ function injectAnnotateOffer ( ) {
145+ // Annotate offers for use in CSFloat Market, if the user isn't logged into CSFloat this does nothing
146+ // Similarly if they don't have an active sale, it does nothing
147+ $J ( document ) . on ( 'ajaxComplete' , async ( event , request , settings ) => {
148+ if ( ! settings . url . includes ( 'tradeoffer/new/send' ) ) {
149+ // Ignore requests that aren't a new trade offer
150+ return ;
151+ }
152+
153+ const offer_id = request ?. responseJSON ?. tradeofferid ;
154+
155+ if ( ! offer_id ) {
156+ // Something wrong with the format
157+ return ;
158+ }
159+
160+ let assets_to_send : string [ ] = [ ] ;
161+ let assets_to_receive : string [ ] = [ ] ;
162+ const deserialized = deserializeForm ( settings . data ) as { json_tradeoffer ?: string } ;
163+
164+ if ( deserialized && deserialized . json_tradeoffer ) {
165+ try {
166+ const parsed = JSON . parse ( deserialized . json_tradeoffer ) as JsonTradeoffer ;
167+ assets_to_send = parsed . me . assets . filter ( ( e ) => e . appid === AppId . CSGO ) . map ( ( e ) => e . assetid ) ;
168+ assets_to_receive = parsed . them . assets . filter ( ( e ) => e . appid === AppId . CSGO ) . map ( ( e ) => e . assetid ) ;
169+ } catch ( e ) {
170+ console . error ( 'failed to parse json tradeoffer' , e , deserialized . json_tradeoffer ) ;
171+ // Still proceed with annotating the offer id on a best-effort
172+ }
173+ }
174+
175+ await ClientSend ( AnnotateOffer , {
176+ assets_to_send,
177+ assets_to_receive,
178+ offer_id : offer_id ,
179+ } ) ;
180+ } ) ;
181+ }
0 commit comments