forked from Nick2bad4u/UserStyles
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGyazoDirectLink.user.js
More file actions
200 lines (173 loc) · 6.67 KB
/
GyazoDirectLink.user.js
File metadata and controls
200 lines (173 loc) · 6.67 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
// ==UserScript==
// @name Gyazo Gif and Video Direct Link Button
// @namespace typpi.online
// @version 3.1
// @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
// @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
// ==/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 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.cssText = 'z-index: 2147483647; font-size: 14px; padding: 6px 10px; max-width: 250px; display: none; 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() {
removeRedirectButton();
console.log('Adding redirect button');
const existingButton = document.getElementById('direct-video-link-button');
if (existingButton) {
console.log('Existing button found, removing it');
existingButton.remove();
}
let targetElement = null;
let attempts = 0;
const maxAttempts = 10;
const interval = 500;
const findTargetElement = () => {
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);
const directLink = extractDirectLink();
button.onclick = function () {
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();
}
function removeRedirectButton() {
if (!location.href.includes('https://gyazo.com/captures')) {
console.log('Not removing redirect button since not on captures page');
return;
}
console.log('Removing redirect button');
const existingButton = document.getElementById('direct-video-link-button');
if (existingButton !== null) {
console.log('Existing button found, removing it');
existingButton.remove();
}
console.log('Redirect button 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');
handlePageChange();
const observer = new MutationObserver(() => {
const currentUrl = location.href;
if (currentUrl !== observer.lastUrl) {
console.log('URL changed from', observer.lastUrl, 'to', currentUrl);
observer.lastUrl = currentUrl;
handlePageChange();
}
});
observer.lastUrl = location.href;
observer.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);
initialize();
});
})();