Skip to content

Commit 72a408c

Browse files
committed
minor refactors: function renames, using promise pattern for clarity
1 parent 415274f commit 72a408c

1 file changed

Lines changed: 25 additions & 18 deletions

File tree

ItchioSimpleRemoveFromCollection.user.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,13 @@ $(document).ready(function () {
2828
_csrf = GetCsrf()
2929

3030
let pageUrl = window.location.href
31+
3132
if (IsAddToCollectionPage(pageUrl)) {
32-
if (!GameInCollection()) { return }
33+
if (!IsGameInAnyCollection()) { return }
3334

3435
let gameCreator = GetUserFromGamePage(pageUrl)
3536
let gameName = GetGameNameFromGamePage(pageUrl)
36-
37-
Itch.getGameData({
38-
user: gameCreator,
39-
game: gameName,
40-
onComplete: function (data) {
41-
if ('errors' in data) {
42-
console.error(`ERROR: Could not find game "${gameName}" by ${gameCreator}.`)
43-
console.error(data)
44-
return
45-
}
46-
47-
_gameId = data.id
48-
AddAndEnableRemoveButtons()
49-
}
50-
})
37+
GetGameId(gameCreator, gameName).then(AddAndEnableRemoveButtons())
5138
} else {
5239
// wait for add-to-collection lightbox to load on any other page
5340
bodyObserver.observe($("body")[0], { childList: true })
@@ -65,13 +52,14 @@ $(document).ready(function () {
6552
// observe lightbox div once loaded on the page
6653
if ($("div#lightbox_container").length > 0) {
6754
lightboxObserver.observe($("div#lightbox_container")[0], { childList: true })
55+
// lightbox container stays loaded now, so we can stop observing for it to load
6856
bodyObserver.disconnect()
6957
}
7058
}
7159

7260
// On the lightbox being loaded on the page
7361
function OnLightboxChange() {
74-
if (!GameInCollection()) { return }
62+
if (!IsGameInAnyCollection()) { return }
7563
AddAndEnableRemoveButtons()
7664
}
7765

@@ -82,7 +70,7 @@ $(document).ready(function () {
8270
return $("meta[name=csrf_token]").attr("value")
8371
}
8472

85-
function GameInCollection() {
73+
function IsGameInAnyCollection() {
8674
return $("ul.already_in").length > 0
8775
}
8876

@@ -100,6 +88,25 @@ $(document).ready(function () {
10088
return gameId
10189
}
10290

91+
async function GetGameId(gameCreator, gameName) {
92+
return new Promise(function(resolve, reject) {
93+
Itch.getGameData({
94+
user: gameCreator,
95+
game: gameName,
96+
onComplete: function (data) {
97+
if ('errors' in data) {
98+
console.error(`ERROR: Could not find game "${this.user}" by ${this.game}.`)
99+
console.error(data)
100+
reject()
101+
}
102+
103+
_gameId = data.id
104+
resolve(_gameId)
105+
}
106+
})
107+
})
108+
}
109+
103110
function AddRemoveButtonUi(collectionLinkEl) {
104111
let collectionId = collectionLinkEl.find("a").prop("href").split("/")[4]
105112
const removeHTML = $(`<a class="remove_from_coll_btn button outline" title="Remove the game from the collection." value="${collectionId}">❌</a>`)

0 commit comments

Comments
 (0)