Skip to content

Commit 03abbf4

Browse files
committed
V1.11.5
1 parent 22f1017 commit 03abbf4

File tree

1 file changed

+100
-84
lines changed

1 file changed

+100
-84
lines changed

Steam Web Integration.user.js

Lines changed: 100 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ function onChange(elem) {
8484
showToast();
8585
}
8686

87+
function arrayToObject(array, key) {
88+
if (!key) {
89+
return array.reduce((obj, item) => Object.assign(obj, { [item]: 1 }), {});
90+
}
91+
92+
return array.reduce((obj, item) => Object.assign(obj, { [item[key]]: item }), {});
93+
}
94+
8795
function createBoxNode() {
8896
return $(`<div/>`, {
8997
"class": `swi-block${settings.boxed ? ` swi-boxed` : ``}`
@@ -118,9 +126,10 @@ function refreshDecommissioned(callback) {
118126
}
119127

120128
let cachedDecommissioned = JSON.parse(GM_getValue(`swi_decommissioned`, null));
121-
if (Array.isArray(cachedDecommissioned)) { // TODO: temporary code to transition from Array to Object; can delete this in ~2022 and change 'let' above back to 'const'
122-
cachedDecommissioned = null;
129+
if (Array.isArray(cachedDecommissioned)) { // fix old data format
130+
cachedDecommissioned = arrayToObject(cachedDecommissioned, `appid`);
123131
}
132+
124133
const lastCachedDecommissioned = GM_getValue(`swi_decommissioned_last`, 0);
125134
if (cachedDecommissioned && Date.now() - lastCachedDecommissioned < settings.decommissionedRefreshInterval * 60000) {
126135
callback(cachedDecommissioned);
@@ -136,8 +145,7 @@ function refreshDecommissioned(callback) {
136145
try {
137146
json = JSON.parse(response.responseText);
138147
if (json.success) {
139-
const responseAsObject = json.removed_apps.reduce((obj, item) => (obj[item.appid] = item, obj), {}); // convert to object
140-
// could delete item.appid from each entry to save minor storage space, but might hurt performance and increase complexity?
148+
const responseAsObject = arrayToObject(json.removed_apps, `appid`);
141149
GM_setValue(`swi_decommissioned`, JSON.stringify(responseAsObject));
142150
GM_setValue(`swi_decommissioned_last`, Date.now());
143151
callback(responseAsObject);
@@ -345,64 +353,62 @@ function doApp(elem, wishlist, ownedApps, ignoredApps, followedApps, decommissio
345353
return;
346354
}
347355

348-
setTimeout(() => {
349-
let html;
350-
let subject;
351-
if (dlc && dlc[appID]) {
352-
subject = `DLC`;
353-
} else if (!dlc) {
354-
subject = `Game or DLC`;
355-
} else {
356-
subject = `Game`;
357-
}
356+
let html;
357+
let subject;
358+
if (dlc && dlc[appID]) {
359+
subject = `DLC`;
360+
} else if (!dlc) {
361+
subject = `Game or DLC`;
362+
} else {
363+
subject = `Game`;
364+
}
358365

359-
if (ownedApps && ownedApps.includes(appID)) { // if owned
360-
html = getIconHTML(settings.ownedColor, `${subject} (${appID}) owned`, lcs, settings.ownedIcon); // ✔
361-
} else if (wishlist.includes(appID)) { // if not owned and wishlisted
362-
html = getIconHTML(settings.wishlistColor, `${subject} (${appID}) wishlisted`, lcs, settings.wishlistIcon); // ❤
363-
} else { // else not owned and not wishlisted
364-
html = getIconHTML(settings.unownedColor, `${subject} (${appID}) not owned`, lcs, settings.unownedIcon); // ✘
365-
}
366+
if (ownedApps && ownedApps[appID]) { // if owned
367+
html = getIconHTML(settings.ownedColor, `${subject} (${appID}) owned`, lcs, settings.ownedIcon); // ✔
368+
} else if (wishlist[appID]) { // if not owned and wishlisted
369+
html = getIconHTML(settings.wishlistColor, `${subject} (${appID}) wishlisted`, lcs, settings.wishlistIcon); // ❤
370+
} else { // else not owned and not wishlisted
371+
html = getIconHTML(settings.unownedColor, `${subject} (${appID}) not owned`, lcs, settings.unownedIcon); // ✘
372+
}
366373

367-
if (settings.wantFollowed && followedApps && followedApps.includes(appID)) {
368-
html += getIconHTML(settings.followedColor, `${subject} (${appID}) followed`, lcs, settings.followedIcon); // ★
369-
}
374+
if (settings.wantFollowed && followedApps && followedApps[appID]) {
375+
html += getIconHTML(settings.followedColor, `${subject} (${appID}) followed`, lcs, settings.followedIcon); // ★
376+
}
370377

371-
if (settings.wantIgnores && ignoredApps && ignoredApps.includes(appID)) { // if ignored and enabled
372-
html += getIconHTML(settings.ignoredColor, `${subject} (${appID}) ignored`, llcs, settings.ignoredIcon); // 🛇
373-
}
378+
if (settings.wantIgnores && ignoredApps && ignoredApps[appID]) { // if ignored and enabled
379+
html += getIconHTML(settings.ignoredColor, `${subject} (${appID}) ignored`, llcs, settings.ignoredIcon); // 🛇
380+
}
374381

375-
if (settings.wantDLC && dlc && dlc[appID]) { // if DLC and enabled
376-
const base = dlc[appID].base_appID;
377-
const ownsBase = ownedApps.includes(base);
378-
html += getIconHTML(settings.dlcColor, `${subject} (${appID}) is downloadable content for an ${ownsBase ? `` : `un`}owned base game (${base})`, dlclcs, settings.dlcIcon); // ⇩
379-
}
382+
if (settings.wantDLC && dlc && dlc[appID]) { // if DLC and enabled
383+
const base = dlc[appID].base_appID;
384+
const ownsBase = Boolean(ownedApps[base]);
385+
html += getIconHTML(settings.dlcColor, `${subject} (${appID}) is downloadable content for an ${ownsBase ? `` : `un`}owned base game (${base})`, dlclcs, settings.dlcIcon); // ⇩
386+
}
380387

381-
if (settings.wantDecommissioned && decommissioned && decommissioned[appID]) { // if decommissioned and enabled
382-
const app = decommissioned[appID];
383-
html += getIconHTML(settings.decommissionedColor, `The ${app.type} '${app.name.replace(/"|'/g, ``)}' (${appID}) is ${app.category.toLowerCase()} and has only ${app.count} confirmed owner${app.count === 1 ? `` : `s`} on Steam`, dlcs, settings.decommissionedIcon, `https://steam-tracker.com/app/${appID}/`); // 🗑
384-
}
388+
if (settings.wantDecommissioned && decommissioned && decommissioned[appID]) { // if decommissioned and enabled
389+
const app = decommissioned[appID];
390+
html += getIconHTML(settings.decommissionedColor, `The ${app.type} '${app.name.replace(/"|'/g, ``)}' (${appID}) is ${app.category.toLowerCase()} and has only ${app.count} confirmed owner${app.count === 1 ? `` : `s`} on Steam`, dlcs, settings.decommissionedIcon, `https://steam-tracker.com/app/${appID}/`); // 🗑
391+
}
385392

386-
if (settings.wantLimited && limited && limited[appID]) { // if limited and enabled
387-
html += getIconHTML(settings.limitedColor, `Game (${appID}) has profile features limited`, llcs, settings.limitedIcon); // ⚙
388-
}
393+
if (settings.wantLimited && limited && limited[appID]) { // if limited and enabled
394+
html += getIconHTML(settings.limitedColor, `Game (${appID}) has profile features limited`, llcs, settings.limitedIcon); // ⚙
395+
}
389396

390-
if (settings.wantCards && cards && cards[appID] && cards[appID].cards && cards[appID].cards > 0) { // if has cards and enabled
391-
html += getIconHTML(settings.cardColor, `Game (${appID}) has ${cards[appID].cards} ${cards[appID].marketable ? `` : `un`}marketable card${cards[appID].cards === 1 ? `` : `s`}`, clcs, settings.cardIcon, `https://www.steamcardexchange.net/index.php?gamepage-appid-${appID}`);
392-
}
397+
if (settings.wantCards && cards && cards[appID] && cards[appID].cards && cards[appID].cards > 0) { // if has cards and enabled
398+
html += getIconHTML(settings.cardColor, `Game (${appID}) has ${cards[appID].cards} ${cards[appID].marketable ? `` : `un`}marketable card${cards[appID].cards === 1 ? `` : `s`}`, clcs, settings.cardIcon, `https://www.steamcardexchange.net/index.php?gamepage-appid-${appID}`);
399+
}
393400

394-
if (settings.wantBundles && bundles && bundles[appID] && bundles[appID].bundles && bundles[appID].bundles > 0) { // if bundled and enabled
395-
html += getIconHTML(settings.bundleColor, `Game (${appID}) has been in ${bundles[appID].bundles} bundle${bundles[appID].bundles === 1 ? `` : `s`}`, blcs, settings.bundleIcon, `https://barter.vg/steam/app/${appID}/#bundles`);
396-
}
401+
if (settings.wantBundles && bundles && bundles[appID] && bundles[appID].bundles && bundles[appID].bundles > 0) { // if bundled and enabled
402+
html += getIconHTML(settings.bundleColor, `Game (${appID}) has been in ${bundles[appID].bundles} bundle${bundles[appID].bundles === 1 ? `` : `s`}`, blcs, settings.bundleIcon, `https://barter.vg/steam/app/${appID}/#bundles`);
403+
}
397404

398-
if (settings.prefix) {
399-
$(elem).before(getBoxNode(html, appID));
400-
} else {
401-
$(elem).after(getBoxNode(html, appID));
402-
}
405+
if (settings.prefix) {
406+
$(elem).before(getBoxNode(html, appID));
407+
} else {
408+
$(elem).after(getBoxNode(html, appID));
409+
}
403410

404-
$(elem).parent().css(`overflow`, `visible`);
405-
}, 0);
411+
$(elem).parent().css(`overflow`, `visible`);
406412
}
407413

408414
function doSub(elem, ownedPackages, lcs) {
@@ -422,7 +428,7 @@ function doSub(elem, ownedPackages, lcs) {
422428
setTimeout(() => {
423429
let html;
424430

425-
if (ownedPackages.includes(subID)) { // if owned
431+
if (ownedPackages[subID]) { // if owned
426432
html = getIconHTML(settings.ownedColor, `Package (${subID}) owned`, lcs, settings.ownedIcon); // ✔
427433
} else { // else not owned
428434
html = getIconHTML(settings.unownedColor, `Package (${subID}) not owned`, lcs, settings.unownedIcon); // ✖
@@ -439,12 +445,7 @@ function doSub(elem, ownedPackages, lcs) {
439445
}
440446

441447
function integrate(userdata, decommissioned, cards, bundles, limited, dlc, lastCached) {
442-
const ignoredApps = Object.keys(userdata.rgIgnoredApps).map((a) => parseInt(a, 10));
443-
const ownedApps = userdata.rgOwnedApps;
444-
const ownedPackages = userdata.rgOwnedPackages;
445-
const followedApps = userdata.rgFollowedApps;
446-
const wishlist = userdata.rgWishlist;
447-
448+
const { ignoredApps, ownedApps, ownedPackages, followedApps, wishlist } = userdata;
448449
const lcs = settings.dateOverride ? new Date(lastCached).toLocaleString(`sv-SE`) : new Date(lastCached).toLocaleString();
449450
const dlcs = new Date(GM_getValue(`swi_decommissioned_last`, 0)).toLocaleString(settings.dateOverride ? `sv-SE` : undefined);
450451
const dlclcs = new Date(GM_getValue(`swi_dlc_last`, 0)).toLocaleString(settings.dateOverride ? `sv-SE` : undefined);
@@ -535,12 +536,25 @@ function integrate(userdata, decommissioned, cards, bundles, limited, dlc, lastC
535536
});
536537
}
537538

539+
function processUserData(userdata) {
540+
const ignoredApps = arrayToObject(Object.keys(userdata.rgIgnoredApps)); // change 0 values to 1
541+
const ownedApps = arrayToObject(userdata.rgOwnedApps);
542+
const ownedPackages = arrayToObject(userdata.rgOwnedPackages);
543+
const followedApps = arrayToObject(userdata.rgFollowedApps);
544+
const wishlist = arrayToObject(userdata.rgWishlist);
545+
return { ignoredApps, ownedApps, ownedPackages, followedApps, wishlist };
546+
}
547+
538548
function refresh() {
539549
const cachedJson = GM_getValue(`swi_data`, null);
540550
let lastCached = GM_getValue(`swi_last`, 0);
541551

542552
if (cachedJson && Date.now() - lastCached < settings.userRefreshInterval * 60000) {
543-
const userdata = JSON.parse(cachedJson);
553+
let userdata = JSON.parse(cachedJson);
554+
if (userdata.rgOwnedApps) { // fix old data format
555+
userdata = processUserData(userdata);
556+
}
557+
544558
refreshDecommissioned((decommissioned) => refreshDLC((dlc) => refreshLimited((limited) => refreshCards((cards) => refreshBundles((bundles) => integrate(userdata, decommissioned, cards, bundles, limited, dlc, lastCached))))));
545559
return;
546560
}
@@ -551,17 +565,18 @@ function refresh() {
551565
"onload": (response) => {
552566
let userdata = JSON.parse(response.responseText);
553567

554-
if (userdata.rgOwnedApps.length === 0 && userdata.rgOwnedPackages.length === 0 && userdata.rgIgnoredApps.length === 0 && userdata.rgWishlist.length === 0 && userdata.rgFollowedApps.length === 0) { // not logged in
568+
if (userdata.rgOwnedApps.length === 0 && userdata.rgOwnedPackages.length === 0) { // not logged in
555569
if (!cachedJson) {
556570
console.log(`[Steam Web Integration] No cached information available. Please login to Steam to fix this.`);
557571
return;
558572
}
559573

560574
userdata = JSON.parse(cachedJson);
561575
} else {
576+
userdata = processUserData(userdata);
562577
lastCached = Date.now();
563-
GM_setValue(`swi_last`, lastCached);
564578
GM_setValue(`swi_data`, JSON.stringify(userdata));
579+
GM_setValue(`swi_last`, lastCached);
565580
}
566581

567582
refreshDecommissioned((decommissioned) => refreshDLC((dlc) => refreshLimited((limited) => refreshCards((cards) => refreshBundles((bundles) => integrate(userdata, decommissioned, cards, bundles, limited, dlc, lastCached))))));
@@ -570,7 +585,7 @@ function refresh() {
570585
}
571586

572587
function init() {
573-
const settingsuri = `https://revadike.com/swi/settings`;
588+
const settingsUrl = `https://revadike.com/swi/settings`;
574589

575590
const defaults = {
576591
"attributes": [`href`, `src`, `style`],
@@ -616,25 +631,26 @@ function init() {
616631
};
617632

618633
const stylesheet = `
619-
.swi-block {
620-
display: inline-block;
621-
line-height: initial;
622-
}
623-
.swi-block.swi-boxed {
624-
background: rgba(0, 0, 0, 0.7);
625-
border-radius: 5px;
626-
margin: auto 4px auto 4px;
627-
padding: 2px 4px 2px 4px;
628-
position: relative;
629-
}
630-
.swi-block span {
631-
cursor: help;
632-
margin: 2px;
633-
}
634-
.swi-block a {
635-
text-decoration: none;
636-
}
637-
`;
634+
.swi-block {
635+
display: inline-block;
636+
line-height: initial;
637+
}
638+
.swi-block.swi-boxed {
639+
background: rgba(0, 0, 0, 0.7);
640+
border-radius: 5px;
641+
margin: auto 4px auto 4px;
642+
padding: 2px 4px 2px 4px;
643+
position: relative;
644+
}
645+
.swi-block > span {
646+
cursor: help;
647+
margin: 2px;
648+
}
649+
.swi-block > span > a {
650+
cursor: help;
651+
text-decoration: none;
652+
}
653+
`;
638654

639655
settings = JSON.parse(GM_getValue(`swi_settings`, JSON.stringify(defaults)));
640656
Object.keys(defaults).forEach((setting) => {
@@ -645,7 +661,7 @@ function init() {
645661
settings[setting] = defaults[setting];
646662
});
647663

648-
if (unsafeWindow.location.href.startsWith(settingsuri)) {
664+
if (unsafeWindow.location.href.startsWith(settingsUrl)) {
649665
unsafeWindow.onChange = onChange;
650666
unsafeWindow.scriptInfo = GM_info.script;
651667
unsafeWindow.settings = settings;
@@ -660,7 +676,7 @@ function init() {
660676
}
661677

662678
// Open the setup page on any web page.
663-
GM_registerMenuCommand(`Change settings`, () => unsafeWindow.open(settingsuri, `_blank`));
679+
GM_registerMenuCommand(`Change settings`, () => unsafeWindow.open(settingsUrl, `_blank`));
664680

665681
// Factory reset on any web page.
666682
GM_registerMenuCommand(`Factory reset`, factoryReset);

0 commit comments

Comments
 (0)