-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGyazoDirectLink.user.js
More file actions
238 lines (205 loc) · 8.11 KB
/
GyazoDirectLink.user.js
File metadata and controls
238 lines (205 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// ==UserScript==
// @name Gyazo Gif and Video Direct Link Button
// @namespace nick2bad4u.github.io
// @version 3.7
// @description Adds a link button to redirect to the direct video or gif link on Gyazo
// @author Nick2bad4u
// @license UnLicense
// @homepageURL https://github.com/Nick2bad4u/UserStyles
// @homepage https://github.com/Nick2bad4u/UserStyles
// @supportURL https://github.com/Nick2bad4u/UserStyles/issues
// @match https://gyazo.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=gyazo.com
// @grant none
// @tag gyazo
// @downloadURL https://update.greasyfork.org/scripts/521788/Gyazo%20Gif%20and%20Video%20Direct%20Link%20Button.user.js
// @updateURL https://update.greasyfork.org/scripts/521788/Gyazo%20Gif%20and%20Video%20Direct%20Link%20Button.meta.js
// ==/UserScript==
(function () {
'use strict';
function createButtonElement() {
console.log('Creating button element');
const button = document.createElement('button');
button.id = 'direct-video-link-button';
button.classList.add('btn', 'explorer-action-btn', 'explorer-action-btn-dark');
button.setAttribute('data-tooltip-content', 'Direct Link');
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', '24');
svg.setAttribute('height', '24');
svg.setAttribute('viewBox', '0 0 24 24');
svg.setAttribute('class', 'icon-link');
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
path.setAttribute(
'd',
'M3.9 11.1c-.4.4-.4 1 0 1.4l6.6 6.6c.4.4 1 .4 1.4 0l3.6-3.6c.4-.4.4-1 0-1.4-.4-.4-1-.4-1.4 0L10 16.6l-5.3-5.3 2.5-2.5c.4-.4.4-1 0-1.4-.4-.4-1-.4-1.4 0l-3.6 3.6zM20.1 3.9c-.4-.4-1-.4-1.4 0l-3.6 3.6c-.4.4-.4 1 0 1.4.4.4 1 .4 1.4 0l2.5-2.5L14 10l-6.6-6.6c-.4-.4-1-.4-1.4 0l-3.6 3.6c-.4.4-.4 1 0 1.4.4.4 1 .4 1.4 0L10 3.4l5.3 5.3-2.5 2.5c-.4.4-.4 1 0 1.4.4.4 1 .4 1.4 0l3.6-3.6c.4-.4.4-1 0-1.4L13.6 2c-.4-.4-1-.4-1.4 0L4.6 9.6c-.4.4-.4 1 0 1.4.4.4 1 .4 1.4 0l5.3-5.3L14 10l6.6-6.6c.4-.4.4-1 0-1.4z',
);
svg.appendChild(path);
button.appendChild(svg);
console.log('Button element created', button);
return button;
}
function createTooltipElement() {
console.log('Creating tooltip element');
const existingTooltip = document.getElementById('tooltip-direct-video-link-button');
if (existingTooltip) {
console.log('Existing tooltip found, removing it');
existingTooltip.remove();
}
const tooltip = document.createElement('div');
tooltip.id = 'tooltip-direct-video-link-button';
tooltip.setAttribute('role', 'tooltip');
tooltip.classList.add(
'react-tooltip',
'core-styles-module_tooltip__3vRRp',
'styles-module_tooltip__mnnfp',
'styles-module_dark__xNqje',
'react-tooltip__place-bottom',
'core-styles-module_show__Nt9eE',
'react-tooltip__show',
);
tooltip.style.zIndex = '2147483647';
tooltip.style.fontSize = '14px';
tooltip.style.padding = '6px 10px';
tooltip.style.maxWidth = '250px';
tooltip.style.display = 'none';
tooltip.style.position = 'absolute';
tooltip.innerHTML =
'Direct Link<div class="react-tooltip-arrow core-styles-module_arrow__cvMwQ styles-module_arrow__K0L3T" style="left: 38px; top: -4px;"></div>';
document.body.appendChild(tooltip);
console.log('Tooltip element created', tooltip);
return tooltip;
}
function addTooltipListeners(button, tooltip) {
console.log('Adding tooltip listeners');
button.onmouseover = function () {
console.log('Mouseover event on button');
const rect = button.getBoundingClientRect();
tooltip.style.left = rect.left + window.scrollX - 18.9453 + 'px';
tooltip.style.top = rect.top + window.scrollY + 42 + 'px';
tooltip.style.display = 'block';
console.log('Tooltip displayed');
};
button.onmouseout = function () {
console.log('Mouseout event on button');
tooltip.style.display = 'none';
console.log('Tooltip hidden');
};
console.log('Tooltip listeners added');
}
function extractDirectLink() {
console.log('Extracting direct link');
let directLink;
const imgElement = document.querySelector('img.image-viewer');
if (imgElement) {
directLink = imgElement.src;
console.log('Direct link extracted from image', directLink);
} else {
const sourceElement = document.querySelector('#gyazo-video-player > video > source');
directLink = sourceElement ? sourceElement.src : '#';
console.log('Direct link extracted from video source', directLink);
}
return directLink;
}
function addRedirectButton() {
// Check if the button already exists
if (document.getElementById('direct-video-link-button')) {
console.log('Redirect button already exists, skipping creation');
return;
}
console.log('Adding redirect button');
let targetElement = null;
let attempts = 0;
const maxAttempts = 10;
const interval = 500;
const findTargetElement = () => {
// Check again for existing button before retrying
if (document.getElementById('direct-video-link-button')) {
console.log('Redirect button already exists during retry, skipping creation');
return;
}
targetElement = document.querySelector(
'#react-root > div.header-block.explorer-header-block > div.explorer-action-btn-toolbar > div.explorer-action-btn-group',
);
if (targetElement) {
console.log('Target element found');
const button = createButtonElement();
const tooltip = createTooltipElement();
addTooltipListeners(button, tooltip);
button.onclick = function () {
const directLink = extractDirectLink();
console.log('Button clicked, redirecting to', directLink);
window.location.href = directLink;
};
targetElement.insertAdjacentElement('beforebegin', button);
console.log('Redirect button added');
} else if (attempts < maxAttempts) {
attempts++;
console.log(`Target element not found, retrying in ${interval}ms (attempt ${attempts}/${maxAttempts})`);
setTimeout(findTargetElement, interval);
} else {
console.log('Target element not found after multiple attempts, exiting');
}
};
findTargetElement();
}
// Removes the redirect button and tooltip from the DOM if they exist
function removeRedirectButton() {
console.log('Removing redirect button if it exists');
const existingButton = document.getElementById('direct-video-link-button');
if (existingButton) {
console.log('Existing button found, removing it');
existingButton.remove();
}
const existingTooltip = document.getElementById('tooltip-direct-video-link-button');
if (existingTooltip) {
console.log('Existing tooltip found, removing it');
existingTooltip.remove();
}
console.log('Redirect button and tooltip removed');
}
function handlePageChange() {
console.log('Handling page change');
const currentUrl = location.href;
if (currentUrl.includes('https://gyazo.com/captures')) {
removeRedirectButton();
console.log('On captures page, not adding button');
} else {
addRedirectButton();
}
}
function initialize() {
console.log('Initializing script');
// Remove any existing button and tooltip to prevent duplicates
removeRedirectButton();
handlePageChange();
// Store the observer globally for cleanup
window.scriptObserver = new MutationObserver(() => {
const currentUrl = location.href;
if (currentUrl !== window.scriptObserver.lastUrl) {
console.log('URL changed from', window.scriptObserver.lastUrl, 'to', currentUrl);
window.scriptObserver.lastUrl = currentUrl;
// Remove any existing button and tooltip before handling the page change
removeRedirectButton();
handlePageChange();
}
});
window.scriptObserver.lastUrl = location.href;
window.scriptObserver.observe(document.body, {
childList: true,
subtree: true,
});
console.log('Mutation observer added');
}
window.addEventListener('load', initialize);
window.addEventListener('popstate', () => {
console.log('A soft navigation has been detected:', location.href);
// Cleanup existing observers and buttons
if (window.scriptObserver) {
console.log('Disconnecting existing mutation observer');
window.scriptObserver.disconnect();
}
removeRedirectButton();
// Reinitialize the script
initialize();
});
})();