diff --git a/src/lib/bridge/handlers/fetch_steam_user.ts b/src/lib/bridge/handlers/fetch_steam_user.ts new file mode 100644 index 0000000..0e0e4ab --- /dev/null +++ b/src/lib/bridge/handlers/fetch_steam_user.ts @@ -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( + 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; + } +); diff --git a/src/lib/bridge/handlers/handlers.ts b/src/lib/bridge/handlers/handlers.ts index 8049a4c..310bcb3 100644 --- a/src/lib/bridge/handlers/handlers.ts +++ b/src/lib/bridge/handlers/handlers.ts @@ -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} = { [RequestType.EXECUTE_SCRIPT_ON_PAGE]: ExecuteScriptOnPage, @@ -38,4 +39,5 @@ export const HANDLERS_MAP: {[key in RequestType]: RequestHandler} = { [RequestType.PING_EXTENSION_STATUS]: PingExtensionStatus, [RequestType.PING_CANCEL_TRADE]: PingCancelTrade, [RequestType.CREATE_TRADE_OFFER]: CreateTradeOffer, + [RequestType.FETCH_STEAM_USER]: FetchSteamUser, }; diff --git a/src/lib/bridge/handlers/types.ts b/src/lib/bridge/handlers/types.ts index 85a76c9..542d6d4 100644 --- a/src/lib/bridge/handlers/types.ts +++ b/src/lib/bridge/handlers/types.ts @@ -17,4 +17,5 @@ export enum RequestType { PING_EXTENSION_STATUS = 15, PING_CANCEL_TRADE = 16, CREATE_TRADE_OFFER = 17, + FETCH_STEAM_USER = 18, }