Skip to content

Commit

Permalink
Adds Bridge Handler for Fetching Logged in Steam User
Browse files Browse the repository at this point in the history
  • Loading branch information
Step7750 committed Sep 12, 2024
1 parent 2f7400c commit 60c153b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/lib/bridge/handlers/fetch_steam_user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {SimpleHandler} from './main';
import {RequestType} from './types';

interface FetchSteamUserRequest {}

interface FetchSteamUserResponse {
isLoggedIn: boolean;
steamID?: string;
sessionID?: string;
}

export const FetchSteamUser = new SimpleHandler<FetchSteamUserRequest, FetchSteamUserResponse>(
RequestType.FETCH_STEAM_USER,
async (req) => {
const resp = await fetch('https://steamcommunity.com');
if (!resp.ok) {
throw new Error('non-ok response for steamcommunity.com');
}

const res: FetchSteamUserResponse = {
isLoggedIn: false,
};

const text = await resp.text();
const steamIDMatch = text.match(/g_steamID = "(\d+)"/);
if (steamIDMatch) {
res.isLoggedIn = true;
res.steamID = steamIDMatch[1];
}

const sessionIDMatch = text.match(/g_sessionID = "([0-9a-fA-F]+)"/);
if (sessionIDMatch) {
res.sessionID = sessionIDMatch[1];
}

return res;
}
);
2 changes: 2 additions & 0 deletions src/lib/bridge/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {PingSetupExtension} from './ping_setup_extension';
import {PingExtensionStatus} from './ping_extension_status';
import {PingCancelTrade} from './ping_cancel_trade';
import {CreateTradeOffer} from './create_trade_offer';
import {FetchSteamUser} from './fetch_steam_user';

export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
[RequestType.EXECUTE_SCRIPT_ON_PAGE]: ExecuteScriptOnPage,
Expand All @@ -38,4 +39,5 @@ export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
[RequestType.PING_EXTENSION_STATUS]: PingExtensionStatus,
[RequestType.PING_CANCEL_TRADE]: PingCancelTrade,
[RequestType.CREATE_TRADE_OFFER]: CreateTradeOffer,
[RequestType.FETCH_STEAM_USER]: FetchSteamUser,
};
1 change: 1 addition & 0 deletions src/lib/bridge/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export enum RequestType {
PING_EXTENSION_STATUS = 15,
PING_CANCEL_TRADE = 16,
CREATE_TRADE_OFFER = 17,
FETCH_STEAM_USER = 18,
}

0 comments on commit 60c153b

Please sign in to comment.