-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
92 lines (83 loc) · 3.22 KB
/
Copy pathscraper.js
File metadata and controls
92 lines (83 loc) · 3.22 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
// 🟢 This script runs inside the active webpage when the user clicks the extension icon.
console.log('🟢 Scraper.js is running!');
// 📸 Function to find and extract all image URLs from the page
function getImages() {
// ⭕ Create a Set to store unique image URLs (prevents duplicates)
console.log('⭕ Creating a New Image Set.');
const imageUrls = new Set();
const svgElements = [] // SVGs need to be converted before saving
const validImageExtensions = [
'.jpg',
'.jpeg',
'.png',
'.gif',
'.webp',
'.bmp',
'.svg',
]; // ✅ List of allowed image types
// 🔎 Select all <img> elements on the current webpage
document.querySelectorAll('img').forEach((img) => {
let imageUrl = img.src; // Default to the standard `src` attribute
// 🖼️ Check if `srcset` exists (it may contain a higher-resolution version)
if (img.srcset) {
const srcsetArray = img.srcset
.split(',')
.map((entry) => entry.trim().split(' ')[0]);
imageUrl = srcsetArray[srcsetArray.length - 1]; // Get the highest-resolution image
console.log(`🔍 Found high-res in srcset: ${imageUrl}`);
}
// 🔎 Check for `data-src` or `data-large-src` (commonly used for lazy loading)
if (img.dataset.src) {
imageUrl = img.dataset.src;
console.log(`🛠️ Found full-size in data-src: ${imageUrl}`);
}
// ⚠️ RISK OF DUPLICATION
if (img.dataset.largeSrc) {
imageUrl = img.dataset.largeSrc;
console.log(`🛠️ Found full-size in data-large-src: ${imageUrl}`);
}
// 🔗 If the image is inside an <a> tag that links to a larger version, grab that link
const parentLink = img.closest('a');
if (
parentLink &&
parentLink.href &&
!parentLink.href.includes('javascript')
) {
imageUrl = parentLink.href;
console.log(`🔗 Found full-size image from link: ${imageUrl}`);
}
if (
imageUrl &&
validImageExtensions.some((ext) =>
imageUrl.toLocaleLowerCase().includes(ext)
)
) {
console.log(`🖼️ Found image: ${img.src}`); // Log each image found
imageUrls.add(imageUrl); // ✅ Add the image URL to the Set (duplicates are ignored)
} else {
console.warn(`⚠️ Skipped non-image URL: ${imageUrl}`);
}
});
// 📤 If images were found, send them to the background script for downloading
if (imageUrls.size > 0) {
console.log('📨 Sending unique image URLs to background script...');
chrome.runtime.sendMessage(
{ action: 'download_images', imageUrls: Array.from(imageUrls) }, // Convert Set to Array
(response) => {
// ❗ Handle errors when sending the message to `background.js`
if (chrome.runtime.lastError) {
console.error(
'❌ Error sending message to background script:',
chrome.runtime.lastError.message
);
} else {
console.log('📩 Response from background:', response); // ✅ Log response from background.js
}
}
);
} else {
console.warn('⚠️ No images found on this page.'); // 🚨 Alert if no images were found
}
}
// 🔥 Run the image extraction function immediately when this script is injected into the page
getImages();