|
1 | 1 | // ==UserScript== |
2 | 2 | // @name Gyazo Gif and Video Direct Link Button |
3 | 3 | // @namespace typpi.online |
4 | | -// @version 3.1 |
| 4 | +// @version 3.2 |
5 | 5 | // @description Adds a link button to redirect to the direct video or gif link on Gyazo |
6 | 6 | // @author Nick2bad4u |
7 | 7 | // @license UnLicense |
|
125 | 125 |
|
126 | 126 | addTooltipListeners(button, tooltip); |
127 | 127 |
|
128 | | - const directLink = extractDirectLink(); |
129 | 128 | button.onclick = function () { |
| 129 | + const directLink = extractDirectLink(); |
130 | 130 | console.log('Button clicked, redirecting to', directLink); |
131 | 131 | window.location.href = directLink; |
132 | 132 | }; |
|
175 | 175 | console.log('Initializing script'); |
176 | 176 | handlePageChange(); |
177 | 177 |
|
178 | | - const observer = new MutationObserver(() => { |
| 178 | + // Store the observer globally for cleanup |
| 179 | + window.scriptObserver = new MutationObserver(() => { |
179 | 180 | const currentUrl = location.href; |
180 | | - if (currentUrl !== observer.lastUrl) { |
181 | | - console.log('URL changed from', observer.lastUrl, 'to', currentUrl); |
182 | | - observer.lastUrl = currentUrl; |
| 181 | + if (currentUrl !== window.scriptObserver.lastUrl) { |
| 182 | + console.log('URL changed from', window.scriptObserver.lastUrl, 'to', currentUrl); |
| 183 | + window.scriptObserver.lastUrl = currentUrl; |
183 | 184 | handlePageChange(); |
184 | 185 | } |
185 | 186 | }); |
186 | 187 |
|
187 | | - observer.lastUrl = location.href; |
188 | | - observer.observe(document.body, { |
| 188 | + window.scriptObserver.lastUrl = location.href; |
| 189 | + window.scriptObserver.observe(document.body, { |
189 | 190 | childList: true, |
190 | 191 | subtree: true, |
191 | 192 | }); |
|
195 | 196 | window.addEventListener('load', initialize); |
196 | 197 | window.addEventListener('popstate', () => { |
197 | 198 | console.log('A soft navigation has been detected:', location.href); |
| 199 | + |
| 200 | + // Cleanup existing observers and buttons |
| 201 | + if (window.scriptObserver) { |
| 202 | + console.log('Disconnecting existing mutation observer'); |
| 203 | + window.scriptObserver.disconnect(); |
| 204 | + } |
| 205 | + removeRedirectButton(); |
| 206 | + |
| 207 | + // Reinitialize the script |
198 | 208 | initialize(); |
199 | 209 | }); |
200 | 210 | })(); |
0 commit comments