-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
317 lines (274 loc) · 11 KB
/
content.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Function to check if an image is an Unsplash image using multiple detection strategies
function isUnsplashImage(img) {
// Strategy 1: Check srcset URL pattern
const srcset = img.getAttribute("srcset");
if (!srcset) return false;
const baseUrl = extractBaseUrl(srcset);
if (
!baseUrl.startsWith("https://images.unsplash.com/") ||
baseUrl.includes("/profile-")
) {
return false;
}
// Strategy 2: Check if image is in a typical Unsplash photo container
const figure = img.closest("figure");
if (figure) {
// Check for typical Unsplash photo container structure
const hasPhotoLinks =
figure.querySelectorAll('a[href*="/photos/"]').length > 0;
const hasDownloadButton =
figure.querySelectorAll('a[href*="download"]').length > 0;
if (hasPhotoLinks || hasDownloadButton) return true;
}
// Strategy 3: Check image attributes and properties
const imgWidth = img.naturalWidth || img.width;
const imgHeight = img.naturalHeight || img.height;
// Unsplash images are typically high quality and larger than profile pictures
if (imgWidth > 300 && imgHeight > 300) {
// Check if image has typical Unsplash photo attributes
const hasUnsplashAttributes =
img.getAttribute("alt")?.toLowerCase().includes("photo") ||
img.getAttribute("data-test")?.includes("photo") ||
img.getAttribute("loading") === "lazy";
if (hasUnsplashAttributes) return true;
}
// Strategy 4: Legacy class-based detection (as fallback)
return (
img.classList.contains("DVW3V") ||
img.classList.contains("I7OuT") ||
img.classList.contains("L1BOa")
);
}
// Function to safely add the copy button using Shadow DOM
function addCopyButton(img) {
// Skip if already processed
if (img.classList.contains("SqNWg")) return;
// Use the new detection function
if (!isUnsplashImage(img)) return;
// Ensure we don't duplicate the button
if (img.parentElement.querySelector(".unsplash-copy-button-container"))
return;
// Extract the base URL from the srcset attribute
const srcset = img.getAttribute("srcset");
const baseUrl = extractBaseUrl(srcset);
// Check if the URL matches the desired pattern and is not a profile image
if (
!baseUrl.startsWith("https://images.unsplash.com/") ||
baseUrl.includes("/profile-")
)
return;
// Create a container for the shadow DOM
const shadowContainer = document.createElement("div");
shadowContainer.className = "unsplash-copy-button-container";
shadowContainer.style.position = "absolute";
shadowContainer.style.top = "10px"; // Position at the top
shadowContainer.style.left = "2%"; // Center horizontally
shadowContainer.style.zIndex = "1"; // High z-index to ensure it's above other elements
shadowContainer.style.pointerEvents = "auto"; // Ensure the button can be clicked
// Create the shadow root
const shadowRoot = shadowContainer.attachShadow({ mode: "open" });
// Add hover effect using shadow DOM CSS
const style = document.createElement("style");
style.textContent = `
.unsplash-copy-button {
position: relative;
z-index: 1;
background-color: rgba(0, 0, 0, 0.4);
color: #fff;
border: 1px solid #e1e1e1;
padding: 5px 10px;
font-size: 12px;
cursor: pointer;
border-radius: 4px;
transition: background-color 0.3s, color 0.3s;
margin-bottom: 5px;
}
.unsplash-copy-button:hover {
background-color: #3B136C; /* Change background on hover */
color: #fff; /* Change text color on hover */
}
`;
shadowRoot.appendChild(style);
// Create a button element inside the shadow DOM
const copyButton = document.createElement("button");
copyButton.textContent = "Copy Link";
copyButton.className = "unsplash-copy-button"; // Apply the class to the button
// Create a button element inside the shadow DOM
const downloadButton = document.createElement("button");
downloadButton.textContent = "Download";
downloadButton.className = "unsplash-copy-button"; // Apply the class to the button
const br = document.createElement("br");
// Append the button to the shadow DOM
shadowRoot.appendChild(copyButton);
shadowRoot.appendChild(br);
shadowRoot.appendChild(downloadButton);
// Copy URL to clipboard on button click and prevent event propagation and default behavior
copyButton.onclick = (event) => {
event.stopPropagation();
event.preventDefault();
chrome.storage.sync.get(
["width", "height", "quality", "fit", "crop", "customQuery"],
(settings) => {
let baseUrl = extractBaseUrl(srcset);
// Append user settings as query parameters to the URL
const params = new URLSearchParams();
if (settings.width) params.append("w", settings.width);
if (settings.height) params.append("h", settings.height);
if (settings.quality) params.append("q", settings.quality);
if (settings.fit) params.append("fit", settings.fit);
if (settings.crop) params.append("crop", settings.crop);
if (settings.ar) params.append("ar", settings.crop);
// If customQuery exists, parse it and add its key-value pairs to params
if (settings.customQuery) {
const customParams = new URLSearchParams(settings.customQuery);
customParams.forEach((value, key) => {
params.append(key, value);
});
}
if (params.toString()) {
baseUrl += "?" + params.toString();
}
navigator.clipboard.writeText(baseUrl).then(() => {
copyButton.textContent = "Link Copied!";
setTimeout(() => {
copyButton.textContent = "Copy Link";
}, 1000);
});
}
);
};
// Handle "Download Image" button click
downloadButton.onclick = (event) => {
event.stopPropagation();
event.preventDefault();
chrome.storage.sync.get(
["width", "height", "quality", "fit", "crop", "customQuery"],
(settings) => {
let baseUrl = extractBaseUrl(img.getAttribute("srcset"));
// Append user settings as query parameters to the URL
const params = new URLSearchParams();
if (settings.width) params.append("w", settings.width);
if (settings.height) params.append("h", settings.height);
if (settings.quality) params.append("q", settings.quality);
if (settings.fit) params.append("fit", settings.fit);
if (settings.crop) params.append("crop", settings.crop);
if (settings.ar) params.append("ar", settings.crop);
// If customQuery exists, parse it and add its key-value pairs to params
if (settings.customQuery) {
const customParams = new URLSearchParams(settings.customQuery);
customParams.forEach((value, key) => {
params.append(key, value);
});
}
if (params.toString()) {
baseUrl += "?" + params.toString();
}
// Extract image ID for a meaningful filename
const imageIdMatch = baseUrl.match(
/images\.unsplash\.com\/photo\/(\w+)/
);
const imageId = imageIdMatch ? imageIdMatch[1] : "unsplash-image";
// Generate the filename using the new function
const filename = generateFilename(imageId, settings);
// Send a message to the background script to initiate the download
chrome.runtime.sendMessage(
{
action: "downloadImage",
url: baseUrl,
filename: filename,
},
(response) => {
if (response && response.success) {
downloadButton.textContent = "Download Started!";
setTimeout(() => {
downloadButton.textContent = "Download";
}, 1000);
} else {
console.error(`Download failed: ${response.message}`);
downloadButton.textContent = "Download Failed!";
setTimeout(() => {
downloadButton.textContent = "Download";
}, 1000);
}
}
);
}
);
};
// Add shadow container to the image's parent container
const container = img.parentElement;
container.style.position = "relative"; // Ensure the parent is positioned to allow absolute positioning
container.appendChild(shadowContainer);
}
// Function to extract the base URL from the srcset
function extractBaseUrl(srcset) {
const regex = /(https:\/\/images\.unsplash\.com\/[^\s?]+)/;
const match = srcset.match(regex);
return match ? match[1] : ""; // Return the matched base URL or an empty string if not found
}
// MutationObserver to observe for images being added to the DOM
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) {
// Ensure it is an element
if (node.tagName === "IMG" && node.hasAttribute("srcset")) {
addCopyButton(node);
} else {
// If it's not an image, check its children
node.querySelectorAll("img[srcset]").forEach((img) => {
addCopyButton(img);
});
}
}
});
});
});
// Start observing the document body for added nodes
observer.observe(document.body, {
childList: true,
subtree: true,
});
// Initial run to add buttons to already existing images
document.querySelectorAll("img[srcset]").forEach((img) => {
addCopyButton(img);
});
/**
* Generates a sanitized filename incorporating image ID and user-selected parameters.
*
* @param {string} imageId - The unique identifier of the image.
* @param {Object} settings - User-selected settings for customizing the image URL.
* @returns {string} - A sanitized filename string.
*/
function generateFilename(imageId, settings) {
// Function to sanitize filename by removing invalid characters
function sanitizeFilename(name) {
return name.replace(/[^a-z0-9_\-\.]/gi, "_");
}
// Accumulate parameters for filename
let paramsForFilename = [];
if (settings.width) paramsForFilename.push(`w${settings.width}`);
if (settings.height) paramsForFilename.push(`h${settings.height}`);
if (settings.quality) paramsForFilename.push(`q${settings.quality}`);
if (settings.fit) paramsForFilename.push(`fit${settings.fit}`);
if (settings.crop) paramsForFilename.push(`crop${settings.crop}`);
if (settings.ar) paramsForFilename.push(`ar${settings.ar}`);
// Include custom query parameters if they exist
if (settings.customQuery) {
const customParams = new URLSearchParams(settings.customQuery);
customParams.forEach((value, key) => {
// Replace any spaces or special characters in key or value
const safeKey = key.replace(/[^a-z0-9_\-]/gi, "");
const safeValue = value.replace(/[^a-z0-9_\-]/gi, "");
paramsForFilename.push(`${safeKey}${safeValue}`);
});
}
// Join all parameters with a hyphen
const paramsString = paramsForFilename.join("-");
// Construct the final filename
const baseFilename = sanitizeFilename(imageId);
const filename = paramsString
? `${baseFilename}-${paramsString}.jpg`
: `${baseFilename}.jpg`;
return filename;
}