Repository navigation
Expand file tree
/
Copy pathhumble_game_store_enhancer.user.js
More file actions
362 lines (322 loc) · 13.8 KB
/
Copy pathhumble_game_store_enhancer.user.js
File metadata and controls
362 lines (322 loc) · 13.8 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// ==UserScript==
// @name Humble Bundle Game Store Enhancer
// @namespace https://github.com/gbzret4d/game-store-enhancer
// @version 0.3.1
// @description Humble Bundle Steam Integration with robust status checks, review scores, and overlay fixes.
// @author gbzret4d
// @updateURL https://raw.githubusercontent.com/gbzret4d/game-store-enhancer/main/humble_game_store_enhancer.user.js
// @downloadURL https://raw.githubusercontent.com/gbzret4d/game-store-enhancer/main/humble_game_store_enhancer.user.js
// @match https://www.humblebundle.com/*
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @grant unsafeWindow
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
// --- Configuration ---
const LOG_PREFIX = '[GSE Humble]';
const CACHE_URL = 'https://raw.githubusercontent.com/gbzret4d/game-store-enhancer/main/data/steam_apps.min.json';
// Broadened selectors to catch more elements (Legacy + Modern)
const TILE_SELECTOR = [
'.entity-block-container',
'.entity-Link',
'.browse-product-grid-item',
'.tier-item-view',
'.content-choice',
'.game-box',
'.product-item',
'.entity-link',
'.mosaic-tile',
// Legacy v0.1 Selectors (Restored)
'.entity',
'.game-tile',
'.item-details',
'.full-tile-view',
// Broad Matchers
'[class*="entity-block-container"]',
'[class*="product-item"]',
'div[class*="entity-container"]'
].join(', ');
// --- State ---
const state = {
steamApps: new Map(), // name -> appid
userData: { owned: new Set(), wishlist: new Set(), ignored: new Set() },
processed: new WeakSet(),
reviewCache: GM_getValue('review_cache', {})
};
// --- Styles ---
document.documentElement.dataset.gseInstalled = "true";
document.documentElement.classList.add('gse-installed');
try { if (typeof unsafeWindow !== 'undefined') unsafeWindow.gseInstalled = true; } catch (e) { }
GM_addStyle(`
.hbsi-badge {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 5px;
background-color: #171a21;
padding: 4px 6px;
border-radius: 4px;
border: 1px solid #3c3d3e;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
font-family: "Motiva Sans", "Arial", sans-serif;
font-size: 11px;
font-weight: bold;
color: #c7d5e0;
text-decoration: none !important;
line-height: 1;
z-index: 2147483647 !important;
cursor: pointer;
pointer-events: auto !important;
transition: all 0.2s ease;
white-space: nowrap;
height: auto !important;
width: auto !important;
box-sizing: border-box;
align-self: flex-start;
flex-shrink: 0;
flex-grow: 0;
}
.hbsi-badge:hover {
border-color: #66c0f4;
color: #ffffff;
transform: translateY(-1px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.6);
}
.hbsi-badge svg {
width: 14px;
height: 14px;
fill: currentColor;
}
.hbsi-status-owned { color: #a4d007 !important; border-color: #4c6b22 !important; }
.hbsi-status-wishlist { color: #66c0f4 !important; border-color: #2e4d6d !important; }
.hbsi-status-ignored { color: #d9534f !important; border-color: #6d2e2e !important; }
.hbsi-review-positive { color: #66c0f4; }
.hbsi-review-mixed { color: #a8926a; }
.hbsi-review-negative { color: #d9534f; }
.hbsi-review-score {
font-size: 10px;
opacity: 0.9;
margin-left: 2px;
}
.hbsi-tile-owned { box-shadow: inset 0 0 0 3px #5cb85c !important; }
.hbsi-tile-wishlist { box-shadow: inset 0 0 0 3px #5bc0de !important; }
.hbsi-tile-ignored {
box-shadow: inset 0 0 0 3px #d9534f !important;
opacity: 0.6;
filter: grayscale(80%);
}
.hbsi-pos-abs {
position: absolute !important;
top: 6px !important;
left: 6px !important;
bottom: auto !important;
right: auto !important;
}
`);
// --- Helpers ---
function normalize(str) {
if (typeof str !== 'string') return '';
return str.toLowerCase().replace(/[^a-z0-9]/g, '');
}
// --- API Fetchers ---
// 1. User Data (Owned/Wishlist)
async function fetchUserData() {
return new Promise(resolve => {
console.log(LOG_PREFIX, "Fetching UserData...");
GM_xmlhttpRequest({
method: "GET",
url: "https://store.steampowered.com/dynamicstore/userdata/",
onload: (res) => {
try {
const data = JSON.parse(res.responseText);
state.userData = {
owned: new Set(data.rgOwnedPackages || []),
wishlist: new Set(data.rgWishlist || []),
ignored: new Set(Object.keys(data.rgIgnoredApps || {}))
};
console.log(LOG_PREFIX, "UserData loaded:", state.userData.owned.size, "owned items");
resolve(state.userData);
} catch (e) {
console.error(LOG_PREFIX, "Failed to parse UserData", e);
resolve(state.userData); // Return empty on fail
}
},
onerror: () => resolve(state.userData)
});
});
}
// 2. App Cache (Source of Truth)
async function fetchAppCache() {
return new Promise((resolve) => {
console.log(LOG_PREFIX, "Fetching AppCache from", CACHE_URL);
GM_xmlhttpRequest({
method: "GET",
url: CACHE_URL,
onload: function (res) {
try {
const data = JSON.parse(res.responseText);
// Data format: { "normalized_name": app_id } -> Load directly
for (const [name, appid] of Object.entries(data)) {
state.steamApps.set(name, appid);
}
state.cacheLoaded = true;
console.log(LOG_PREFIX, `Cache loaded: ${state.steamApps.size} apps`);
} catch (e) {
console.error(LOG_PREFIX, 'Failed to parse AppCache', e);
}
resolve();
},
onerror: function (e) {
console.error(LOG_PREFIX, 'Failed to fetch AppCache', e);
resolve();
}
});
});
}
// 3. Review Score (Steam Store API)
// We use a simple cache to avoid spamming calls for the same game in grid views
async function fetchReviewScore(appid) {
if (state.reviewCache[appid]) return state.reviewCache[appid];
return new Promise(resolve => {
GM_xmlhttpRequest({
method: "GET",
url: `https://store.steampowered.com/apphoverpublic/${appid}?l=english&json=1`,
onload: (res) => {
try {
const data = JSON.parse(res.responseText);
if (data && data.strReviewSummary) {
// Extract simple status (e.g. "Very Positive")
// Format is usually <span class="...">Very Positive</span>
const match = data.strReviewSummary.match(/>([^<]+)</);
const score = match ? match[1] : "?";
// Determine class based on score text
let scoreClass = 'hbsi-review-mixed';
if (/positive/i.test(score)) scoreClass = 'hbsi-review-positive';
if (/negative/i.test(score)) scoreClass = 'hbsi-review-negative';
const result = { score, scoreClass };
state.reviewCache[appid] = result;
GM_setValue('review_cache', state.reviewCache);
resolve(result);
} else {
resolve(null);
}
} catch (e) {
resolve(null);
}
},
onerror: () => resolve(null)
});
});
}
// --- Core Logic ---
async function processTile(tile) {
if (state.processed.has(tile)) return;
state.processed.add(tile);
// 1. Find Title
let titleEl = tile.querySelector(
'.item-title, .entity-title, .product-title, .content-choice-title, .game-box-title, h2, h3, h4, [class*="title"]'
);
// Fallback for Store Grid
if (!titleEl && tile.classList.contains('browse-product-grid-item')) {
titleEl = tile.querySelector('.entity-title');
}
if (!titleEl) return;
const gameName = titleEl.textContent.trim();
const normName = normalize(gameName);
// 2. Resolve AppID
const appid = state.steamApps.get(normName);
if (!appid) {
// Only log if desperate debugging needed
// console.log(LOG_PREFIX, "No AppID found for:", gameName);
return;
}
// 3. Check Status
// Safety check: userData might be partial if fetch failed
const isOwned = state.userData.owned ? state.userData.owned.has(parseInt(appid)) : false;
const isWishlist = state.userData.wishlist ? state.userData.wishlist.has(parseInt(appid)) : false;
const isIgnored = state.userData.ignored ? state.userData.ignored.has(appid) : false;
// 4. Create Badge
createBadge(tile, appid, isOwned, isWishlist, isIgnored);
// 5. Visual Feedback on Tile (Border/Opacity)
if (isOwned) tile.classList.add('hbsi-tile-owned');
if (isWishlist) tile.classList.add('hbsi-tile-wishlist');
if (isIgnored) tile.classList.add('hbsi-tile-ignored');
}
async function createBadge(tile, appid, isOwned, isWishlist, isIgnored) {
// Validation: Don't double badge
if (tile.querySelector('.hbsi-badge')) return;
const badge = document.createElement('a');
badge.className = 'hbsi-badge hbsi-pos-abs';
badge.href = `https://store.steampowered.com/app/${appid}`;
badge.target = '_blank';
badge.title = 'View on Steam';
// Icon
badge.innerHTML = `<svg viewBox="0 0 24 24"><path d="M12 0C5.373 0 0 5.373 0 12c0 3.167 1.22 6.046 3.235 8.197L2.4 24l3.803-.835A11.95 11.95 0 0 0 12 24c6.627 0 12-5.373 12-12S18.627 0 12 0zm5.65 16.5c-.3 0-.585-.11-.795-.315l-3.26-3.15c-.445-.43-.46-1.125-.03-1.555.42-.42 1.105-.415 1.54.02l2.365 2.29 4.96-5.73c.4-4.63.495-1.155-.065-1.635-.455-.38-1.135-.33-1.57.17l-5.61 6.48 c-.215.25-.525.395-.855.395z"/></svg>`;
// Status Colors
if (isOwned) {
badge.classList.add('hbsi-status-owned');
badge.title = 'Owned on Steam';
} else if (isWishlist) {
badge.classList.add('hbsi-status-wishlist');
badge.title = 'On Steam Wishlist';
} else if (isIgnored) {
badge.classList.add('hbsi-status-ignored');
badge.title = 'Ignored on Steam';
}
// Append to suitable container
// Note: 'hbsi-pos-abs' positions it absolute top-left.
const style = window.getComputedStyle(tile);
if (style.position === 'static') {
tile.style.position = 'relative';
}
tile.appendChild(badge);
// Fetch Review Score (Async)
const reviewData = await fetchReviewScore(appid);
if (reviewData) {
const scoreSpan = document.createElement('span');
scoreSpan.className = `hbsi-review-score ${reviewData.scoreClass}`;
scoreSpan.textContent = reviewData.score;
badge.appendChild(scoreSpan);
}
}
// --- Initialization ---
async function main() {
console.log(LOG_PREFIX, "v0.3.0 Init...");
const [userData, _] = await Promise.all([
fetchUserData(),
fetchAppCache()
]);
state.userData = userData;
// Observer
const observer = new MutationObserver((mutations) => {
const tiles = document.querySelectorAll(TILE_SELECTOR);
if (tiles.length > 0) tiles.forEach(processTile);
});
observer.observe(document.body, { childList: true, subtree: true });
// Polling Retry Logic (up to 5 seconds)
let attempts = 0;
const interval = setInterval(() => {
const tiles = document.querySelectorAll(TILE_SELECTOR);
console.log(LOG_PREFIX, `Scan #${attempts + 1}: found ${tiles.length} tiles`);
if (tiles.length > 0) {
tiles.forEach(processTile);
// Don't clear interval, keep polling for lazy loaded stuff just in case
}
attempts++;
if (attempts >= 5) {
clearInterval(interval);
if (tiles.length === 0) {
console.warn(LOG_PREFIX, "Still 0 tiles after 5s. Dumping DOM sample:");
const sample = Array.from(document.querySelectorAll('div')).slice(0, 20).map(d => d.className).join(' | ');
console.log(LOG_PREFIX, "Div classes found:", sample);
}
}
}, 1000);
}
// Start
main();
})();