Skip to content

Commit

Permalink
Add Fetching of private apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifeismana committed Oct 17, 2024
1 parent b284d5c commit 91cedb8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
96 changes: 96 additions & 0 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ let storeSessionId;
let checkoutSessionId;
let userDataCache = null;
let userFamilyDataCache = null;
let userPrivateAppsCache = null;
let userFamilySemaphore = null;
let userPrivateAppsSemaphore = null;
let tokenSemaphore = null;
let nextAllowedRequest = 0;

Expand Down Expand Up @@ -48,6 +50,7 @@ ExtensionApi.runtime.onMessage.addListener( ( request, sender, callback ) =>
switch( request.contentScriptQuery )
{
case 'InvalidateCache': InvalidateCache(); callback(); return true;
case 'FetchPrivateApps': FetchPrivateApps( callback ); return true;
case 'FetchSteamUserData': FetchSteamUserData( callback ); return true;
case 'FetchSteamUserFamilyData': FetchSteamUserFamilyData( callback ); return true;
case 'GetApp': GetApp( request.appid, callback ); return true;
Expand Down Expand Up @@ -78,6 +81,99 @@ function InvalidateCache()
SetLocalOption( 'userfamilydata', '{}' );
}

/**
* @param {Function} callback
*/
async function FetchPrivateApps( callback )
{
if( userPrivateAppsCache !== null )
{
callback( { data: userPrivateAppsCache } );
return;
}

if( userPrivateAppsSemaphore !== null )
{
callback( await userFamilySemaphore );
return;
}

const now = Date.now();
const cacheData = await GetLocalOption( { privateappsdata: false } );
const cache = cacheData.userfamilydata && cacheData.userfamilydata;

if( cache && cache.cached && cache.data && now < cache.cached + 21600000 )
{
callback( { data: cache.data } );
return;
}

let callbackResponse = null;
let semaphoreResolve = null;
userPrivateAppsSemaphore = new Promise( resolve =>
{
semaphoreResolve = resolve;
} );

try
{
const token = await GetStoreToken();
const paramsPrivateApps = new URLSearchParams();
paramsPrivateApps.set( 'access_token', token );
const responseFetch = await fetch(
`https://api.steampowered.com/IAccountPrivateAppsService/GetPrivateAppList/v1/?${paramsPrivateApps.toString()}`,
{
headers: {
Accept: 'application/json',
}
}
);
const response = await responseFetch.json();

if( !response || !response.response || !response.response.private_apps )
{
throw new Error( 'Is Steam okay?' );
}

userPrivateAppsCache =
{
rgPrivateApps: response.response.private_apps.appids || [],
};

callbackResponse =
{
data: userPrivateAppsCache
};

callback( callbackResponse );

await SetLocalOption( 'privateappsdata', JSON.stringify( {
data: userPrivateAppsCache,
cached: now
} ) );
}
catch( error )
{
callbackResponse =
{
error: error.message,
};

if( cache && cache.data )
{
callbackResponse.data = cache.data;
}

callback( callbackResponse );
}
finally
{
semaphoreResolve( callbackResponse );
userPrivateAppsSemaphore = null;
}

};

/**
* @param {Function} callback
*/
Expand Down
22 changes: 21 additions & 1 deletion scripts/steamdb/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
}, resolve );
} );

/** @type {Promise<{data?: object, error?: string}>} */
const PrivateDataPromise = new Promise( ( resolve ) =>
{
SendMessageToBackgroundScript( {
contentScriptQuery: 'FetchPrivateApps',
}, resolve );
} );

/** @type {Promise<{data?: object, error?: string}>} */
const familyDataPromise = new Promise( ( resolve ) =>
{
Expand All @@ -103,7 +111,7 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
}, 10000 ); // 10 seconds
} );

const userData = await userDataPromise;
const [ userData, privateData ] = await Promise.all( [ userDataPromise, PrivateDataPromise ] );

// If family data does not load fast enough, assume it failed
const familyData = await Promise.race( [
Expand All @@ -116,6 +124,11 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
WriteLog( 'Failed to load userdata', userData.error );
}

if( privateData.error )
{
WriteLog( 'Failed to load privatedata', privateData.error );
}

if( familyData.error )
{
WriteLog( 'Failed to load family userdata', familyData.error );
Expand All @@ -128,6 +141,11 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
{
response = userData.data;

if( privateData.data )
{
response.rgPrivateApps = privateData.data.rgPrivateApps;
}

if( familyData.data )
{
response.rgFamilySharedApps = familyData.data.rgFamilySharedApps;
Expand Down Expand Up @@ -159,6 +177,8 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
beforeDom ? '(before dom completed)' : '',
'Packages',
response.rgOwnedPackages?.length || 0,
'Private Apps',
response.rgPrivateApps?.length || 0,
'Family Apps',
response.rgFamilySharedApps?.length || 0,
);
Expand Down

0 comments on commit 91cedb8

Please sign in to comment.