|
1 | 1 | // ==UserScript== |
2 | 2 | // @name Gyazo Gif and Video Direct Link Button |
3 | 3 | // @namespace typpi.online |
4 | | -// @version 3.3 |
| 4 | +// @version 3.4 |
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 |
|
109 | 109 | return directLink; |
110 | 110 | } |
111 | 111 |
|
112 | | - let isButtonAdded = false; // Global flag to track button addition |
113 | | - |
114 | 112 | function addRedirectButton() { |
115 | | - // Check if the button already exists or has been added |
116 | | - if (isButtonAdded || document.getElementById('direct-video-link-button')) { |
117 | | - console.log('Redirect button already exists or has been added, skipping creation'); |
| 113 | + // Check if the button already exists |
| 114 | + if (document.getElementById('direct-video-link-button')) { |
| 115 | + console.log('Redirect button already exists, skipping creation'); |
118 | 116 | return; |
119 | 117 | } |
120 | 118 |
|
121 | 119 | console.log('Adding redirect button'); |
122 | | - isButtonAdded = true; // Set the flag to true to prevent duplicate calls |
123 | 120 |
|
124 | 121 | let targetElement = null; |
125 | 122 | let attempts = 0; |
126 | 123 | const maxAttempts = 10; |
127 | 124 | const interval = 500; |
128 | 125 |
|
129 | 126 | const findTargetElement = () => { |
| 127 | + // Check again for existing button before retrying |
| 128 | + if (document.getElementById('direct-video-link-button')) { |
| 129 | + console.log('Redirect button already exists during retry, skipping creation'); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
130 | 133 | targetElement = document.querySelector( |
131 | 134 | '#react-root > div.header-block.explorer-header-block > div.explorer-action-btn-toolbar > div.explorer-action-btn-group', |
132 | 135 | ); |
|
159 | 162 | } |
160 | 163 |
|
161 | 164 | function removeRedirectButton() { |
162 | | - // Reset the global flag when removing the button |
163 | | - isButtonAdded = false; |
164 | | - |
165 | 165 | if (!location.href.includes('https://gyazo.com/captures')) { |
166 | 166 | console.log('Not removing redirect button since not on captures page'); |
167 | 167 | return; |
|
189 | 189 |
|
190 | 190 | function initialize() { |
191 | 191 | console.log('Initializing script'); |
| 192 | + |
| 193 | + // Remove any existing button to prevent duplicates |
| 194 | + removeRedirectButton(); |
| 195 | + |
192 | 196 | handlePageChange(); |
193 | 197 |
|
194 | 198 | // Store the observer globally for cleanup |
|
197 | 201 | if (currentUrl !== window.scriptObserver.lastUrl) { |
198 | 202 | console.log('URL changed from', window.scriptObserver.lastUrl, 'to', currentUrl); |
199 | 203 | window.scriptObserver.lastUrl = currentUrl; |
| 204 | + |
| 205 | + // Remove any existing button before handling the page change |
| 206 | + removeRedirectButton(); |
200 | 207 | handlePageChange(); |
201 | 208 | } |
202 | 209 | }); |
|
0 commit comments