Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

Steam import: read the owned games from the All Games page itself (#222)

Mikek13c2dd640b1e
diff --git a/resources/steam-library.js b/resources/steam-library.js
index c3fec19f8a5..cd015e43646 100644
--- a/resources/steam-library.js
+++ b/resources/steam-library.js
@@ -53,6 +53,28 @@ const ownAllGames = () => validID(steamid) && location.hostname === "steamcommun
new URLSearchParams(location.search).get("tab") === "all" && profile === steamid &&
Array.isArray(window.rgGames);
if (ownAllGames()) return snapshot(window.rgGames, window.rgGames.length);
+// Steam's current All Games page carries the owner's complete list in its own
+// server-rendered data (the "OwnedGames" query). Read it when the page says it is the
+// viewer's own profile: Steam's session token is refused (401) by the owned-games API.
+function pageOwnedGames() {
+ if (reactUser?.logged_in !== true || !validID(steamid) || location.hostname !== "steamcommunity.com") return null;
+ const parse = value => { try { return typeof value === "string" ? JSON.parse(value) : value; } catch { return null; } };
+ const loaders = Array.isArray(window.SSR?.loaderData) ? window.SSR.loaderData.map(parse) : [];
+ if (!loaders.some(item => item && item.bOwnProfile === true && String(item.steamid) === steamid)) return null;
+ const queries = parse(parse(window.SSR?.renderContext)?.queryData)?.queries;
+ if (!Array.isArray(queries)) return null;
+ for (const query of queries) {
+ const key = query?.queryKey;
+ if (Array.isArray(key) && key[0] === "OwnedGames" && String(key[1]) === steamid &&
+ query.state?.status === "success" && Array.isArray(query.state.data)) return query.state.data;
+ }
+ return null;
+}
+const pageGames = pageOwnedGames();
+if (pageGames && pageGames.length) {
+ const value = snapshot(pageGames, pageGames.length);
+ if (value.state === "ready") return value;
+}
if (!reactUser && validID(steamid) && validID(profile) && !ownAllGames() && !user.webapi_token) {
return { state: "open-library" };
}
@@ -64,7 +86,7 @@ async function readJSON(url, credentials, limit) {
const response = await fetch(url, {
signal: controller.signal, redirect: "error", credentials, cache: "no-store"
});
- if (!response.ok) throw Error("request failed");
+ if (!response.ok) throw Error("http" + response.status);
const raw = await response.text();
if (raw.length > limit) throw Error("response too large");
return JSON.parse(raw);
@@ -96,8 +118,9 @@ try {
const value = payload.response;
if (!value || !Object.prototype.hasOwnProperty.call(value, "game_count")) return { state: "unavailable", why: "private" };
return snapshot(value.games || [], value.game_count);
-} catch {
- return { state: "unavailable", why: "request" };
+} catch (error) {
+ // Only Steam's status code leaves this page, never a URL, token or message.
+ return { state: "unavailable", why: /^http[0-9]{3}$/.test(error?.message) ? error.message : "request" };
} finally {
clearTimeout(timer);
}
diff --git a/tests/test_steam_login.cjs b/tests/test_steam_login.cjs
index a8ff7a94c2d..a850e1a2a50 100644
--- a/tests/test_steam_login.cjs
+++ b/tests/test_steam_login.cjs
@@ -162,3 +162,33 @@ test("React viewer must match every other identity on the page", async () => {
({ok:true,text:async()=>JSON.stringify({logged_in:true,steamid:"76561198000000001",token:"other"})}));
assert.equal(result.state, "unavailable");
});
+
+function ownGamesPage(games, own = true, owner = id) {
+ const queryData = JSON.stringify({queries:[
+ {queryKey:["OwnedGames", "76561198000000009", "english"], state:{status:"success", data:[{appid:1, name:"Someone else's"}]}},
+ {queryKey:["OwnedGames", owner, "english"], state:{status:"success", data:games}}]});
+ const page = reactPage({logged_in:true, steamid:id}, [JSON.stringify({steamid:id}), JSON.stringify({steamid:owner, bOwnProfile:own})]);
+ page[0].SSR.renderContext = {queryData};
+ return page;
+}
+test("the current All Games page gives the list from its own data, with no request", async () => {
+ const games = [{appid:730, name:"Counter-Strike 2", playtime_forever:2220}, {appid:1097150, name:"Fall Guys"}, {appid:622590}];
+ const result = await run(...ownGamesPage(games), () => { throw Error("must not fetch"); });
+ assert.deepEqual(result, {state:"ready", steamid:id, game_count:2,
+ games:[{appid:"730", name:"Counter-Strike 2"}, {appid:"1097150", name:"Fall Guys"}]});
+});
+test("another profile's page data is never imported", async () => {
+ const refused = async url => url === "https://steamcommunity.com/chat/clientjstoken"
+ ? {ok:true, text:async()=>JSON.stringify({logged_in:true, steamid:id, token:"session-only"})}
+ : {ok:false, status:401, text:async()=>"<html>"};
+ for (const page of [ownGamesPage([{appid:10, name:"Not mine"}], false), ownGamesPage([{appid:10, name:"Not mine"}], true, "76561198000000001")]) {
+ const result = await run(...page, refused);
+ assert.deepEqual(result, {state:"unavailable", why:"http401"});
+ }
+});
+test("an empty page list falls back to Steam's API", async () => {
+ const result = await run(...ownGamesPage([]), async url => url === "https://steamcommunity.com/chat/clientjstoken"
+ ? {ok:true, text:async()=>JSON.stringify({logged_in:true, steamid:id, token:"t"})}
+ : {ok:true, text:async()=>JSON.stringify({response:{game_count:1, games:[{appid:10, name:"Owned"}]}})});
+ assert.equal(result.state, "ready"); assert.equal(result.games.length, 1);
+});