|
20 | 20 | }, |
21 | 21 | }); |
22 | 22 |
|
23 | | - doc.getElementById('searchbox').addEventListener('keyup', function (event) { |
| 23 | + getNextSearchURL = () => { |
| 24 | + const searchInputElements = document.getElementsByClassName('ais-SearchBox-input'); |
| 25 | + const text = searchInputElements[0].value.trim(); |
| 26 | + const selectedPaginationItemElements = doc.getElementsByClassName('ais-Pagination-item--selected'); |
| 27 | + const page = selectedPaginationItemElements.length ? parseInt(selectedPaginationItemElements[0].innerText) : 1 |
24 | 28 | const url = new URL(window.location); |
25 | | - url.searchParams.set('sq', event.target.value.trim()); |
26 | | - if (url.href != window.location.href) { |
27 | | - url.searchParams.set('p', 1); |
| 29 | + url.searchParams.set('sq', text); |
| 30 | + url.searchParams.set('p', page); |
| 31 | + return url; |
| 32 | + }; |
| 33 | + |
| 34 | + let idleTimer; |
| 35 | + const startIdleTimer = (url) => { |
| 36 | + stopIdleTimer(); |
| 37 | + idleTimer = window.setTimeout(()=>{ |
28 | 38 | window.history.pushState({}, '', url); |
| 39 | + }, 1500); |
| 40 | + }; |
| 41 | + const stopIdleTimer = () => { |
| 42 | + window.clearTimeout(idleTimer); |
| 43 | + } |
| 44 | + |
| 45 | + doc.getElementById('searchbox').addEventListener('keyup', function (event) { |
| 46 | + const url = getNextSearchURL(); |
| 47 | + if (url.searchParams.get('sq') != (new URL(window.location)).searchParams.get('sq')) { |
| 48 | + url.searchParams.set('p', 1); |
| 49 | + startIdleTimer(url); |
29 | 50 | } |
30 | 51 | }) |
31 | 52 |
|
32 | 53 | doc.getElementById('pagination').addEventListener('click', function (event) { |
33 | | - const page = doc.getElementsByClassName('ais-Pagination-item--selected').length ? parseInt(doc.getElementsByClassName('ais-Pagination-item--selected')[0].innerText) : 1 |
34 | | - const url = new URL(window.location); |
35 | | - url.searchParams.set('p', page); |
| 54 | + stopIdleTimer(); |
| 55 | + const url = getNextSearchURL(); |
36 | 56 | window.history.pushState({}, '', url); |
37 | 57 | }) |
38 | 58 |
|
|
0 commit comments