Write me javascript code I can execute in the browser console for a page which has a…
- User
Write me javascript code I can execute in the browser console for a page which has a gallery of images I'd like to download. The gallery is in the div class="row", with each image's details in a div with class="col-auto". Example with 2 images in the gallery (there may be 25 or more images):
<div class="row"> <div class="col-auto"> <a href="1255a/51z777j7u7708.webp"><img src="1255a_thumbs/51z777j7u7708.webp"> <br>Photo_Z00Y5198.we...</a> </div> <div class="col-auto"> <a href="1255a/51z777j7u7709.webp"><img src="1255a_thumbs/51z777j7u7709.webp"> <br>Photo_Z00Y5199.we...</a> </div> </div>The javascript code should be something I can paste into the browser console to execute. The script should download each image in the gallery using the image path found in but save the image with the name found in the next line before the first period in the name. For example, the image at path 1255a/51z777j7u7708.webp has the truncated name shown in the HTML as Photo_Z00Y5198.we..., which would mean the image should be saved with the name Photo_Z00Y5198.webp.
Assistant
Here's a JavaScript code you can paste into your browser console to download all the images from the gallery:
// Function to trigger a download for a given URL and filename function downloadFile(url, filename) { const a = document.createElement('a'); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); document.body.removeChild(a); } // Function to extract the clean filename from the text function getCleanFilename(text) { // Find the part before the first period (if any) const firstPeriodIndex = text.indexOf('.'); if (firstPeriodIndex !== -1) { return text.substring(0, firstPeriodIndex) + '.webp'; } return text + '.webp'; // fallback if no period found } // Get all the gallery items const galleryItems = document.querySelectorAll('.row .col-auto'); // Process each gallery item galleryItems.forEach(item => { // Get the anchor tag containing the image and name const anchor = item.querySelector('a'); if (anchor) { // Get the image URL from the href attribute const imageUrl = anchor.getAttribute('href'); // Get the text content which contains the filename const textContent = anchor.textContent.trim(); // Find the line with the filename (after <br>) const lines = textContent.split('\n').map(line => line.trim()).filter(line => line); const filenameLine = lines.length > 1 ? lines[1] : lines[0]; // Clean up the filename const cleanFilename = getCleanFilename(filenameLine); // Log what we're doing (for debugging) console.log(`Downloading ${imageUrl} as ${cleanFilename}`); // Trigger the download downloadFile(imageUrl, cleanFilename); } }); console.log('Download process initiated for all images. Check your browser downloads.');How to use this:
- Open the browser's developer tools (usually F12 or right-click → Inspect)
- Go to the Console tab
- Paste this entire code block and press Enter
Notes:
- The script will attempt to download all images immediately
- Some browsers might block multiple automatic downloads, in which case you'll need to allow them
- If the images are on a different domain, you might run into CORS issues
- The script assumes all images should have a .webp extension (as in your example)
- The script handles the case where the filename is truncated with "..." in the HTML
If you need to modify the behavior (like changing the file extension or handling different HTML structure), you can adjust the code accordingly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.