-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
244 lines (211 loc) · 8.79 KB
/
Copy pathuser.js
File metadata and controls
244 lines (211 loc) · 8.79 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
239
240
241
242
243
244
// ==UserScript==
// @name Image List with Clipboard Copy
// @namespace http://tampermonkey.net/
// @version 1.1.0
// @description Adds a button to list images and copy selected ones to clipboard
// @author errorduplicator
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// State management
const state = {
sidebar: null,
copyButton: null,
initialized: false,
retryCount: 0,
maxRetries: 5,
isVisible: false,
observer: null,
throttleTimeout: null
};
// Throttle function to limit execution
function throttle(func, limit) {
if (state.throttleTimeout) return;
state.throttleTimeout = setTimeout(() => {
func();
state.throttleTimeout = null;
}, limit);
}
// Modified observer setup
function setupObserver() {
if (state.observer) {
state.observer.disconnect();
}
state.observer = new MutationObserver((mutations) => {
throttle(() => {
if (!document.getElementById('image-copy-sidebar')) {
state.initialized = false;
initializeScript();
}
}, 1000);
});
// Observe only body with limited options
state.observer.observe(document.body, {
childList: true,
subtree: false
});
}
// Cleanup function
function cleanup() {
if (state.observer) {
state.observer.disconnect();
}
if (state.throttleTimeout) {
clearTimeout(state.throttleTimeout);
}
}
// Add cleanup on page unload
window.addEventListener('unload', cleanup);
function createSidebar() {
try {
const sidebar = document.createElement('div');
sidebar.id = 'image-copy-sidebar';
sidebar.style.position = 'fixed';
sidebar.style.right = '-200px'; // Start hidden
sidebar.style.top = '0';
sidebar.style.width = '200px';
sidebar.style.height = '100%';
sidebar.style.backgroundColor = 'white';
sidebar.style.padding = '10px';
sidebar.style.overflowY = 'auto';
sidebar.style.zIndex = '10000';
sidebar.style.transition = 'right 0.3s ease-in-out';
document.body.appendChild(sidebar);
// Create toggle button
const toggleButton = document.createElement('button');
toggleButton.textContent = '≡';
toggleButton.style.position = 'fixed';
toggleButton.style.right = '0';
toggleButton.style.top = '0';
toggleButton.style.zIndex = '10001';
toggleButton.style.padding = '5px 10px';
document.body.appendChild(toggleButton);
toggleButton.addEventListener('click', () => {
state.isVisible = !state.isVisible;
sidebar.style.right = state.isVisible ? '0' : '-200px';
});
const copyButton = document.createElement('button');
copyButton.textContent = 'Copy Selected';
copyButton.style.width = '100%';
copyButton.style.margin = '10px 0';
copyButton.addEventListener('click', () => {
const selectedImages = [];
const checkboxes = state.sidebar.querySelectorAll('input[type="checkbox"]:checked');
checkboxes.forEach(checkbox => {
const img = checkbox.nextElementSibling;
selectedImages.push(img);
});
if (selectedImages.length > 0) {
const container = document.createElement('div');
selectedImages.forEach(img => {
const imgClone = img.cloneNode();
container.appendChild(imgClone);
});
document.body.appendChild(container);
const range = document.createRange();
range.selectNodeContents(container);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
try {
document.execCommand('copy');
const notification = document.createElement('div');
notification.textContent = 'Selected images copied to clipboard!';
notification.style.position = 'fixed';
notification.style.bottom = '10px';
notification.style.right = '10px';
notification.style.backgroundColor = 'green';
notification.style.color = 'white';
notification.style.padding = '10px';
notification.style.borderRadius = '5px';
notification.style.zIndex = '10002';
document.body.appendChild(notification);
setTimeout(() => {
document.body.removeChild(notification);
}, 3000);
} catch (err) {
console.error('Failed to copy images: ', err);
const notification = document.createElement('div');
notification.textContent = 'Failed to copy images.';
notification.style.position = 'fixed';
notification.style.bottom = '10px';
notification.style.right = '10px';
notification.style.backgroundColor = 'red';
notification.style.color = 'white';
notification.style.padding = '10px';
notification.style.borderRadius = '5px';
notification.style.zIndex = '10002';
document.body.appendChild(notification);
setTimeout(() => {
document.body.removeChild(notification);
}, 3000);
}
document.body.removeChild(container);
} else {
alert('No images selected.');
}
});
sidebar.appendChild(copyButton);
state.sidebar = sidebar;
state.copyButton = copyButton;
state.initialized = true;
return sidebar;
} catch (error) {
console.error('Error creating sidebar:', error);
return null;
}
}
function scanImages() {
if (!state.sidebar) return;
// Clear existing image containers
const containers = state.sidebar.querySelectorAll('div');
containers.forEach(container => {
if (container !== state.copyButton) {
container.remove();
}
});
// Scan for images
const images = Array.from(document.getElementsByTagName('img'))
.filter(img => img.src && img.width > 10 && img.height > 10);
// Create containers for found images
images.forEach((img, index) => {
const container = document.createElement('div');
container.style.display = 'flex';
container.style.alignItems = 'center';
container.style.margin = '10px';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `img-checkbox-${index}`;
container.appendChild(checkbox);
const imageClone = img.cloneNode();
imageClone.style.maxWidth = '50px';
imageClone.style.maxHeight = '50px';
imageClone.style.marginLeft = '10px';
container.appendChild(imageClone);
state.sidebar.appendChild(container);
});
console.log(`Found ${images.length} images`);
}
function createRefreshButton() {
const refreshButton = document.createElement('button');
refreshButton.textContent = 'Refresh Images';
refreshButton.style.width = '100%';
refreshButton.style.margin = '10px 0';
refreshButton.addEventListener('click', scanImages);
state.sidebar.insertBefore(refreshButton, state.sidebar.firstChild);
}
function initializeScript() {
if (state.initialized) return;
const sidebar = createSidebar();
if (!sidebar) return;
createRefreshButton();
// Delay initial scan
setTimeout(scanImages, 1000);
state.initialized = true;
}
// Initial setup with delay
setTimeout(initializeScript, 500);
setupObserver();
})();