diff options
| author | RadoslavL <rlelchev@abv.bg> | 2023-11-09 21:56:41 +0200 |
|---|---|---|
| committer | RadoslavL <rlelchev@abv.bg> | 2023-11-09 21:56:41 +0200 |
| commit | 019807256f74dab1ad7d56e7f1d6b993009999fd (patch) | |
| tree | cce1bdd2b62723263d84b371e3477e7696d83d62 | |
| parent | 2b2d67fcfa4be7f0284cad5464c13128e31d3c40 (diff) | |
| download | invidious-019807256f74dab1ad7d56e7f1d6b993009999fd.tar.gz invidious-019807256f74dab1ad7d56e7f1d6b993009999fd.tar.bz2 invidious-019807256f74dab1ad7d56e7f1d6b993009999fd.zip | |
Seperated repetitive code in a function
| -rw-r--r-- | assets/js/pagination.js | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/assets/js/pagination.js b/assets/js/pagination.js index ef32adae..2e560a34 100644 --- a/assets/js/pagination.js +++ b/assets/js/pagination.js @@ -32,23 +32,37 @@ function button_press(){ // On the first page, the stored continuation token is null. if (prev_ctoken === null) { sessionStorage.removeItem(CONT_CACHE_KEY); - let url = window.location.href.split('?')[0]; - let params = window.location.href.split('?')[1]; - let url_params = new URLSearchParams(params); - url_params.delete('continuation'); - window.location.href = `${url}?${url_params.toString()}`; + let url = set_continuation(); + window.location.href = url; return; } sessionStorage.setItem(CONT_CACHE_KEY, JSON.stringify(prev_data)); + let url = set_continuation(prev_ctoken); + + window.location.href = url; +}; + +// Method to set the current page's continuation token +// Removes the continuation parameter when a continuation token is not given +function set_continuation(prev_ctoken = null){ let url = window.location.href.split('?')[0]; let params = window.location.href.split('?')[1]; let url_params = new URLSearchParams(params); - url_params.set("continuation", prev_ctoken); - window.location.href = `${url}?${url_params.toString()}`; -}; + if (prev_ctoken) { + url_params.set("continuation", prev_ctoken); + } else { + url_params.delete('continuation'); + }; + + if(Array.from(url_params).length > 0){ + return `${url}?${url_params.toString()}`; + } else { + return url; + } +} addEventListener('DOMContentLoaded', function(){ const pagination_data = JSON.parse(document.getElementById('pagination-data').textContent); |
