// ==UserScript==
// @name JYNGMN Image Downloader
// @name JYNGMN Image Downloader
// @namespace http://tampermonkey.net/
// @version 1.25
// @description Download images from specified WebURLs as a ZIP file, log image URLs, and add DOWNLOAD buttons correctly, without leaving the page. Compatible with Chrome and Edge browsers, supports multiple pages.
// @author Your Name
// @match https://www.xinggan5.top/*
// @connect *
// @grant GM_xmlhttpRequest
// @grant GM_download
// ==/UserScript==
(function () {
'use strict';
code
Assistant
// Logging to indicate script initialization
console.log('WebURL Image Downloader script initializing...');
// Utility function to convert relative URLs to absolute URLs
function makeAbsoluteURL(url, base) {
try {
return new URL(url, base || window.location.origin).href;
} catch (error) {
console.error(`Invalid URL: ${url} - Error: ${error.message}`);
return url;
}
}
// Function to inject external scripts, such as JSZip
async function injectScript(url) {
return new Promise((resolve, reject) => {
if (document.querySelector(`script[src="${url}"]`)) {
console.log(`Script already injected: ${url}`);
resolve();
return;
}
const script = document.createElement('script');
script.src = url;
script.onload = () => {
console.log(`Injected script loaded: ${url}`);
resolve();
};
script.onerror = () => {
console.error(`Failed to load script: ${url}`);
reject(new Error(`Failed to load script: ${url}`));
};
document.head.appendChild(script);
});
}
// Function to fetch and parse HTML from a given URL using GM_xmlhttpRequest
function fetchHTML(url) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function (response) {
if (response.status >= 200 && response.status < 400) {
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, 'text/html');
resolve(doc);
} else {
reject(new Error(`Failed to fetch page: ${url} - Status: ${response.status}`));
}
},
onerror: function () {
reject(new Error(`Network error while fetching page: ${url}`));
}
});
});
}
// Function to fetch an image blob from a URL using fetch API
async function fetchImage(url) {
try {
console.log(`Fetching image: ${url}`);
const response = await fetch(url, { mode: 'cors', credentials: 'omit' });
if (!response.ok) throw new Error(`Failed to fetch image: ${url} - Status: ${response.status}`);
return await response.blob();
} catch (error) {
console.error(`Error fetching image: ${error.message}`);
return null;
}
}
// Utility function to extract file extension from a URL
function getFileExtension(url) {
const match = url.match(/\.([a-zA-Z0-9]+)(?:\?.*)?$/);
return match ? match[1].toLowerCase() : 'jpg';
}
// Function to trigger download of a ZIP blob using an anchor element
function downloadUsingAnchor(blobUrl, fileName) {
const a = document.createElement('a');
a.href = blobUrl;
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
console.log(`Download initiated for: ${fileName}`);
}
// Function to create and download the ZIP file using JSZip
async function createAndDownloadZip(zip, zipName = 'images.zip') {
try {
console.log('Generating ZIP Blob...');
const zipBlob = await zip.generateAsync({ type: 'blob', compression: 'STORE' });
console.log('ZIP Blob generated. Size:', zipBlob.size, 'bytes');
const blobUrl = URL.createObjectURL(zipBlob);
downloadUsingAnchor(blobUrl, zipName);
} catch (error) {
console.error(`Error creating or downloading ZIP: ${error.message}`);
alert(`An error occurred while creating the ZIP file: ${error.message}`);
}
}
/**
* Main function to download images from a given URL.
* @param {string} linkUrl - The URL from which to download images.
* @param {boolean} useCurrentPage - If true, use the current document instead of fetching the linkUrl.
*/
async function downloadImagesFromUrl(linkUrl, useCurrentPage = false) {
try {
console.log(`Starting image download process for URL: ${useCurrentPage ? window.location.href : linkUrl}`);
let doc;
if (useCurrentPage) {
doc = document;
} else {
doc = await fetchHTML(linkUrl);
}
// Extracting the title for the ZIP file name
const jianjieDiv = doc.querySelector('div.article-meta');
if (!jianjieDiv) {
throw new Error('div.article-meta not found on the target page.');
}
const jianjieText = jianjieDiv.textContent.trim();
console.log(`Extracted jianjie text: ${jianjieText}`);
const titleMatch = jianjieText.match(/\[.*?\d+P\]/);
if (!titleMatch || !titleMatch[0]) {
console.error('TITLE not found in extracted text.');
alert('Title pattern not matched. Please check the page structure.');
return;
}
const TITLE = titleMatch[0].trim().replace(/[\\/:*?"<>|]/g, '_');
console.log(`Extracted TITLE for ZIP: ${TITLE}`);
// Collecting all image page URLs from div.pagination p
const pageDiv = doc.querySelector('div.pagination p');
let pageUrls = [];
if (pageDiv) {
const pageLinks = pageDiv.querySelectorAll('a[href]');
pageUrls = Array.from(pageLinks)
.filter(link => /^\d+$/.test(link.textContent.trim()))
.map(link => makeAbsoluteURL(link.getAttribute('href'), useCurrentPage ? window.location.href : linkUrl));
} else {
console.warn('div.pagination p not found. Proceeding with the current page only.');
}
// If useCurrentPage is true and pageUrls is empty, add the current page URL
if (useCurrentPage && pageUrls.length === 0) {
pageUrls.push(useCurrentPage ? window.location.href : linkUrl);
}
console.log(`Total related pages found: ${pageUrls.length}`);
// To store unique image URLs
const images = new Set();
// Function to process a single page and collect image URLs
async function processPage(pageUrl) {
try {
console.log(`Processing page: ${pageUrl}`);
const pageDoc = useCurrentPage && pageUrl === window.location.href ? document : await fetchHTML(pageUrl);
const contentDivs = pageDoc.querySelectorAll('article.article-content p');
if (contentDivs.length > 0) {
let imagesInPage = [];
contentDivs.forEach(div => {
const imgs = div.querySelectorAll('img[src]');
imagesInPage.push(...imgs);
});
console.log(`Found ${imagesInPage.length} images in ${pageUrl}`);
imagesInPage.forEach(img => {
const src = img.getAttribute('src');
if (src) {
images.add(makeAbsoluteURL(src, pageUrl));
}
});
} else {
console.warn(`No article.article-content p found in ${pageUrl}`);
}
} catch (e) {
console.warn(`Failed to process ${pageUrl}: ${e.message}`);
}
}
// Process all pages sequentially or with concurrency
const CONCURRENT_LIMIT = 5;
let index = 0;
async function processNext() {
while (index < pageUrls.length) {
const currentIndex = index++;
const pageUrl = pageUrls[currentIndex];
await processPage(pageUrl);
}
}
const workers = [];
for (let i = 0; i < CONCURRENT_LIMIT; i++) {
workers.push(processNext());
}
await Promise.all(workers);
if (images.size === 0) {
console.error('No images found.');
alert('No images found on the target page(s).');
return;
}
const imageArray = Array.from(images);
console.log(`Total images collected: ${imageArray.length}`);
// Initialize JSZip
const zip = new JSZip();
let counter = 0;
const totalImages = imageArray.length;
for (const imgUrl of imageArray) {
try {
const blob = await fetchImage(imgUrl);
if (!blob) {
console.warn(`Skipping image due to fetch failure: ${imgUrl}`);
continue;
}
const extension = getFileExtension(imgUrl);
const validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tiff'];
const finalExtension = validExtensions.includes(extension) ? extension : 'jpg';
const filename = `${String(counter + 1).padStart(3, '0')}.${finalExtension}`;
zip.file(filename, blob);
counter++;
console.log(`Added ${filename} to ZIP (${counter}/${totalImages})`);
} catch (e) {
console.warn(`Failed to fetch/add image ${imgUrl}: ${e.message}`);
}
}
if (counter === 0) {
console.error('No images were successfully added to the ZIP.');
alert('No images were successfully downloaded.');
return;
}
console.log(`Successfully added ${counter} images to the ZIP.`);
await createAndDownloadZip(zip, `${TITLE}.zip`);
} catch (error) {
console.error(`Error during image download process: ${error.message}`);
alert('An error occurred during the image download process.');
}
}
/**
* Function to add a DOWNLOAD button to a target element with an associated linkUrl.
* @param {HTMLElement} targetElement - The element to which the button will be appended.
* @param {string} linkUrl - The URL to be processed when the button is clicked.
* @param {boolean} useCurrentPage - If true, processes the current page without fetching linkUrl.
*/
function addDownloadButton(targetElement, linkUrl, useCurrentPage = false) {
const button = document.createElement('button');
button.innerText = 'DOWNLOAD';
button.classList.add('download-button');
// Styling the button for better appearance
button.style.marginLeft = '10px';
button.style.padding = '5px 10px';
button.style.cursor = 'pointer';
button.style.backgroundColor = '#28a745';
button.style.color = '#fff';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.fontSize = '14px';
button.style.transition = 'background-color 0.3s';
// Hover effects for the button
button.addEventListener('mouseenter', () => {
button.style.backgroundColor = '#218838';
});
button.addEventListener('mouseleave', () => {
button.style.backgroundColor = '#28a745';
});
// Click event to initiate the download process
button.addEventListener('click', async (event) => {
event.preventDefault(); // Prevent default behavior if applicable
button.disabled = true;
const originalText = button.innerText;
button.innerText = 'Processing...';
console.log(`DOWNLOAD button clicked for URL: ${linkUrl}, useCurrentPage: ${useCurrentPage}`);
await downloadImagesFromUrl(linkUrl, useCurrentPage);
button.innerText = originalText;
button.disabled = false;
});
targetElement.appendChild(button);
console.log(`DOWNLOAD button added to`, targetElement);
}
/**
* Function to find relevant elements and add DOWNLOAD buttons accordingly.
* Differentiates between Button1 and Button2 based on element location.
*/
function findAndAddButtons() {
// Adding DOWNLOAD button to div.node p a b span (Button 1)
const titleLinks = document.querySelectorAll('div.node p a b');
titleLinks.forEach(link => {
const span = link.querySelector('span');
if (span && !span.querySelector('button.download-button')) {
const href = link.getAttribute('href');
if (href) {
const linkUrl = makeAbsoluteURL(href, window.location.href);
addDownloadButton(span, linkUrl, false); // useCurrentPage = false
} else {
console.warn('No href found for div.node p a b element.');
}
}
});
// Adding DOWNLOAD button to div.toptip (Button 2)
const itemInfoDivs = document.querySelectorAll('div.toptip');
itemInfoDivs.forEach(div => {
if (!div.querySelector('button.download-button')) {
// Assuming Button 2 processes the current page
// Pass linkUrl as the current page URL
addDownloadButton(div, window.location.href, true); // useCurrentPage = true
}
});
}
/**
* Function to initialize the script: inject JSZip and add buttons
*/
async function initializeScript() {
console.log('Initializing WebURL Image Downloader script...');
try {
await injectScript('https://cdn.jsdelivr.net/npm/jszip@3.10.0/dist/jszip.min.js');
if (typeof JSZip === 'undefined') {
throw new Error('JSZip failed to load after injection.');
}
console.log('JSZip loaded successfully. Version:', JSZip.version);
findAndAddButtons();
// Optional: Observe DOM changes to add buttons dynamically if the page is single-page or dynamically loaded
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
findAndAddButtons();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
console.log('MutationObserver set to watch for new elements to add DOWNLOAD buttons.');
} catch (error) {
console.error('Failed to inject JSZip:', error.message);
alert('Failed to load JSZip library. Please check the console for details.');
}
}
// Wait for the DOM to be fully loaded before initializing
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeScript);
} else {
initializeScript();
}
})();
I have problem creating a button (let's refer it as button 1) inside div.node p a b and grabbing images from all pages.
It sounds like you're encountering issues with creating Button 1 within div.node p a b and ensuring that your script successfully grabs images from all relevant pages on your target website (https://www.xinggan5.top/*). Let's break down the potential problems and provide solutions to help you achieve the desired functionality.
1. Understanding the Issue
The main problem likely stems from how the script attempts to:
Create Button 1 inside div.node p a b.
Grab images from all pages linked within the website's pagination.
Upon reviewing your script, one critical issue stands out:
Incorrectly Extracting the href Attribute: The script tries to obtain the href attribute directly from the <b> tags within div.node p a b. However, <b> elements typically do not possess href attributes; it's the parent <a> elements that contain them.
2. Fixing Button Creation (Button 1)
To rectify the issue with Button 1, we'll adjust how the script locates and extracts the href attribute from the correct element.
Current Implementation:
javascript
const titleLinks = document.querySelectorAll('div.node p a b');
titleLinks.forEach(link => {
const span = link.querySelector('span');
if (span && !span.querySelector('button.download-button')) {
const href = link.getAttribute('href'); // Incorrect: <b> doesn't have href
if (href) {
const linkUrl = makeAbsoluteURL(href, window.location.href);
addDownloadButton(span, linkUrl, false); // useCurrentPage = false
} else {
console.warn('No href found for div.node p a b element.');
}
}
});
Problem:
The script is attempting to get the href from the <b> element, which doesn't exist. This results in href being null, and thus, no button is added.
Solution:
Extract href from the Parent <a> Element: Modify the script to target the parent <a> element of each <b> tag to correctly retrieve the href attribute.
Revised Implementation:
javascript
function findAndAddButtons() {
// Button 1: Adding DOWNLOAD button to div.node p a b
const titleLinks = document.querySelectorAll('div.node p a b');
console.log(`Found ${titleLinks.length} <b> elements within div.node p a`);
titleLinks.forEach((bElement, index) => {
const aElement = bElement.closest('a'); // Find the closest parent <a>
if (!aElement) {
console.warn(`No parent <a> found for <b> element at index ${index}.`);
return;
}
const href = aElement.getAttribute('href');
if (!href) {
console.warn(`No href found in parent <a> element for <b> at index ${index}.`);
return;
}
const span = bElement.querySelector('span');
const targetElement = span || bElement; // Use <span> if available, else <b>
if (!targetElement.querySelector('button.download-button')) {
const linkUrl = makeAbsoluteURL(href, window.location.href);
console.log(`Adding DOWNLOAD button to`, targetElement, `for URL: ${linkUrl}`);
addDownloadButton(targetElement, linkUrl, false); // useCurrentPage = false
} else {
console.log(`DOWNLOAD button already exists for <b> element at index ${index}.`);
}
});
// Button 2: Adding DOWNLOAD button to div.toptip
const itemInfoDivs = document.querySelectorAll('div.toptip');
console.log(`Found ${itemInfoDivs.length} div.toptip elements`);
itemInfoDivs.forEach((div, index) => {
if (!div.querySelector('button.download-button')) {
console.log(`Adding DOWNLOAD button to div.toptip at index ${index}`);
addDownloadButton(div, window.location.href, true); // useCurrentPage = true
} else {
console.log(`DOWNLOAD button already exists for div.toptip at index ${index}`);
}
});
}
Explanation of Changes:
Using closest('a') to Find the Parent <a> Element:
This ensures you're extracting the href from the correct element.
Fallback to <b> if <span> is Absent:
If a <span> within the <b> doesn't exist, the script appends the button directly to the <b> element.
Enhanced Logging:
Added console.log statements to track the script's progress and identify potential issues during execution.
3. Ensuring Images Are Grabbed from All Pages
With the correction in place for Button 1, let's ensure that your script effectively grabs images from all relevant pages.
Key Areas to Review:
Pagination Handling:
Verify that the pagination links are correctly identified and that all pages are being iterated over.
Image Collection Logic:
Ensure that images are being collected from each page without missing any due to asynchronous operations or errors.
Concurrency Control:
Adjust the CONCURRENT_LIMIT as needed based on the website's structure and your browser's performance.
Reviewing and Enhancing Image Fetching:
Your current script uses a CONCURRENT_LIMIT of 5 for processing pages. This is a reasonable starting point, but depending on the website's structure and the number of pages, you might need to adjust this value.
Additionally, ensure that the processPage function effectively gathers all images from each page.
Example Enhancement:
javascript
// Function to process a single page and collect image URLs
async function processPage(pageUrl) {
try {
console.log(`Processing page: ${pageUrl}`);
const pageDoc = useCurrentPage && pageUrl === window.location.href ? document : await fetchHTML(pageUrl);
// Extract images from article content
const contentDivs = pageDoc.querySelectorAll('article.article-content p');
if (contentDivs.length > 0) {
let imagesInPage = [];
contentDivs.forEach(div => {
const imgs = div.querySelectorAll('img[src]');
imagesInPage.push(...imgs);
});
console.log(`Found ${imagesInPage.length} images in ${pageUrl}`);
imagesInPage.forEach(img => {
const src = img.getAttribute('src');
if (src) {
images.add(makeAbsoluteURL(src, pageUrl));
}
});
} else {
console.warn(`No article.article-content p found in ${pageUrl}`);
}
} catch (e) {
console.warn(`Failed to process ${pageUrl}: ${e.message}`);
}
}
Recommendations:
Validate Pagination Links:
Make sure that the pagination section (div.pagination p) accurately captures all relevant pages. If the website uses dynamic loading (e.g., infinite scroll), you might need to adjust your approach.
Monitor Fetching Progress:
Use console.log statements within your image fetching loop to track progress and identify any potential bottlenecks or failures.
Handle Errors Gracefully:
Ensure that if one page fails to load, it doesn't halt the entire image scraping process. Your current error handling approach with try-catch blocks is appropriate.
4. Comprehensive Script Review
To provide a clearer and more functional script, here's a revised version incorporating the fixes and improvements discussed:
javascript
5. Testing the Script
After implementing the above changes, follow these steps to ensure everything works as intended:
Install the Updated Script:
Ensure you replace your existing Tampermonkey script with the revised version provided above.
Reload the Target Page:
Navigate to a page under https://www.xinggan5.top/* to trigger the script.
Check the Console for Logs:
Open the browser's Developer Tools (usually via F12 or Ctrl+Shift+I).
Navigate to the Console tab to monitor the script's logs. This will help you verify if buttons are being added and if images are being processed.
Verify Button Placement:
Ensure that the DOWNLOAD buttons appear within the intended div.node p a b elements and div.toptip elements.
Test the DOWNLOAD Functionality:
Click on the DOWNLOAD buttons to initiate the image downloading process.
Confirm that a ZIP file is generated and that it contains the expected images from all relevant pages.
Handle Any Additional Issues:
If buttons still aren't appearing or images aren't being downloaded correctly, refer to the console logs to identify and address specific errors or warnings.
6. Additional Recommendations
Dynamic Content Handling:
If the website uses dynamic content loading (e.g., infinite scroll or AJAX-based pagination), the MutationObserver included in the script should help in adding buttons to newly loaded content. Ensure that this functionality works as expected by monitoring the console logs during interactions like scrolling.
Optimizing Performance:
Depending on the number of images and pages, the script might consume significant memory or processing power. Monitor your browser's performance and consider implementing more robust error handling or limiting the number of concurrent fetches if necessary.
Enhancing User Experience:
Consider providing user feedback during the downloading process, such as progress indicators or notifications upon completion, to improve the user experience.
Security Considerations:
Ensure that the script only operates on trusted websites to prevent potential security risks associated with downloading and executing external scripts.
7. Conclusion
By correctly targeting the parent <a> elements to extract href attributes and ensuring that buttons are added to the appropriate elements, your script should now successfully create Button 1 within div.node p a b and initiate the image downloading process across all relevant pages. Enhanced logging will aid in monitoring the script's operations and troubleshooting any further issues that may arise.
Feel free to reach out if you encounter additional challenges or need further assistance!
I hope this comprehensive solution helps you resolve the issues with your Tampermonkey script. Let me know if you need any more assistance!
Not yet included in a published release.
// ==UserScript==
// @name JYNGMN Image Downloader
// @namespace http://tampermonkey.net/
// @version 1.25
// @description Download images from specified WebURLs as a ZIP file, log image URLs, and add DOWNLOAD buttons correctly, without leaving the page. Compatible with Chrome and Edge browsers, supports multiple pages.
// @author
// @match https://www.xinggan5.top/*
// @connect *
// @grant GM_xmlhttpRequest
// @grant GM_download
// ==/UserScript==
(function () {
'use strict';
// Logging to indicate script initialization
console.log('WebURL Image Downloader script initializing...');
// Utility function to convert relative URLs to absolute URLs
function makeAbsoluteURL(url, base) {
try {
return new URL(url, base || window.location.origin).href;
} catch (error) {
console.error(`Invalid URL: ${url} - Error: ${error.message}`);
return url;
}
}
// Function to inject external scripts, such as JSZip
async function injectScript(url) {
return new Promise((resolve, reject) => {
if (document.querySelector(`script[src="${url}"]`)) {
console.log(`Script already injected: ${url}`);
resolve();
return;
}
const script = document.createElement('script');
script.src = url;
script.onload = () => {
console.log(`Injected script loaded: ${url}`);
resolve();
};
script.onerror = () => {
console.error(`Failed to load script: ${url}`);
reject(new Error(`Failed to load script: ${url}`));
};
document.head.appendChild(script);
});
}
// Function to fetch and parse HTML from a given URL using GM_xmlhttpRequest
function fetchHTML(url) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function (response) {
if (response.status >= 200 && response.status < 400) {
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, 'text/html');
resolve(doc);
} else {
reject(new Error(`Failed to fetch page: ${url} - Status: ${response.status}`));
}
},
onerror: function () {
reject(new Error(`Network error while fetching page: ${url}`));
}
});
});
}
// Function to fetch an image blob from a URL using fetch API
async function fetchImage(url) {
try {
console.log(`Fetching image: ${url}`);
const response = await fetch(url, { mode: 'cors', credentials: 'omit' });
if (!response.ok) throw new Error(`Failed to fetch image: ${url} - Status: ${response.status}`);
return await response.blob();
} catch (error) {
console.error(`Error fetching image: ${error.message}`);
return null;
}
}
// Utility function to extract file extension from a URL
function getFileExtension(url) {
const match = url.match(/\.([a-zA-Z0-9]+)(?:\?.*)?$/);
return match ? match[1].toLowerCase() : 'jpg';
}
// Function to trigger download of a ZIP blob using an anchor element
function downloadUsingAnchor(blobUrl, fileName) {
const a = document.createElement('a');
a.href = blobUrl;
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
console.log(`Download initiated for: ${fileName}`);
}
// Function to create and download the ZIP file using JSZip
async function createAndDownloadZip(zip, zipName = 'images.zip') {
try {
console.log('Generating ZIP Blob...');
const zipBlob = await zip.generateAsync({ type: 'blob', compression: 'STORE' });
console.log('ZIP Blob generated. Size:', zipBlob.size, 'bytes');
const blobUrl = URL.createObjectURL(zipBlob);
downloadUsingAnchor(blobUrl, zipName);
} catch (error) {
console.error(`Error creating or downloading ZIP: ${error.message}`);
alert(`An error occurred while creating the ZIP file: ${error.message}`);
}
}
/**
* Main function to download images from a given URL.
* @param {string} linkUrl - The URL from which to download images.
* @param {boolean} useCurrentPage - If true, use the current document instead of fetching the linkUrl.
*/
async function downloadImagesFromUrl(linkUrl, useCurrentPage = false) {
try {
console.log(`Starting image download process for URL: ${useCurrentPage ? window.location.href : linkUrl}`);
let doc;
if (useCurrentPage) {
doc = document;
} else {
doc = await fetchHTML(linkUrl);
}
// Extracting the title for the ZIP file name
const jianjieDiv = doc.querySelector('div.article-meta');
if (!jianjieDiv) {
throw new Error('div.article-meta not found on the target page.');
}
const jianjieText = jianjieDiv.textContent.trim();
console.log(`Extracted jianjie text: ${jianjieText}`);
const titleMatch = jianjieText.match(/\[.*?\d+P\]/);
if (!titleMatch || !titleMatch[0]) {
console.error('TITLE not found in extracted text.');
alert('Title pattern not matched. Please check the page structure.');
return;
}
const TITLE = titleMatch[0].trim().replace(/[\\/:*?"<>|]/g, '_');
console.log(`Extracted TITLE for ZIP: ${TITLE}`);
// Collecting all image page URLs from div.pagination p
const pageDiv = doc.querySelector('div.pagination p');
let pageUrls = [];
if (pageDiv) {
const pageLinks = pageDiv.querySelectorAll('a[href]');
pageUrls = Array.from(pageLinks)
.filter(link => /^\d+$/.test(link.textContent.trim()))
.map(link => makeAbsoluteURL(link.getAttribute('href'), useCurrentPage ? window.location.href : linkUrl));
} else {
console.warn('div.pagination p not found. Proceeding with the current page only.');
}
// If useCurrentPage is true and pageUrls is empty, add the current page URL
if (useCurrentPage && pageUrls.length === 0) {
pageUrls.push(useCurrentPage ? window.location.href : linkUrl);
}
console.log(`Total related pages found: ${pageUrls.length}`);
// To store unique image URLs
const images = new Set();
// Function to process a single page and collect image URLs
async function processPage(pageUrl) {
try {
console.log(`Processing page: ${pageUrl}`);
const pageDoc = useCurrentPage && pageUrl === window.location.href ? document : await fetchHTML(pageUrl);
const contentDivs = pageDoc.querySelectorAll('article.article-content p');
if (contentDivs.length > 0) {
let imagesInPage = [];
contentDivs.forEach(div => {
const imgs = div.querySelectorAll('img[src]');
imagesInPage.push(...imgs);
});
console.log(`Found ${imagesInPage.length} images in ${pageUrl}`);
imagesInPage.forEach(img => {
const src = img.getAttribute('src');
if (src) {
images.add(makeAbsoluteURL(src, pageUrl));
}
});
} else {
console.warn(`No article.article-content p found in ${pageUrl}`);
}
} catch (e) {
console.warn(`Failed to process ${pageUrl}: ${e.message}`);
}
}
// Process all pages sequentially or with concurrency
const CONCURRENT_LIMIT = 5;
let index = 0;
async function processNext() {
while (index < pageUrls.length) {
const currentIndex = index++;
const pageUrl = pageUrls[currentIndex];
await processPage(pageUrl);
}
}
const workers = [];
for (let i = 0; i < CONCURRENT_LIMIT; i++) {
workers.push(processNext());
}
await Promise.all(workers);
if (images.size === 0) {
console.error('No images found.');
alert('No images found on the target page(s).');
return;
}
const imageArray = Array.from(images);
console.log(`Total images collected: ${imageArray.length}`);
// Initialize JSZip
const zip = new JSZip();
let counter = 0;
const totalImages = imageArray.length;
for (const imgUrl of imageArray) {
try {
const blob = await fetchImage(imgUrl);
if (!blob) {
console.warn(`Skipping image due to fetch failure: ${imgUrl}`);
continue;
}
const extension = getFileExtension(imgUrl);
const validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tiff'];
const finalExtension = validExtensions.includes(extension) ? extension : 'jpg';
const filename = `${String(counter + 1).padStart(3, '0')}.${finalExtension}`;
zip.file(filename, blob);
counter++;
console.log(`Added ${filename} to ZIP (${counter}/${totalImages})`);
} catch (e) {
console.warn(`Failed to fetch/add image ${imgUrl}: ${e.message}`);
}
}
if (counter === 0) {
console.error('No images were successfully added to the ZIP.');
alert('No images were successfully downloaded.');
return;
}
console.log(`Successfully added ${counter} images to the ZIP.`);
await createAndDownloadZip(zip, `${TITLE}.zip`);
} catch (error) {
console.error(`Error during image download process: ${error.message}`);
alert('An error occurred during the image download process.');
}
}
/**
* Function to add a DOWNLOAD button to a target element with an associated linkUrl.
* @param {HTMLElement} targetElement - The element to which the button will be appended.
* @param {string} linkUrl - The URL to be processed when the button is clicked.
* @param {boolean} useCurrentPage - If true, processes the current page without fetching linkUrl.
*/
function addDownloadButton(targetElement, linkUrl, useCurrentPage = false) {
const button = document.createElement('button');
button.innerText = 'DOWNLOAD';
button.classList.add('download-button');
// Styling the button for better appearance
button.style.marginLeft = '10px';
button.style.padding = '5px 10px';
button.style.cursor = 'pointer';
button.style.backgroundColor = '#28a745';
button.style.color = '#fff';
button.style.border = 'none';
button.style.borderRadius = '4px';
button.style.fontSize = '14px';
button.style.transition = 'background-color 0.3s';
// Hover effects for the button
button.addEventListener('mouseenter', () => {
button.style.backgroundColor = '#218838';
});
button.addEventListener('mouseleave', () => {
button.style.backgroundColor = '#28a745';
});
// Click event to initiate the download process
button.addEventListener('click', async (event) => {
event.preventDefault(); // Prevent default behavior if applicable
button.disabled = true;
const originalText = button.innerText;
button.innerText = 'Processing...';
console.log(`DOWNLOAD button clicked for URL: ${linkUrl}, useCurrentPage: ${useCurrentPage}`);
await downloadImagesFromUrl(linkUrl, useCurrentPage);
button.innerText = originalText;
button.disabled = false;
});
targetElement.appendChild(button);
console.log(`DOWNLOAD button added to`, targetElement);
}
/**
* Function to find relevant elements and add DOWNLOAD buttons accordingly.
* Differentiates between Button1 and Button2 based on element location.
*/
function findAndAddButtons() {
// Button 1: Adding DOWNLOAD button to div.node p a b
const titleLinks = document.querySelectorAll('div.node p a b');
console.log(`Found ${titleLinks.length} <b> elements within div.node p a`);
titleLinks.forEach((bElement, index) => {
const aElement = bElement.closest('a'); // Find the closest parent <a>
if (!aElement) {
console.warn(`No parent <a> found for <b> element at index ${index}.`);
return;
}
const href = aElement.getAttribute('href');
if (!href) {
console.warn(`No href found in parent <a> element for <b> at index ${index}.`);
return;
}
const span = bElement.querySelector('span');
const targetElement = span || bElement; // Use <span> if available, else <b>
if (!targetElement.querySelector('button.download-button')) {
const linkUrl = makeAbsoluteURL(href, window.location.href);
console.log(`Adding DOWNLOAD button to`, targetElement, `for URL: ${linkUrl}`);
addDownloadButton(targetElement, linkUrl, false); // useCurrentPage = false
} else {
console.log(`DOWNLOAD button already exists for <b> element at index ${index}.`);
}
});
// Button 2: Adding DOWNLOAD button to div.toptip
const itemInfoDivs = document.querySelectorAll('div.toptip');
console.log(`Found ${itemInfoDivs.length} div.toptip elements`);
itemInfoDivs.forEach((div, index) => {
if (!div.querySelector('button.download-button')) {
console.log(`Adding DOWNLOAD button to div.toptip at index ${index}`);
addDownloadButton(div, window.location.href, true); // useCurrentPage = true
} else {
console.log(`DOWNLOAD button already exists for div.toptip at index ${index}`);
}
});
}
/**
* Function to initialize the script: inject JSZip and add buttons
*/
async function initializeScript() {
console.log('Initializing WebURL Image Downloader script...');
try {
await injectScript('https://cdn.jsdelivr.net/npm/jszip@3.10.0/dist/jszip.min.js');
if (typeof JSZip === 'undefined') {
throw new Error('JSZip failed to load after injection.');
}
console.log('JSZip loaded successfully. Version:', JSZip.version);
findAndAddButtons();
// Optional: Observe DOM changes to add buttons dynamically if the page is single-page or dynamically loaded
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
findAndAddButtons();
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
console.log('MutationObserver set to watch for new elements to add DOWNLOAD buttons.');
} catch (error) {
console.error('Failed to inject JSZip:', error.message);
alert('Failed to load JSZip library. Please check the console for details.');
}
}
// Wait for the DOM to be fully loaded before initializing
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeScript);
} else {
initializeScript();
}
})();