Skip to content

Commit

Permalink
Allows Proving Trade with Token
Browse files Browse the repository at this point in the history
  • Loading branch information
Step7750 committed Apr 4, 2024
1 parent a2db2a0 commit b80838d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/lib/bridge/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {ExtensionVersion} from './extension_version';
import {SendCookies} from './send_cookies';
import {HasPermissions} from './has_permissions';
import {MetaSettings} from './meta_settings';
import {ProveTradesToken} from './prove_trades_token';

export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
[RequestType.EXECUTE_SCRIPT_ON_PAGE]: ExecuteScriptOnPage,
Expand All @@ -32,4 +33,5 @@ export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
[RequestType.SEND_COOKIES]: SendCookies,
[RequestType.HAS_PERMISSIONS]: HasPermissions,
[RequestType.META_SETTINGS]: MetaSettings,
[RequestType.PROVE_TRADES_TOKEN]: ProveTradesToken,
};
25 changes: 25 additions & 0 deletions src/lib/bridge/handlers/prove_trades_token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {EmptyRequestHandler, SimpleHandler} from './main';
import {RequestType} from './types';

export interface ProveTradesTokenRequest {
token: string;
}

export interface ProveTradesTokenResponse {
message: string;
}

export const ProveTradesToken = new SimpleHandler<ProveTradesTokenRequest, ProveTradesTokenResponse>(
RequestType.PROVE_TRADES_TOKEN,
async (req) => {
const resp = await fetch(`https://csfloat.com/api/v1/trades/prove-token?token=${req.token}`, {
credentials: 'include',
});

if (resp.status !== 200) {
throw new Error('failed to prove, are you logged into CSFloat?');
}

return resp.json() as Promise<ProveTradesTokenResponse>;
}
);
1 change: 1 addition & 0 deletions src/lib/bridge/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export enum RequestType {
SEND_COOKIES,
HAS_PERMISSIONS,
META_SETTINGS,
PROVE_TRADES_TOKEN,
}
27 changes: 16 additions & 11 deletions src/lib/components/trade_history/trade_proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {CustomElement, InjectAppend, InjectionMode} from '../injectors';
import {FloatElement} from '../custom';
import {fetchListingTime} from './helpers';
import '../common/ui/steam-button';
import {ProveTradesToken} from '../../bridge/handlers/prove_trades_token';
import {ClientSend} from '../../bridge/client';

@CustomElement()
@InjectAppend('.tradehistoryrow .tradehistory_content', InjectionMode.CONTINUOUS)
export class TradeProof extends FloatElement {
@state()
private proofNumber: number | undefined;
private message: string | undefined;

@state()
private isProcessing = false;
Expand All @@ -20,12 +22,12 @@ export class TradeProof extends FloatElement {
}

render() {
return this.proofNumber
? html` <span>Proof: ${this.proofNumber}</span> `
return this.message
? html` <span>${this.message}</span> `
: html`
<csfloat-steam-button
@click="${this.onClick}"
.text="${this.isProcessing ? 'Computing Proof...' : 'CSFloat Proof'}"
.text="${this.isProcessing ? 'Proving...' : 'Prove Trade on CSFloat'}"
>
</csfloat-steam-button>
`;
Expand All @@ -34,14 +36,17 @@ export class TradeProof extends FloatElement {
private async onClick() {
this.isProcessing = true;

const index = $J('.tradehistoryrow').index($J(this).parent().parent());
const token = document
.getElementById('application_config')
?.getAttribute('data-loyalty_webapi_token')
?.replace('"', '')
.replace('"', '');

try {
this.proofNumber = await fetchListingTime(index);
} catch (e) {
alert(
"Failed to parse time, make sure you're on an english version of the page by appending ?l=english to the url"
);
const resp = await ClientSend(ProveTradesToken, {token});
this.message = resp.message;
} catch (e: any) {
alert(e.toString());
}
this.isProcessing = false;
}
}

0 comments on commit b80838d

Please sign in to comment.