Skip to content

Commit

Permalink
wip: fetchUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
i-Moony committed Dec 15, 2023
1 parent 37b2f21 commit 1bfc5d0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/lib/inventory/fetchBasicInformation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
async function fetchBasicInformation(userCustomId:string, sessionId:string): Promise<null>
{
if (!userCustomId || !sessionId)
return null;

let response:Response;

const headers = new Headers();

headers.set("Cookie", `sessionId=${sessionId};`);

try
{
response = await fetch(`https://steamcommunity.com/id/${userCustomId}/inventory`, {headers});
}
catch (e)
{
return null;
};

const parser = new DOMParser();
const html = parser.parseFromString(await response.text(), "text/html");

const header = html.querySelector("#global_header");
const generalScript = header.nextElementSibling;
const economyScriptContainer = html.querySelector("#responsive_page_template_content");
const economyScript = economyScriptContainer.querySelector("script");

return null;
};

export default fetchBasicInformation;
22 changes: 22 additions & 0 deletions src/lib/profile/fetchCustomId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
async function fetchCustomId(userId:string): Promise<string>
{
const fetchUrl = `https://steamcommunity.com/profiles/${userId}`;

let response:Response;

try
{
response = await fetch(fetchUrl);
}
catch (e)
{
return null;
};

const profileUrl = response.url;
const splittedUrl = profileUrl.split("/").filter((part) => part.length > 0);

return splittedUrl[splittedUrl.length - 1];
};

export default fetchCustomId;
3 changes: 3 additions & 0 deletions src/lib/storage/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface SensitiveStorageData
{
sessionId: string,
userId: string,
userCustomId: string,
};

interface LocalStorageData extends StorageData, SensitiveStorageData {};
Expand All @@ -20,12 +21,14 @@ class LocalStorage
userId: null,
debug: false,
notifyOnUpdate: true,
userCustomId: null,
};

public static sensitiveKeys:Array<keyof SensitiveStorageData> =
[
"sessionId",
"userId",
'userCustomId',
];

public static async get<T>(key:keyof LocalStorageData): Promise<T | undefined>
Expand Down
19 changes: 18 additions & 1 deletion src/pages/options/routes/Inventory.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<script setup lang="ts"></script>
<script setup lang="ts">
import fetchBasicInformation from '../../../lib/inventory/fetchBasicInformation';
import fetchCustomId from "../../../lib/profile/fetchCustomId";
import { LocalStorage } from '../../../lib/storage/local';
let storage = await LocalStorage.getAll();
if (!storage.userCustomId)
{
const customId = await fetchCustomId(storage.userId);
await LocalStorage.set({userCustomId: customId});
storage = await LocalStorage.getAll();
};
await fetchBasicInformation(storage.userCustomId, storage.sessionId);
</script>

<template>
<p>Inventory!</p>
Expand Down

0 comments on commit 1bfc5d0

Please sign in to comment.