@@ -3,10 +3,15 @@ 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' ;
610
711init ( 'src/lib/page_scripts/trade_offer.js' , main ) ;
812
913async function main ( ) {
14+ injectAnnotateOffer ( ) ;
1015 injectInventoryFallback ( ) ;
1116}
1217
@@ -117,3 +122,59 @@ function injectInventoryFallback() {
117122 ) ;
118123 } ;
119124}
125+
126+ interface JsonTradeofferAsset {
127+ appid : number ;
128+ contextid : string ;
129+ amount : number ;
130+ assetid : string ;
131+ }
132+
133+ interface JsonTradeoffer {
134+ me : {
135+ assets : JsonTradeofferAsset [ ] ;
136+ } ;
137+ them : {
138+ assets : JsonTradeofferAsset [ ] ;
139+ } ;
140+ version : number ;
141+ }
142+
143+ function injectAnnotateOffer ( ) {
144+ // Annotate offers for use in CSFloat Market, if the user isn't logged into CSFloat this does nothing
145+ // Similarly if they don't have an active sale, it does nothing
146+ $J ( document ) . on ( 'ajaxComplete' , async ( event , request , settings ) => {
147+ if ( ! settings . url . includes ( 'tradeoffer/new/send' ) ) {
148+ // Ignore requests that aren't a new trade offer
149+ return ;
150+ }
151+
152+ const offer_id = request ?. responseJSON ?. tradeofferid ;
153+
154+ if ( ! offer_id ) {
155+ // Something wrong with the format
156+ return ;
157+ }
158+
159+ let assets_to_send : string [ ] = [ ] ;
160+ let assets_to_receive : string [ ] = [ ] ;
161+ const deserialized = deserializeForm ( settings . data ) as { json_tradeoffer ?: string } ;
162+
163+ if ( deserialized && deserialized . json_tradeoffer ) {
164+ try {
165+ const parsed = JSON . parse ( deserialized . json_tradeoffer ) as JsonTradeoffer ;
166+ assets_to_send = parsed . me . assets . filter ( ( e ) => e . appid === AppId . CSGO ) . map ( ( e ) => e . assetid ) ;
167+ assets_to_receive = parsed . them . assets . filter ( ( e ) => e . appid === AppId . CSGO ) . map ( ( e ) => e . assetid ) ;
168+ } catch ( e ) {
169+ console . error ( 'failed to parse json tradeoffer' , e , deserialized . json_tradeoffer ) ;
170+ // Still proceed with annotating the offer id on a best-effort
171+ }
172+ }
173+
174+ await ClientSend ( AnnotateOffer , {
175+ assets_to_send,
176+ assets_to_receive,
177+ offer_id : offer_id ,
178+ } ) ;
179+ } ) ;
180+ }
0 commit comments