|
24 | 24 | <button class="btn btn-dark" id="view-image-btn" type="button">Go</button> |
25 | 25 | <a class="btn btn-dark mx-2" href="" target="_blank" type="button">Clone Reader</a> |
26 | 26 | </div> |
27 | | - |
| 27 | + <!-- Loading Spinner (Hidden by default) --> |
| 28 | + <div class="spinner-border mx-2" role="status" style="display: none;"> |
| 29 | + <span class="visually-hidden">Loading...</span> |
| 30 | + </div> |
28 | 31 | </div> |
29 | 32 | </div> |
30 | 33 |
|
|
33 | 36 | <!-- Image Display Section --> |
34 | 37 | <div class="container mt-2 d-flex justify-content-center px-1"> |
35 | 38 | <img id="image-display" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="> |
| 39 | + <!-- Overlay for click detection --> |
| 40 | + <!-- <div class="overlay" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; cursor: pointer;"></div> --> |
36 | 41 | </div> |
37 | 42 |
|
38 | 43 | <!-- Pagination Section --> |
|
71 | 76 | const prevBtn = document.getElementById('prev-btn'); |
72 | 77 | const nextBtn = document.getElementById('next-btn'); |
73 | 78 | const lastBtn = document.getElementById('last-btn'); |
74 | | - |
| 79 | + |
75 | 80 | let currentPage = 1; |
76 | 81 | let maxPage = 1; |
77 | 82 | let baseUrl = ''; |
78 | 83 | let extension = ''; |
79 | | - |
| 84 | + |
| 85 | + // Event listener for clicks on the overlay |
| 86 | + imageDisplay.addEventListener('click', function(event) { |
| 87 | + const imageWidth = imageDisplay.offsetWidth; |
| 88 | + |
| 89 | + // Calculate the click position relative to the image width |
| 90 | + const clickPosition = event.clientX - imageDisplay.getBoundingClientRect().left; |
| 91 | + |
| 92 | + // If click is in the left 20% of the image, go to the previous page |
| 93 | + if (clickPosition < imageWidth * 0.2) { |
| 94 | + navigateToPage(currentPage - 1); // Handle navigation to the previous page |
| 95 | + } |
| 96 | + // If click is in the right 80% of the image, go to the next page |
| 97 | + else if (clickPosition > imageWidth * 0.2) { |
| 98 | + navigateToPage(currentPage + 1); // Handle navigation to the next page |
| 99 | + } |
| 100 | + }); |
| 101 | + |
80 | 102 | function parseUrl(url) { |
81 | 103 | const match = url.match(/(\d+)(\.\w+)$/); |
82 | 104 | if (!match) return null; |
|
91 | 113 | pageDisplay.textContent = `${currentPage} / ${maxPage}`; |
92 | 114 | } |
93 | 115 |
|
94 | | - function navigateToPage(targetPage) { |
| 116 | + async function navigateToPage(targetPage) { |
95 | 117 | if (targetPage < 1) { |
96 | | - alert('Already at the first page'); |
97 | 118 | return; |
98 | 119 | } |
99 | | - |
| 120 | + |
100 | 121 | const url = `${baseUrl}${targetPage}${extension}`; |
101 | | - if(targetPage <= maxPage){ |
102 | | - currentPage = targetPage |
| 122 | + |
| 123 | + // Show the loading spinner before the image starts loading |
| 124 | + const spinner = document.querySelector('.spinner-border'); |
| 125 | + spinner.style.display = 'inline-block'; // Show the spinner |
| 126 | + |
| 127 | + if (targetPage <= maxPage) { |
| 128 | + currentPage = targetPage; |
103 | 129 | imageDisplay.src = url; |
104 | 130 | updatePageDisplay(); |
105 | 131 | } else { |
106 | | - const testImage = new Image(); |
107 | | - |
108 | | - testImage.onload = () => { |
| 132 | + try { |
| 133 | + // Create a new image instance to check if the image exists |
| 134 | + const testImage = new Image(); |
| 135 | + |
| 136 | + // Wait for the image to load |
| 137 | + await loadImage(testImage, url); |
| 138 | + |
109 | 139 | currentPage = targetPage; |
110 | 140 | if (targetPage > maxPage) maxPage = targetPage; |
| 141 | + |
| 142 | + // Image is loaded, update the image display |
111 | 143 | imageDisplay.src = url; |
112 | 144 | updatePageDisplay(); |
113 | | - }; |
114 | | - |
115 | | - testImage.onerror = () => { |
| 145 | + } catch (error) { |
116 | 146 | alert('Page does not exist'); |
117 | | - }; |
118 | | - |
119 | | - testImage.src = url; |
| 147 | + } |
120 | 148 | } |
121 | | - |
122 | | - |
123 | 149 |
|
| 150 | + // Hide the spinner after the image has loaded |
| 151 | + spinner.style.display = 'none'; // Hide the spinner |
| 152 | + } |
| 153 | + |
| 154 | + // Helper function to return a Promise for image loading |
| 155 | + function loadImage(img, src) { |
| 156 | + return new Promise((resolve, reject) => { |
| 157 | + img.onload = resolve; // Resolve the promise when the image loads |
| 158 | + img.onerror = reject; // Reject the promise if the image fails to load |
| 159 | + img.src = src; |
| 160 | + }); |
124 | 161 | } |
125 | 162 |
|
126 | 163 | viewImageBtn.addEventListener('click', () => { |
|
150 | 187 | prevBtn.addEventListener('click', () => navigateToPage(currentPage - 1)); |
151 | 188 | nextBtn.addEventListener('click', () => navigateToPage(currentPage + 1)); |
152 | 189 | lastBtn.addEventListener('click', () => navigateToPage(maxPage)); |
| 190 | + |
| 191 | + // Event listener for clicks on the overlay |
| 192 | + overlay.addEventListener('click', function(event) { |
| 193 | + // Get the width of the image |
| 194 | + const imageWidth = imageDisplay.offsetWidth; |
| 195 | + |
| 196 | + // Calculate the click position relative to the image width |
| 197 | + const clickPosition = event.clientX - imageDisplay.getBoundingClientRect().left; |
| 198 | + |
| 199 | + // If click is in the left 20% of the image, go to the previous page |
| 200 | + if (clickPosition < imageWidth * 0.2) { |
| 201 | + navigateToPage(currentPage - 1); // Handle navigation to the previous page |
| 202 | + } |
| 203 | + // If click is in the right 80% of the image, go to the next page |
| 204 | + else if (clickPosition > imageWidth * 0.2) { |
| 205 | + navigateToPage(currentPage + 1); // Handle navigation to the next page |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + |
153 | 210 | }); |
154 | 211 | </script> |
155 | 212 |
|
|
0 commit comments