Skip to content

Commit da1b83d

Browse files
committed
feat(GyazoDirectLink): bump version to 3.2 and improve direct link extraction and mutation observer handling
1 parent d4aeb97 commit da1b83d

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

GyazoDirectLink.user.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name Gyazo Gif and Video Direct Link Button
33
// @namespace typpi.online
4-
// @version 3.1
4+
// @version 3.2
55
// @description Adds a link button to redirect to the direct video or gif link on Gyazo
66
// @author Nick2bad4u
77
// @license UnLicense
@@ -125,8 +125,8 @@
125125

126126
addTooltipListeners(button, tooltip);
127127

128-
const directLink = extractDirectLink();
129128
button.onclick = function () {
129+
const directLink = extractDirectLink();
130130
console.log('Button clicked, redirecting to', directLink);
131131
window.location.href = directLink;
132132
};
@@ -175,17 +175,18 @@
175175
console.log('Initializing script');
176176
handlePageChange();
177177

178-
const observer = new MutationObserver(() => {
178+
// Store the observer globally for cleanup
179+
window.scriptObserver = new MutationObserver(() => {
179180
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;
183184
handlePageChange();
184185
}
185186
});
186187

187-
observer.lastUrl = location.href;
188-
observer.observe(document.body, {
188+
window.scriptObserver.lastUrl = location.href;
189+
window.scriptObserver.observe(document.body, {
189190
childList: true,
190191
subtree: true,
191192
});
@@ -195,6 +196,15 @@
195196
window.addEventListener('load', initialize);
196197
window.addEventListener('popstate', () => {
197198
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
198208
initialize();
199209
});
200210
})();

0 commit comments

Comments
 (0)