-
Notifications
You must be signed in to change notification settings - Fork 0
/
five-hours.js
259 lines (203 loc) · 8.39 KB
/
five-hours.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const name = "callionica.copy-clicked-images";
globalThis[name] = function copyClickedImages() {
function computedStyle(element) {
return document.defaultView.getComputedStyle(element);
}
function leaves(elements) {
const leaves = [];
const ancestors = new Set();
for (const element of elements) {
if (!ancestors.has(element)) {
leaves.push(element);
pathToRoot(element).forEach(element => ancestors.add(element));
}
}
return leaves;
}
/**
* Extracts the first URL from a style such as background-image
* @param { string } text
* @returns { string | undefined }
*/
function firstUrlFromStyle(text) {
if (text == undefined) {
return undefined;
}
const re = /url[(]['"](?<url>[^'"]*)['"][)]/i;
return re.exec(text)?.groups?.url ?? undefined;
}
/**
* Returns the URL of the first image in the list of elements
* @param {HTMLElement[]} elements
* @returns { string }
*/
function firstImage(elements) {
for (const element of elements) {
if (element.tagName === "IMG" && element.currentSrc) {
return element.currentSrc;
}
const style = computedStyle(element);
if (element.tagName === "svg") { // yes, lowercase
// console.log("fill", style.fill);
// console.log("stroke", style.stroke);
// const blob = new Blob([element.outerHTML], { type: "image/svg+xml" });
// const url = URL.createObjectURL(blob);
// return url.toString();
return `data:image/svg+xml;utf8,${encodeURIComponent(element.outerHTML)}`;
}
const bg = style.backgroundImage;
const img = firstUrlFromStyle(bg);
if (img !== undefined) {
return img;
}
}
for (const element of elements) {
const es = [...element.querySelectorAll("img")].filter(e => e.currentSrc);
if (es[0] !== undefined) {
return es[0].currentSrc;
}
}
}
// Returns the result of calling the fn with the elements that the user clicks on.
// We use a callback here specifically to give the caller a chance to execute code
// within the context of the event handler since stuff like clipboard access, audio,
// and other features are only available in user event handlers
/**
* @template T
* @param { (clickedElements: HTMLElement[])=>T } fn
*/
async function getClickedElements(fn) {
const timeout = async ms => new Promise(res => setTimeout(res, ms));
/** @type { HTMLElement[] } */
let clickedElements = null;
/** @type { T } */
let result = null;
// Create an overlay to capture clicks
const overlay = document.createElement("div");
overlay.style = "height: 100vh; width: 100vw; position: fixed; top: 0; left: 0; margin: 0; padding: 0; border: 0; opacity: 0.2; background-color: black; z-index: 999; cursor: pointer;";
const instructions = document.createElement("div");
instructions.style = "height: 100vh; width: 100vw; position: fixed; top: 0; left: 0; margin: 0; padding: 0; border: 0; z-index: 999; cursor: pointer; font-size: 64px; transition: all 0.3s linear;";
instructions.innerHTML = `
<p style="width: 100%; height: 100%; line-height: 2; text-align: center; margin-top: 10vh; padding: 0; border: 0; color: black; background-color: rgba(255, 255, 255, 0.5);">
CLICK IMAGE TO COPY URL
</p>
`;
document.body.append(overlay, instructions);
setTimeout(() => instructions.style.opacity = "0", 1500);
function onclick(evt) {
evt.stopImmediatePropagation();
evt.stopPropagation();
evt.preventDefault();
const tap = document.createElement("div");
const rect = overlay.getBoundingClientRect();
// console.log(rect);
const width = 64;
const height = 64;
const x = evt.clientX - width/2;
const y = evt.clientY - height/2;
tap.style = `height: ${height}px; width: ${width}px; position: fixed; top: ${y}px; left: ${x}px; background-color: white; border-radius: 50%; transition: all 0.3s linear;`;
setTimeout(() => {
tap.style.transform = "scale(2.0)";
tap.style.opacity = "0";
}, 0);
overlay.append(tap);
setTimeout(() => tap.remove(), 1000);
const foundElements = [...document.elementsFromPoint(evt.clientX, evt.clientY)].filter(e => e !== overlay);
result = fn(foundElements)
clickedElements = foundElements;
}
const event = "onclick";
const old = document[event];
document[event] = onclick;
try {
while (!clickedElements) {
await timeout(100);
}
instructions.remove();
setTimeout(() => overlay.remove(), 350);
} finally {
document[event] = old;
}
return result;
}
/**
*
* @param { HTMLElement[] } clickedElements
*/
function getImages(clickedElements) {
// console.log("clickedElements", clickedElements);
const first = firstImage(clickedElements);
// console.log("first", first);
return [first];
const leafElements = leaves(clickedElements);
// console.log("leafElements", leafElements);
/** @type { string[] } */
let images = [];
/**
* Returns the URLs of any img elements in the collection.
* If no valid URLs are found, returns the URLs of any background-images applied to the elements.
*
* @param { HTMLElement[] } elements
*/
function imgElementsOrBackgroundImages(elements) {
// Prefer grabbing IMG elements that have been clicked on
/** @type { HTMLImageElement[] } */
const imageElements = elements.filter(e => e.tagName === "IMG");
let images = imageElements.map(img => img.currentSrc).filter(url => url);
// If none of those, pull URLs from the background-image style
if (images.length === 0) {
images = elements.map(element => computedStyle(element).backgroundImage).filter(img => img && img.startsWith(`url("`) && img.endsWith(`")`)).map(img => img.substring(5, img.length - 2));
}
return images;
}
images = imgElementsOrBackgroundImages(leafElements);
// If none of those, check whether the leaf elements contain any IMG elements
if (images.length === 0) {
images = leafElements.flatMap(element => [...element.querySelectorAll("img")].map(img => img.currentSrc)).filter(url => url);
}
// If none of those, check entire tree
if (images.length === 0) {
images = imgElementsOrBackgroundImages(clickedElements);
}
// Ensure uniqueness while maintaining the order
return [...new Set(images)];
}
function pathToRoot(node) {
const nodes = [];
while (node && node.nodeName !== "#document") {
nodes.push(node);
node = node.parentNode;
}
return nodes
}
function commonPathToRoot(node, path) {
if (path === undefined) {
return pathToRoot(node);
}
if (path.length === 0) {
return path;
}
const set = new Set(pathToRoot(node));
let index = path.length;
for (let i = 0; i < path.length; ++i) {
if (set.has(path[i])) {
index = i;
break;
}
}
return path.slice(index);
}
function _commonPath(items) {
return items.reduce((path, node) => commonPathToRoot(node, path), undefined);
}
async function main() {
const images = await getClickedElements(elements => {
const images = getImages(elements);
navigator.clipboard.writeText(images.join("\n"));
return images;
});
return images;
}
main().then(images => console.log(images));
};
globalThis[name]();