Skip to content

Commit f3443af

Browse files
committed
code refactor: extract duplicate code, remove leftover stuff, TODO for new bug
1 parent 461bc81 commit f3443af

1 file changed

Lines changed: 16 additions & 31 deletions

File tree

ItchioSimpleRemoveFromCollection.user.js

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

3030
let pageUrl = window.location.href
31-
3231
if (IsAddToCollectionPage(pageUrl)) {
32+
// TODO: not working in page with url like example: https://itch.io/g/tobyfox/deltarune/add-to-collection?source=home
3333
if (!GameInCollection()) { return }
3434

3535
let gameCreator = GetUserFromGamePage(pageUrl)
3636
let gameName = GetGameNameFromGamePage(pageUrl)
3737

38-
// TODO: refactor duplicate code
3938
Itch.getGameData({
4039
user: gameCreator,
4140
game: gameName,
4241
onComplete: function (data) {
4342
if ('errors' in data) {
43+
console.error(`ERROR: Could not find game "${gameName}" by ${gameCreator}.`)
4444
console.error(data)
4545
return
4646
}
4747

4848
_gameId = data.id
49-
50-
$('li.already_in_row').each(function (i) {
51-
AddRemoveButton($('li.already_in_row').eq(i))
52-
})
53-
54-
$('.remove_from_coll_btn').click(function (e) {
55-
let collId = $(this).attr("value")
56-
PostRemoveGameFromCollection(collId, _gameId, _csrf, $(this).closest('li.already_in_row'))
57-
})
49+
AddAndEnableRemoveButtons()
5850
}
5951
})
6052
} else {
@@ -81,15 +73,7 @@ $(document).ready(function () {
8173
// On the lightbox being loaded on the page
8274
function OnLightboxChange() {
8375
if (!GameInCollection()) { return }
84-
85-
$('li.already_in_row').each(function (i) {
86-
AddRemoveButton($('li.already_in_row').eq(i))
87-
})
88-
89-
$('.remove_from_coll_btn').click(function (e) {
90-
let collId = $(this).attr("value")
91-
PostRemoveGameFromCollection(collId, _gameId, _csrf, $(this).closest('li.already_in_row'))
92-
})
76+
AddAndEnableRemoveButtons()
9377
}
9478

9579

@@ -117,9 +101,8 @@ $(document).ready(function () {
117101
return gameId
118102
}
119103

120-
function AddRemoveButton(collectionLinkEl) {
104+
function AddRemoveButtonUi(collectionLinkEl) {
121105
let collectionId = collectionLinkEl.find("a").prop("href").split("/")[4]
122-
// TODO: use style margin instead of nbsp
123106
const removeHTML = $(`<a class="remove_from_coll_btn button outline" title="Remove the game from the collection." value="${collectionId}">❌</a>`)
124107
removeHTML.css({
125108
"margin-right": "5px",
@@ -146,6 +129,17 @@ $(document).ready(function () {
146129
});
147130
}
148131

132+
function AddAndEnableRemoveButtons() {
133+
$('li.already_in_row').each(function (i) {
134+
AddRemoveButtonUi($('li.already_in_row').eq(i))
135+
})
136+
// Add click event for the newly added remove buttons
137+
$('.remove_from_coll_btn').click(function (e) {
138+
let collId = $(this).attr("value")
139+
PostRemoveGameFromCollection(collId, _gameId, _csrf, $(this).closest('li.already_in_row'))
140+
})
141+
}
142+
149143
function IsAddToCollectionPage(url) {
150144
return url.includes("add-to-collection")
151145
}
@@ -157,13 +151,4 @@ $(document).ready(function () {
157151
function GetGameNameFromGamePage(url) {
158152
return url.split("/")[3]
159153
}
160-
161-
162-
// --- FOR DEBUGGING ---
163-
var showDebugMessages = true;
164-
165-
function log(message) {
166-
if (showDebugMessages) { console.log(message); }
167-
}
168-
169154
});

0 commit comments

Comments
 (0)