-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathprofile_gamecovers_injected.js
More file actions
279 lines (238 loc) · 5.82 KB
/
Copy pathprofile_gamecovers_injected.js
File metadata and controls
279 lines (238 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict';
( () =>
{
const profilePagesRegex = /^https:\/\/steamcommunity\.com\/(id|profiles)\/[^/]+(\/games(\/[^/]+)?|\/home)?\/?$/;
if( !profilePagesRegex.test( window.location.href ) )
{
return;
}
// Check if "games" part
const profilePath = window.location.href.match( profilePagesRegex )[ 2 ];
const isGamesOrHome = profilePath !== undefined
&& ( profilePath.includes( 'games' ) || profilePath.includes( 'home' ) );
/** @type {HTMLScriptElement} */
const currentScript = document.querySelector( '#steamdb_profile_gamecovers' );
const fallbackCoverImage = currentScript.dataset.appLogo;
/** @type {Map<number, HTMLImageElement>} */
const appsImageStore = new Map();
/**
* @param {string} url
* @returns {number}
*/
function GetAppIDFromUrl( url )
{
const appid = url.match( /\/(?:app|sub|bundle|friendsthatplay|gamecards|recommended|widget)\/(?<id>[0-9]+)/ );
return appid ? Number.parseInt( appid.groups.id, 10 ) : -1;
}
/** @param {Record<string, any>} node */
function GetReactFiber( node )
{
const reactFiberKey = Object.keys( node ).find( key => key.startsWith( '__reactFiber' ) );
return node[ reactFiberKey ];
}
/** @param {HTMLImageElement} image */
function CheckValidImg( image )
{
if( image.complete && image.naturalWidth === 0 )
{
return false;
}
const imageSrc = image.src;
if( imageSrc === undefined || imageSrc === null )
{
return false;
}
if( imageSrc === fallbackCoverImage )
{
return true;
}
// Empty src is equal to the current location
return imageSrc !== ""
&& imageSrc !== window.location.href
// Skip fallback cover from Steam CDN
&& !imageSrc.includes( 'public/ssr' );
}
/**
* @param {number} appId
* @param {string} path
* @returns {string}
*/
function GetCoverUrl( appId, path )
{
return `https://shared.fastly.steamstatic.com/store_item_assets/steam/apps/${appId}/${path}?t=${Date.now()}`;
}
/**
* @param {HTMLImageElement} img
*/
function SetFallbackCoverImage( img )
{
img.src = fallbackCoverImage;
img.style.objectFit = 'cover';
}
/**
* @param {number} appId
* @param {HTMLImageElement} img
*/
function StoreCoverImage( appId, img )
{
SetFallbackCoverImage( img );
img.addEventListener( 'load', () =>
{
img.style.objectFit = '';
}, { once: true } );
// Handle load error
img.addEventListener( 'error', () =>
{
// Rollback
SetFallbackCoverImage( img );
appsImageStore.delete( appId );
}, { once: true } );
appsImageStore.set( appId, img );
}
function ParseProfileGameCovers()
{
const gamePictures = document.querySelectorAll( 'picture' );
for( const picture of gamePictures )
{
const fiber = GetReactFiber( picture );
if( !fiber )
{
continue;
}
const gameProps = fiber.return.memoizedProps?.game;
if( !gameProps )
{
continue;
}
const appId = gameProps.appid;
if( appsImageStore.has( appId ) )
{
continue;
}
const coverImg = picture.querySelector( 'img' );
if( !coverImg )
{
continue;
}
if( CheckValidImg( coverImg ) )
{
continue;
}
const spanPicture = picture.nextSibling;
if( spanPicture instanceof HTMLSpanElement )
{
spanPicture.style.display = 'none';
}
StoreCoverImage( appId, coverImg );
}
}
function ParseProfileCovers()
{
/** @type {NodeListOf<HTMLImageElement>} */
const gameCovers = document.querySelectorAll( 'img.game_capsule' );
for( const cover of gameCovers )
{
if( CheckValidImg( cover ) )
{
continue;
}
const parent = cover.parentElement;
if( parent instanceof HTMLAnchorElement )
{
const appId = GetAppIDFromUrl( parent.href );
if( appsImageStore.has( appId ) )
{
continue;
}
StoreCoverImage( appId, cover );
}
}
}
function ParseProfileActivityCovers()
{
/** @type {NodeListOf<HTMLImageElement>} */
const logos = document.querySelectorAll( '.blotter_gamepurchase_logo > img[src=""]' );
for( const logo of logos )
{
const parent = logo.parentElement;
if( parent instanceof HTMLAnchorElement )
{
const appId = GetAppIDFromUrl( parent.href );
if( appsImageStore.has( appId ) )
{
continue;
}
StoreCoverImage( appId, logo );
}
}
}
async function LoadGameCovers()
{
if( appsImageStore.size === 0 )
{
return;
}
const appIds = Array.from( appsImageStore.keys() ).slice( 0, 50 );
console.log( '[SteamDB]: Loading apps', appIds );
// https://api.steampowered.com/IStoreBrowseService/GetItems/v1/?input_json={"ids":[{"appid":"654310"},{"appid":"1091500"},{"appid":"730"}],"context":{"country_code":"US"},"data_request":{"include_assets":true}}
const url = new URL( 'https://api.steampowered.com/IStoreBrowseService/GetItems/v1/' );
url.searchParams.set( 'input_json', JSON.stringify( {
ids: appIds.slice( 0, 50 ).map( ( appid ) => ( { appid } ) ),
context: {
country_code: 'US',
},
data_request: {
include_assets: true,
},
} ) );
try
{
const req = await fetch( url , {
headers: {
'X-Requested-With': 'SteamDB',
},
} );
const res = await req.json();
for( const item of res.response.store_items )
{
const appImage = appsImageStore.get( item.appid );
if( !appImage )
{
continue;
}
const headerAsset = item.assets?.header;
if( headerAsset )
{
appImage.src = GetCoverUrl( item.appid, headerAsset );
}
else
{
appImage.src = GetCoverUrl( item.appid, 'header.jpg' );
}
appsImageStore.delete( item.appid );
}
}
catch( err )
{
console.error( '[SteamDB]: Failed to load app', err );
}
}
function InvokeParseCovers()
{
if( isGamesOrHome )
{
ParseProfileGameCovers();
ParseProfileActivityCovers();
}
else
{
ParseProfileCovers();
}
LoadGameCovers();
}
InvokeParseCovers();
setInterval( () =>
{
InvokeParseCovers();
}, 5_000 );
} )();