Skip to content

Commit

Permalink
Clear home timeout on URL change
Browse files Browse the repository at this point in the history
When the user navigated to home, if he navigated to another section before the timeout ended, (handleHotCards * enabled cards) functions were run, and, if there were hot cards in the given section, the elements were duplicated because of the additional handleHotCards that would run in the current page.

- Add reusable overrideHistoryMethods function in helpers.js
- Replace code with new overrideHistoryMethods function
  • Loading branch information
HandyRandyx committed Aug 2, 2024
1 parent 324536b commit 9731991
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
14 changes: 6 additions & 8 deletions plugins/hotCards/hotCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@
*/
function handleHomeHotCards() {
const pattern = /^\/$/;
let timeoutId;

overrideHistoryMethods(() => clearTimeout(timeoutId));

registerPathChangeListener(pattern, () => {
setTimeout(() => {
timeoutId = setTimeout(() => {
for (const card of Object.values(CARDS))
if (card.enabled) handleHotCards(card, true);
}, 3000);
Expand Down Expand Up @@ -871,11 +875,5 @@
hotCards.length = 0;
}

["pushState", "replaceState"].forEach((method) => {
const original = history[method];
history[method] = function () {
restoreCards();
return original.apply(this, arguments);
};
});
overrideHistoryMethods(restoreCards);
})();
2 changes: 1 addition & 1 deletion plugins/hotCards/hotCards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ ui:
requires:
- CommunityScriptsUILibrary
javascript:
- utils/helpers.js
- utils/fetchInterceptor.js
- utils/stashHandler.js
- utils/registerPathChangeListener.js
- utils/helpers.js
- hotCards.js
css:
- hotCards.css
Expand Down
13 changes: 13 additions & 0 deletions plugins/hotCards/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ function waitForImageLoad(imageEl, callback) {
if (imageEl.complete) return callback(imageEl);
setTimeout(waitForImageLoad, 100, imageEl, callback);
}

/** History */

function overrideHistoryMethods(callback) {
["pushState", "replaceState"].forEach((method) => {
const original = history[method];
history[method] = function () {
const result = original.apply(this, arguments);
callback();
return result;
};
});
}
9 changes: 1 addition & 8 deletions plugins/hotCards/utils/registerPathChangeListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ function registerPathChangeListener(pattern, callback) {
window.addEventListener("popstate", checkURL);

// Hijack pushState and replaceState methods
["pushState", "replaceState"].forEach((method) => {
const original = history[method];
history[method] = function () {
const result = original.apply(this, arguments);
checkURL();
return result;
};
});
overrideHistoryMethods(checkURL);

// Initial check
checkURL();
Expand Down

0 comments on commit 9731991

Please sign in to comment.