// ==UserScript== // @name Inventory.gift // @namespace https://inventory.gift/ // @version 1.0.2 // @description Add Inventory.gift names, owner counts, and SubIDs to Steam gift inventories. // @homepageURL https://www.inventory.gift/ // @supportURL https://github.com/stackedgamer/inventory-gift-userscript/issues // @downloadURL https://raw.githubusercontent.com/stackedgamer/inventory-gift-userscript/main/dist/inventory-gift.user.js // @updateURL https://raw.githubusercontent.com/stackedgamer/inventory-gift-userscript/main/dist/inventory-gift.user.js // @license MIT // @match https://steamcommunity.com/id/*/inventory* // @match https://steamcommunity.com/profiles/*/inventory* // @connect www.inventory.gift // @connect inventory.gift // @grant GM_addStyle // @grant GM_deleteValue // @grant GM_getValue // @grant GM_openInTab // @grant GM_setValue // @grant GM_xmlhttpRequest // @grant unsafeWindow // @run-at document-start // ==/UserScript== // Generated from src/. Do not edit this file directly. (() => { "use strict"; const decimalIdPattern = /^[0-9]+$/; const canonicalSlugPattern = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; const opaqueTokenPattern = /^[A-Za-z0-9_-]{43}$/; function isDecimalId(value) { return typeof value === "string" && decimalIdPattern.test(value); } function classifySteamRequestUrl(value, baseUrl = window.location.href) { try { const url = new URL(value, baseUrl); if (url.origin !== "https://steamcommunity.com") return null; const inventory = /^\/inventory\/([0-9]{17})\/753\/1\/?$/.exec( url.pathname, ); if (inventory !== null) { return { kind: "inventory", steamId64: inventory[1] }; } const validation = /^\/gifts\/([0-9]+)\/validateunpack\/?$/.exec( url.pathname, ); return validation === null ? null : { assetId: validation[1], kind: "validate" }; } catch { return null; } } function isOwnGiftInventory(viewedSteamId64, accountSteamId64) { return ( typeof viewedSteamId64 === "string" && /^[0-9]{17}$/.test(viewedSteamId64) && viewedSteamId64 === accountSteamId64 ); } function sanitizeInventoryAssets(value) { if ( value === null || typeof value !== "object" || !Array.isArray(value.assets) ) { return []; } const assets = []; for (const candidate of value.assets) { if ( candidate !== null && typeof candidate === "object" && String(candidate.appid) === "753" && String(candidate.contextid) === "1" && isDecimalId(candidate.assetid) && isDecimalId(candidate.classid) ) { assets.push({ assetId: candidate.assetid, classId: candidate.classid }); } } return assets; } function normalizeDisplayName(value) { return value .normalize("NFKC") .trim() .replace(/\s+/g, " ") .toLocaleLowerCase("en-US"); } function isUnknownPackageDisplayName(value) { return normalizeDisplayName(value).includes("unknown package"); } function needsPackageObservation(gift) { return ( gift === null || gift.subId === null || isUnknownPackageDisplayName(gift.displayName) ); } function needsBulkPackageObservation(gift) { return gift !== null && gift.subId === null; } function shouldValidateSelectedGift({ collectionRunning, gift, hasLocalObservation, ownInventory, }) { return ( ownInventory && !collectionRunning && gift !== undefined && !hasLocalObservation && needsPackageObservation(gift) ); } function selectInventoryGiftDisplayName(backendName, localName) { if ( typeof localName === "string" && localName.length > 0 && (backendName === null || isUnknownPackageDisplayName(backendName)) ) { return localName; } return backendName; } function parseAssetIdFromElementId(value) { const match = /^753_1_([0-9]+)$/.exec(value); return match?.[1] ?? null; } function parseLookupResponse(value) { if ( value === null || typeof value !== "object" || typeof value.accountSteamId64 !== "string" || !/^[0-9]{17}$/.test(value.accountSteamId64) || !Array.isArray(value.gifts) ) { throw new TypeError("Invalid Inventory.gift lookup response"); } const gifts = []; const seenClassIds = new Set(); for (const candidate of value.gifts) { if ( candidate === null || typeof candidate !== "object" || !isDecimalId(candidate.classId) || typeof candidate.displayName !== "string" || candidate.displayName.trim().length === 0 || typeof candidate.knownOwnerCount !== "string" || !decimalIdPattern.test(candidate.knownOwnerCount) || typeof candidate.slug !== "string" || !canonicalSlugPattern.test(candidate.slug) || !( candidate.subId === null || (Number.isSafeInteger(candidate.subId) && candidate.subId > 0) ) || seenClassIds.has(candidate.classId) ) { throw new TypeError("Invalid Inventory.gift lookup gift"); } seenClassIds.add(candidate.classId); gifts.push({ classId: candidate.classId, displayName: candidate.displayName.trim(), knownOwnerCount: candidate.knownOwnerCount, slug: candidate.slug, subId: candidate.subId, }); } return { accountSteamId64: value.accountSteamId64, gifts }; } function parseValidateUnpackResponse(value) { if ( value === null || typeof value !== "object" || !(value.success === 1 || value.success === true) || typeof value.message !== "string" || typeof value.owned !== "boolean" || typeof value.gift_name !== "string" ) { throw new TypeError("Invalid Steam validateunpack response"); } const trimmedGiftName = value.gift_name.trim(); const parsedSubId = typeof value.packageid === "number" ? value.packageid : typeof value.packageid === "string" && decimalIdPattern.test(value.packageid) ? Number(value.packageid) : Number.NaN; if ( trimmedGiftName.length > 300 || !Number.isSafeInteger(parsedSubId) || parsedSubId <= 0 || parsedSubId > 2_147_483_647 ) { throw new TypeError("Invalid Steam validateunpack response"); } return { giftName: trimmedGiftName.length === 0 ? null : trimmedGiftName, subId: parsedSubId, }; } function parseObservationResponse(value, submittedCount) { if ( value === null || typeof value !== "object" || !Number.isSafeInteger(value.acceptedCount) || value.acceptedCount < 0 || value.acceptedCount > submittedCount ) { throw new TypeError("Invalid Inventory.gift observation response"); } return value.acceptedCount; } function parsePairingCreateResponse(value) { if ( value === null || typeof value !== "object" || typeof value.authorizationUrl !== "string" || !URL.canParse(value.authorizationUrl) || typeof value.expiresAt !== "string" || !Number.isFinite(Date.parse(value.expiresAt)) || typeof value.pairingId !== "string" || !opaqueTokenPattern.test(value.pairingId) || typeof value.pollingSecret !== "string" || !opaqueTokenPattern.test(value.pollingSecret) ) { throw new TypeError("Invalid Inventory.gift pairing response"); } return value; } function parsePairingPollResponse(value) { if (value?.status === "pending") return { status: "pending" }; if ( value?.status === "approved" && typeof value.credential === "string" && opaqueTokenPattern.test(value.credential) ) { return { credential: value.credential, status: "approved" }; } throw new TypeError("Invalid Inventory.gift pairing poll response"); } function isUserscriptCredential(value) { return typeof value === "string" && opaqueTokenPattern.test(value); } function classifyInventoryGiftStatus(status) { return { localOnly: status === 409, rateLimited: status === 429, reconnect: status === 401 || status === 403, }; } function responseJson(transport) { if (transport.responseType === "json" && transport.response !== null) { return transport.response; } if (transport.responseJSON !== undefined) return transport.responseJSON; return JSON.parse(String(transport.responseText)); } function installSteamCaptureBridge(pageWindow, eventNames) { const handledTransports = new WeakSet(); const xhrRequestKey = Symbol("inventoryGiftRequest"); const dispatch = (eventName, detail) => { try { pageWindow.dispatchEvent( new pageWindow.CustomEvent(eventName, { detail }), ); } catch (error) { pageWindow.console?.warn?.( "[Inventory.gift] Could not deliver a Steam capture event", error, ); } }; const reportFailure = (request, message, status = null) => { dispatch(eventNames.failure, { assetId: request.kind === "validate" ? request.assetId : null, kind: request.kind, message, status, }); }; const reportRequestStart = (request) => { if (request.kind === "validate") { dispatch(eventNames.validateStart, { assetId: request.assetId }); } }; const reportRequestEnd = (request) => { if (request.kind === "validate") { dispatch(eventNames.validateEnd, { assetId: request.assetId }); } }; const emitResponse = (request, value) => { if (request.kind === "inventory") { if ( value !== null && typeof value === "object" && Array.isArray(value.assets) ) { dispatch(eventNames.inventory, { assets: value.assets.map((candidate) => ({ appid: candidate?.appid, assetid: candidate?.assetid, classid: candidate?.classid, contextid: candidate?.contextid, })), steamId64: request.steamId64, }); } else { reportFailure( request, "Steam returned an invalid gift inventory response", ); } return; } dispatch(eventNames.validate, { assetId: request.assetId, response: value, }); }; const captureTransport = (request, transport) => { if (handledTransports.has(transport)) return; handledTransports.add(transport); const status = Number(transport.status); if (!Number.isFinite(status) || status < 200 || status >= 300) { reportFailure( request, "Steam request failed", Number.isFinite(status) ? status : null, ); return; } try { emitResponse(request, responseJson(transport)); } catch { reportFailure(request, "Steam returned unreadable JSON"); } }; const originalFetch = pageWindow.fetch; pageWindow.fetch = function inventoryGiftFetch(input) { const result = originalFetch.apply(this, arguments); const requestUrl = input instanceof pageWindow.Request ? input.url : input; const request = classifySteamRequestUrl( String(requestUrl), pageWindow.location.href, ); if (request !== null) { reportRequestStart(request); void result .then(async (response) => { if (!response.ok) { reportFailure(request, "Steam request failed", response.status); return; } emitResponse(request, await response.clone().json()); }) .catch((error) => { if (error?.name === "AbortError") { reportRequestEnd(request); } else { reportFailure(request, "Steam returned unreadable JSON"); } }); } return result; }; const originalOpen = pageWindow.XMLHttpRequest.prototype.open; pageWindow.XMLHttpRequest.prototype.open = function inventoryGiftOpen( method, url, ) { const result = originalOpen.apply(this, arguments); this[xhrRequestKey] = classifySteamRequestUrl( String(url), pageWindow.location.href, ); return result; }; const originalSend = pageWindow.XMLHttpRequest.prototype.send; pageWindow.XMLHttpRequest.prototype.send = function inventoryGiftSend() { const request = this[xhrRequestKey]; if (request !== null && request !== undefined) { reportRequestStart(request); this.addEventListener("load", () => captureTransport(request, this), { once: true, }); this.addEventListener( "abort", () => { handledTransports.add(this); reportRequestEnd(request); }, { once: true }, ); } return originalSend.apply(this, arguments); }; let prototypeResponderInstalled = false; const installPrototypeResponder = () => { if (prototypeResponderInstalled) return; const responders = pageWindow.Ajax?.Responders; if (typeof responders?.register !== "function") return; responders.register({ onCreate(request) { const capturedRequest = classifySteamRequestUrl( String(request?.url), pageWindow.location.href, ); if (capturedRequest !== null) reportRequestStart(capturedRequest); }, onComplete(request, transport) { const capturedRequest = classifySteamRequestUrl( String(request?.url), pageWindow.location.href, ); if (capturedRequest !== null) { captureTransport(capturedRequest, transport); } }, }); prototypeResponderInstalled = true; }; installPrototypeResponder(); pageWindow.document.addEventListener( "DOMContentLoaded", installPrototypeResponder, { once: true }, ); } const apiBaseUrl = "https://www.inventory.gift"; const siteBaseUrl = "https://www.inventory.gift"; const lookupBatchSize = 100; const lookupDebounceMs = 150; const validateUnpackDelayMs = 500; const selectedGiftValidationDelayMs = 100; const observationCheckpointSize = 20; const credentialStorageKey = "inventory-gift:credential"; const captureEvents = Object.freeze({ failure: `inventory-gift:failure:${crypto.randomUUID()}`, inventory: `inventory-gift:assets:${crypto.randomUUID()}`, validate: `inventory-gift:validate:${crypto.randomUUID()}`, validateEnd: `inventory-gift:validate-end:${crypto.randomUUID()}`, validateStart: `inventory-gift:validate-start:${crypto.randomUUID()}`, }); const steamFetch = unsafeWindow.fetch.bind(unsafeWindow); const assetClassIds = new Map(); const capturedValidateResponses = new Map(); const giftLookups = new Map(); const localObservations = new Map(); const pendingClassIds = new Set(); const pendingObservedClassIds = new Set(); const submittedClassIds = new Set(); const submittingObservedClassIds = new Set(); const unreportableClassIds = new Set(); const externalValidateRequests = new Set(); let credential = GM_getValue(credentialStorageKey, null); if (!isUserscriptCredential(credential)) { credential = null; GM_deleteValue(credentialStorageKey); } let inventorySteamId64 = null; let connectedSteamId64 = null; let lookupTimer = null; let lookupInFlight = false; let lastLookupError = null; let lastCaptureError = null; let selectedValidationAbortController = null; let selectedValidationAssetId = null; let selectedValidationAttempted = false; let selectedValidationTimer = null; let collectionCancelled = false; let connectionState = { kind: credential === null ? "disconnected" : "connected", message: null, }; let collectionState = { completed: 0, kind: "idle", message: null, total: 0 }; class InventoryGiftRequestError extends Error { constructor(message, status, code, retryAfter) { super(message); this.code = code; this.retryAfter = retryAfter; this.status = status; } } function resetSelectedGiftValidation() { clearTimeout(selectedValidationTimer); selectedValidationTimer = null; selectedValidationAbortController?.abort(); selectedValidationAbortController = null; selectedValidationAssetId = null; selectedValidationAttempted = false; } function clearCredential(message) { credential = null; connectedSteamId64 = null; GM_deleteValue(credentialStorageKey); connectionState = { kind: "disconnected", message }; collectionCancelled = true; resetSelectedGiftValidation(); capturedValidateResponses.clear(); externalValidateRequests.clear(); giftLookups.clear(); localObservations.clear(); pendingClassIds.clear(); pendingObservedClassIds.clear(); submittedClassIds.clear(); unreportableClassIds.clear(); lastCaptureError = null; lastLookupError = null; } function requestJson(url, body, options = {}) { return new Promise((resolve, reject) => { const headers = { "Content-Type": "application/json" }; if (options.authenticated !== false && credential !== null) { headers.Authorization = `Bearer ${credential}`; } GM_xmlhttpRequest({ data: body === undefined ? undefined : JSON.stringify(body), headers, method: options.method ?? "POST", onerror: () => reject(new Error("Inventory.gift request failed")), onload: (response) => { if (response.status < 200 || response.status >= 300) { let code = null; try { code = JSON.parse(response.responseText)?.error?.code ?? null; } catch {} const retryAfter = Number( response.responseHeaders?.match(/retry-after:\s*([0-9]+)/i)?.[1], ); const failure = classifyInventoryGiftStatus(response.status); if (options.authenticated !== false && failure.reconnect) { clearCredential( "Connection expired or access was disabled. Reconnect to continue.", ); } const message = failure.rateLimited ? `rate limited${Number.isFinite(retryAfter) ? `; retry in ${retryAfter}s` : ""}` : `request returned ${response.status}`; reject( new InventoryGiftRequestError( `Inventory.gift ${message}`, response.status, code, retryAfter, ), ); return; } try { resolve(JSON.parse(response.responseText)); } catch { reject(new Error("Inventory.gift returned invalid JSON")); } }, ontimeout: () => reject(new Error("Inventory.gift request timed out")), timeout: 15_000, url, }); }); } async function connectUserscript() { if (connectionState.kind === "connecting") return; connectionState = { kind: "connecting", message: "Waiting for approval…" }; renderAll(); try { const created = parsePairingCreateResponse( await requestJson( `${apiBaseUrl}/api/userscript/v1/pairings`, {}, { authenticated: false }, ), ); GM_openInTab(created.authorizationUrl, { active: true, insert: true }); const expiresAt = Date.parse(created.expiresAt); while (Date.now() < expiresAt) { await delay(2_000); const polled = parsePairingPollResponse( await requestJson( `${apiBaseUrl}/api/userscript/v1/pairings/poll`, { pairingId: created.pairingId, pollingSecret: created.pollingSecret, }, { authenticated: false }, ), ); if (polled.status === "pending") continue; credential = polled.credential; GM_setValue(credentialStorageKey, credential); connectionState = { kind: "connected", message: "Connected" }; new Set(assetClassIds.values()).forEach(scheduleLookup); renderAll(); return; } throw new Error("The connection request expired"); } catch (error) { connectionState = { kind: "disconnected", message: error instanceof Error ? error.message : "Connection failed", }; renderAll(); } } async function disconnectUserscript() { if (credential === null) return; try { await requestJson(`${apiBaseUrl}/api/userscript/v1/credential`, undefined, { method: "DELETE", }); clearCredential("Disconnected"); } catch (error) { if (credential !== null) { connectionState = { kind: "connected", message: error instanceof Error ? error.message : "Could not disconnect", }; } } renderAll(); } function scheduleLookup(classId) { if (credential === null) return; if (giftLookups.has(classId)) return; pendingClassIds.add(classId); clearTimeout(lookupTimer); lookupTimer = setTimeout(() => void flushLookups(), lookupDebounceMs); } async function flushLookups() { if (lookupInFlight || pendingClassIds.size === 0) return; lookupInFlight = true; const batch = [...pendingClassIds].slice(0, lookupBatchSize); batch.forEach((classId) => pendingClassIds.delete(classId)); try { const response = await requestJson( `${apiBaseUrl}/api/userscript/v1/gifts/lookup`, { classIds: batch, }, ); const lookup = parseLookupResponse(response); const gifts = lookup.gifts; connectedSteamId64 = lookup.accountSteamId64; const returned = new Set(gifts.map((gift) => gift.classId)); gifts.forEach((gift) => giftLookups.set(gift.classId, gift)); batch.forEach((classId) => { if (!returned.has(classId)) giftLookups.set(classId, null); }); lastLookupError = null; flushCapturedValidateResponses(); } catch (error) { batch.forEach((classId) => pendingClassIds.add(classId)); lastLookupError = error instanceof Error ? error.message : "Inventory.gift lookup failed"; } finally { lookupInFlight = false; renderAll(); for (const classId of pendingObservedClassIds) { void maybeSubmitObservedValidate(classId); } if (pendingClassIds.size > 0 && lastLookupError === null) { lookupTimer = setTimeout(() => void flushLookups(), lookupDebounceMs); } } } function receiveAssets(event) { if ( event.detail === null || typeof event.detail !== "object" || !isDecimalId(event.detail.steamId64) ) { return; } const assets = sanitizeInventoryAssets({ assets: event.detail.assets }); inventorySteamId64 = event.detail.steamId64; for (const asset of assets) { assetClassIds.set(asset.assetId, asset.classId); scheduleLookup(asset.classId); const observation = capturedValidateResponses.get(asset.assetId); if (observation !== undefined) { capturedValidateResponses.delete(asset.assetId); receiveObservedValidate(asset.assetId, observation); } } renderAll(); } function activeAssetId() { const active = document.querySelector(".item.app753.context1.activeInfo[id]"); return active instanceof HTMLElement ? parseAssetIdFromElementId(active.id) : null; } function createEnhancement(gift, localObservation, steamName) { const block = document.createElement("section"); block.className = "inventory-gift-detail"; block.dataset.inventoryGiftDetail = ""; const label = document.createElement("strong"); label.textContent = "Inventory.gift"; block.append(label); const backendDisplayName = gift?.displayName ?? null; const localGiftName = localObservation?.giftName ?? null; const displayName = selectInventoryGiftDisplayName( backendDisplayName, localGiftName, ); if ( displayName !== null && normalizeDisplayName(displayName) !== normalizeDisplayName(steamName) ) { const name = document.createElement("span"); name.textContent = `${displayName}`; block.append(name); } if (gift !== null) { const owners = document.createElement("a"); owners.href = new URL(`/${gift.slug}`, siteBaseUrl).href; owners.rel = "noopener noreferrer"; owners.target = "_blank"; owners.textContent = `${BigInt(gift.knownOwnerCount).toLocaleString()} known ${ gift.knownOwnerCount === "1" ? "owner" : "owners" }`; block.append(owners); } const effectiveSubId = gift?.subId ?? localObservation?.subId ?? null; if (effectiveSubId === null) { const subId = document.createElement("span"); subId.textContent = "SubID unknown"; block.append(subId); } else { const link = document.createElement("a"); link.href = `https://steamdb.info/sub/${effectiveSubId}/`; link.rel = "noopener noreferrer"; link.target = "_blank"; link.textContent = `SubID ${effectiveSubId}`; block.append(link); } return block; } function renderDetails() { const assetId = activeAssetId(); const classId = assetId === null ? null : assetClassIds.get(assetId); const gift = classId === null || classId === undefined ? undefined : giftLookups.get(classId); for (const root of document.querySelectorAll( '[data-featuretarget="iteminfo"]', )) { const heading = root.querySelector("h1"); const existing = root.querySelector("[data-inventory-gift-detail]"); if ( credential === null || !(heading instanceof HTMLElement) || gift === undefined ) { existing?.remove(); continue; } const expectedClassId = classId; const localObservation = localObservations.get(expectedClassId) ?? null; const renderSignature = JSON.stringify({ gift, localObservation }); if ( existing?.getAttribute("data-class-id") === expectedClassId && existing.getAttribute("data-render-signature") === renderSignature ) { continue; } existing?.remove(); const enhancement = createEnhancement( gift, localObservation, heading.textContent ?? "", ); enhancement.dataset.classId = expectedClassId; enhancement.dataset.renderSignature = renderSignature; heading.insertAdjacentElement("afterend", enhancement); } } function renderStatus() { const anchor = document.querySelector("#context_selector"); if (!(anchor instanceof HTMLElement)) return; let status = document.querySelector("[data-inventory-gift-status]"); if (!(status instanceof HTMLElement)) { status = document.createElement("div"); status.className = "inventory-gift-status"; status.dataset.inventoryGiftStatus = ""; anchor.insertAdjacentElement("afterend", status); } if (credential === null) { const signature = JSON.stringify(connectionState); if (status.dataset.renderSignature === signature) return; status.dataset.renderSignature = signature; status.replaceChildren(); const text = document.createElement("span"); text.textContent = connectionState.message === null ? "Inventory.gift: Connect to Inventory.gift to enable enrichment" : `Inventory.gift: ${connectionState.message}`; status.append(text); const button = document.createElement("button"); button.className = "inventory-gift-collect"; button.type = "button"; button.disabled = connectionState.kind === "connecting"; button.textContent = connectionState.kind === "connecting" ? "Connecting…" : "Connect to Inventory.gift"; button.addEventListener("click", () => void connectUserscript()); status.append(button); return; } const loadedClassIds = new Set(assetClassIds.values()); const resolved = [...loadedClassIds].filter( (classId) => giftLookups.get(classId) != null, ); const missingSubIds = resolved.filter( (classId) => giftLookups.get(classId)?.subId === null && !localObservations.has(classId), ); const collectibleClassIds = missingSubIds.filter( (classId) => !submittedClassIds.has(classId) && !submittingObservedClassIds.has(classId), ); let content = lastLookupError ? `Inventory.gift: ${lastLookupError}` : `Inventory.gift: ${resolved.length}/${loadedClassIds.size} loaded gift types matched · ${missingSubIds.length} missing SubIDs`; if (lastCaptureError !== null) { content += ` · ${lastCaptureError}`; } if (collectionState.kind === "running") { content += ` · Checking ${collectionState.completed}/${collectionState.total}…`; } else if (collectionState.message !== null) { content += ` · ${collectionState.message}`; } const canCollect = ownsViewedInventory() && collectibleClassIds.length > 0; const buttonLabel = collectionState.kind === "running" ? "Cancel" : `Check ${collectibleClassIds.length} missing ${ collectibleClassIds.length === 1 ? "SubID" : "SubIDs" }`; const signature = JSON.stringify({ buttonLabel: canCollect || collectionState.kind === "running" ? buttonLabel : null, content, }); if (status.dataset.renderSignature === signature) return; status.dataset.renderSignature = signature; status.replaceChildren(); const text = document.createElement("span"); text.textContent = content; status.append(text); if (canCollect || collectionState.kind === "running") { const button = document.createElement("button"); button.className = "inventory-gift-collect"; button.type = "button"; button.textContent = buttonLabel; button.addEventListener("click", () => { if (collectionState.kind === "running") { collectionCancelled = true; button.disabled = true; button.textContent = "Cancelling…"; } else { void collectMissingSubIds(); } }); status.append(button); } if (collectionState.kind !== "running") { const disconnect = document.createElement("button"); disconnect.className = "inventory-gift-disconnect"; disconnect.type = "button"; disconnect.textContent = "Disconnect"; disconnect.addEventListener("click", () => void disconnectUserscript()); status.append(disconnect); } } function collectionTargets() { const targets = new Map(); assetClassIds.forEach((classId, assetId) => { const gift = giftLookups.get(classId); if ( needsBulkPackageObservation(gift) && !localObservations.has(classId) && !submittedClassIds.has(classId) && !unreportableClassIds.has(classId) && !submittingObservedClassIds.has(classId) && !targets.has(classId) ) { targets.set(classId, assetId); } }); return [...targets].map(([classId, assetId]) => ({ assetId, classId })); } function delay(milliseconds) { return new Promise((resolve) => setTimeout(resolve, milliseconds)); } async function validateUnpack(assetId, signal) { // This reference was captured before the bridge wrapped page fetch, so our // explicit selection and bulk requests do not re-enter external capture. const response = await steamFetch( `/gifts/${encodeURIComponent(assetId)}/validateunpack`, { credentials: "include", headers: { accept: "application/json", "X-Requested-With": "Inventory.gift", }, signal, }, ); if (!response.ok) { throw new Error(`Steam returned ${response.status}`); } return parseValidateUnpackResponse(await response.json()); } function ownsViewedInventory() { return isOwnGiftInventory(inventorySteamId64, connectedSteamId64); } function selectedGiftValidationCandidate(assetId) { if (assetId === null) return null; const classId = assetClassIds.get(assetId); if (classId === undefined) return null; const gift = giftLookups.get(classId); return shouldValidateSelectedGift({ collectionRunning: collectionState.kind === "running", gift, hasLocalObservation: localObservations.has(classId), ownInventory: credential !== null && ownsViewedInventory(), }) ? { assetId, classId } : null; } async function validateSelectedGift(assetId) { selectedValidationTimer = null; if (activeAssetId() !== assetId) return; if (selectedGiftValidationCandidate(assetId) === null) return; if (externalValidateRequests.has(assetId)) { selectedValidationAttempted = true; return; } selectedValidationAttempted = true; const abortController = new unsafeWindow.AbortController(); selectedValidationAbortController = abortController; try { const observation = await validateUnpack(assetId, abortController.signal); lastCaptureError = null; receiveObservedValidate(assetId, observation); } catch (error) { if (error?.name !== "AbortError") { lastCaptureError = error instanceof Error ? `Could not check selected gift: ${error.message}` : "Could not check selected gift"; renderAll(); } } finally { if (selectedValidationAbortController === abortController) { selectedValidationAbortController = null; } } } function scheduleSelectedGiftValidation() { const assetId = activeAssetId(); if (assetId !== selectedValidationAssetId) { resetSelectedGiftValidation(); selectedValidationAssetId = assetId; } if ( selectedValidationAttempted || selectedValidationTimer !== null || selectedGiftValidationCandidate(assetId) === null ) { return; } if (externalValidateRequests.has(assetId)) { selectedValidationAttempted = true; return; } selectedValidationTimer = setTimeout( () => void validateSelectedGift(assetId), selectedGiftValidationDelayMs, ); } async function maybeSubmitObservedValidate(classId) { const observation = localObservations.get(classId); const gift = giftLookups.get(classId); if (observation === undefined || gift === undefined || credential === null) return; if (gift === null) { pendingObservedClassIds.delete(classId); unreportableClassIds.add(classId); return; } if (!needsPackageObservation(gift)) { pendingObservedClassIds.delete(classId); return; } if ( collectionState.kind === "running" || submittedClassIds.has(classId) || submittingObservedClassIds.has(classId) ) { return; } submittingObservedClassIds.add(classId); try { await submitObservations([{ classId, ...observation }]); pendingObservedClassIds.delete(classId); } catch (error) { if ( error instanceof InventoryGiftRequestError && classifyInventoryGiftStatus(error.status).localOnly ) { pendingObservedClassIds.delete(classId); unreportableClassIds.add(classId); } collectionState = { completed: 0, kind: "error", message: error instanceof Error ? `Could not submit selected gift: ${error.message}` : "Could not submit selected gift", total: 1, }; } finally { submittingObservedClassIds.delete(classId); renderAll(); } } function receiveObservedValidate(assetId, observation) { if (connectedSteamId64 === null) { capturedValidateResponses.set(assetId, observation); return; } if (!ownsViewedInventory()) return; const classId = assetClassIds.get(assetId); if (classId === undefined) { capturedValidateResponses.set(assetId, observation); return; } localObservations.set(classId, observation); pendingObservedClassIds.add(classId); renderAll(); void maybeSubmitObservedValidate(classId); } function flushCapturedValidateResponses() { if (connectedSteamId64 === null) return; for (const [assetId, observation] of capturedValidateResponses) { if (!assetClassIds.has(assetId)) continue; capturedValidateResponses.delete(assetId); receiveObservedValidate(assetId, observation); } } function receiveValidateUnpack(event) { if (credential === null) return; if ( event.detail === null || typeof event.detail !== "object" || !isDecimalId(event.detail.assetId) ) { return; } externalValidateRequests.delete(event.detail.assetId); try { const observation = parseValidateUnpackResponse(event.detail.response); lastCaptureError = null; receiveObservedValidate(event.detail.assetId, observation); } catch { lastCaptureError = "Steam returned an unsupported validateunpack response"; renderAll(); } } function receiveValidateStart(event) { if ( credential === null || event.detail === null || typeof event.detail !== "object" || !isDecimalId(event.detail.assetId) ) { return; } const assetId = event.detail.assetId; externalValidateRequests.add(assetId); if (selectedValidationAssetId === assetId) { clearTimeout(selectedValidationTimer); selectedValidationTimer = null; selectedValidationAbortController?.abort(); selectedValidationAttempted = true; } } function receiveValidateEnd(event) { if ( event.detail !== null && typeof event.detail === "object" && isDecimalId(event.detail.assetId) ) { externalValidateRequests.delete(event.detail.assetId); } } function receiveCaptureFailure(event) { if (event.detail === null || typeof event.detail !== "object") return; if (isDecimalId(event.detail.assetId)) { externalValidateRequests.delete(event.detail.assetId); } const status = Number.isSafeInteger(event.detail.status) ? ` (${event.detail.status})` : ""; lastCaptureError = `${String(event.detail.message)}${status}`; renderAll(); } async function submitObservations(observations) { if (inventorySteamId64 === null) { throw new Error("Steam inventory owner is unavailable"); } for (let index = 0; index < observations.length; index += lookupBatchSize) { const batch = observations.slice(index, index + lookupBatchSize); let response; try { response = await requestJson( `${apiBaseUrl}/api/userscript/v1/subid-observations`, { observations: batch }, ); } catch (error) { if ( error instanceof InventoryGiftRequestError && classifyInventoryGiftStatus(error.status).localOnly ) { batch.forEach((observation) => unreportableClassIds.add(observation.classId), ); } throw error; } const acceptedCount = parseObservationResponse(response, batch.length); if (acceptedCount !== batch.length) { throw new Error("Inventory.gift did not recognize every gift"); } batch.forEach((observation) => submittedClassIds.add(observation.classId)); } } async function collectMissingSubIds() { if (collectionState.kind === "running" || !ownsViewedInventory()) { return; } clearTimeout(selectedValidationTimer); selectedValidationTimer = null; selectedValidationAbortController?.abort(); selectedValidationAbortController = null; selectedValidationAttempted = true; const targets = collectionTargets(); if (targets.length === 0) return; collectionCancelled = false; collectionState = { completed: 0, kind: "running", message: null, total: targets.length, }; renderAll(); let pendingObservations = []; let completed = 0; let failure = null; for (let index = 0; index < targets.length; index += 1) { const target = targets[index]; if (collectionCancelled) break; try { const observation = await validateUnpack(target.assetId); pendingObservations.push({ classId: target.classId, ...observation }); completed += 1; collectionState.completed = completed; renderAll(); if (pendingObservations.length === observationCheckpointSize) { await submitObservations(pendingObservations); pendingObservations = []; } } catch (error) { failure = error instanceof Error ? error.message : "Steam request failed"; if (error instanceof InventoryGiftRequestError) pendingObservations = []; break; } if (index < targets.length - 1 && !collectionCancelled) { await delay(validateUnpackDelayMs); } } try { if (pendingObservations.length > 0) { await submitObservations(pendingObservations); } } catch (error) { failure = error instanceof Error ? error.message : "Observation submission failed"; } collectionState = { completed, kind: failure === null ? "complete" : "error", message: failure !== null ? `Stopped after ${completed}/${targets.length}: ${failure}` : collectionCancelled ? `Cancelled after submitting ${completed}` : `Submitted ${completed} ${ completed === 1 ? "observation" : "observations" }`, total: targets.length, }; renderAll(); for (const classId of pendingObservedClassIds) { void maybeSubmitObservedValidate(classId); } } function renderAll() { renderStatus(); renderDetails(); scheduleSelectedGiftValidation(); } function installStyles() { GM_addStyle(` .inventory-gift-status { clear: both; color: #8f98a0; font-size: 12px; line-height: 1.4; margin: 6px 0 10px; padding-left: 10px; } .inventory-gift-collect { background: #1a9fff; border: 0; border-radius: 2px; color: #fff; cursor: pointer; font: inherit; margin-left: 8px; padding: 4px 8px; } .inventory-gift-collect:hover { background: #66c0f4; } .inventory-gift-collect:focus-visible { outline: 2px solid #fff; } .inventory-gift-collect:disabled { cursor: default; opacity: 0.65; } .inventory-gift-disconnect { background: transparent; border: 0; color: #8f98a0; cursor: pointer; font: inherit; margin-left: 8px; padding: 4px; text-decoration: underline; } .inventory-gift-disconnect:focus-visible { outline: 2px solid #fff; } .inventory-gift-detail { align-items: flex-start; background: rgba(32, 83, 113, 0.28); border-left: 3px solid #67c1f5; color: #d6d7d8; display: flex; flex-direction: column; font-size: 13px; gap: 3px; margin: 4px 0 8px; padding: 8px 10px; } .inventory-gift-detail strong { color: #67c1f5; } .inventory-gift-detail a { border-radius: 2px; color: #67c1f5; padding: 1px 3px; text-decoration: none; } .inventory-gift-detail a:hover { background: rgba(103, 193, 245, 0.18); color: #fff; text-decoration: underline; } .inventory-gift-detail a:focus-visible { outline: 2px solid #67c1f5; outline-offset: 1px; } `); } window.addEventListener(captureEvents.inventory, receiveAssets); window.addEventListener(captureEvents.validate, receiveValidateUnpack); window.addEventListener(captureEvents.validateEnd, receiveValidateEnd); window.addEventListener(captureEvents.validateStart, receiveValidateStart); window.addEventListener(captureEvents.failure, receiveCaptureFailure); installSteamCaptureBridge(unsafeWindow, captureEvents); installStyles(); new MutationObserver(renderAll).observe(document.documentElement, { childList: true, subtree: true, }); renderAll(); })();