Last active
December 7, 2023 11:45
-
-
Save thanhlongzzz/950a94476ec6531eea5fede7386756e3 to your computer and use it in GitHub Desktop.
download raw images (no watermark ) from a freepik.com page, F12 -> Console -> paste script and enjoy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//eg: https://www.freepik.com/free-photos-vectors/noel-background/5 | |
function getUniqueUrls(urlList) { | |
const urlSet = new Set(); | |
// Iterate through the list and add unique URLs to the set | |
urlList.forEach(url => { | |
// Check if the URL is not already in the set | |
if (!urlSet.has(url)) { | |
urlSet.add(url); | |
} | |
}); | |
// Convert the set back to an array | |
const uniqueUrls = Array.from(urlSet); | |
return uniqueUrls; | |
} | |
function downloadImage(url) { | |
if (url) { | |
fetch(url) | |
.then(response => response.blob()) | |
.then(blob => { | |
const link = document.createElement('a'); | |
const fileName = url.split('/').pop().split('?')[0]; | |
if (fileName.includes('.htm')) { | |
console.log('error: limit reached ' + url) | |
return; | |
} | |
link.href = window.URL.createObjectURL(blob); | |
link.download = fileName; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
}) | |
.catch(error => console.error(`Error downloading image: ${error}`)); | |
} | |
} | |
function downloadImages(imageUrls) { | |
imageUrls.forEach(url => downloadImage(url)); | |
} | |
function scrollToBottom(callback) { | |
var startScrollTop = document.documentElement.scrollTop || document.body.scrollTop; | |
window.scrollTo({ | |
top: document.body.scrollHeight, | |
behavior: 'smooth' | |
}); | |
function checkScroll() { | |
var currentScrollTop = document.documentElement.scrollTop || document.body.scrollTop; | |
if (currentScrollTop === startScrollTop) { | |
// Scrolling is finished | |
if (typeof callback === 'function') { | |
callback(); | |
} | |
} else { | |
// Continue checking until scrolling is finished | |
startScrollTop = currentScrollTop; | |
requestAnimationFrame(checkScroll); | |
} | |
} | |
// Start checking for scrolling completion | |
checkScroll(); | |
} | |
// Function to fetch a URL and find image links, get largest srcset | |
function getLargestUrl(srcset) { | |
const urls = srcset.split(', '); | |
// Extract width and URL for each entry | |
const entries = urls.map(entry => { | |
const [url, widthString] = entry.split(' '); | |
const width = parseInt(widthString, 10); | |
return { | |
url, | |
width | |
}; | |
}); | |
// Find the entry with the highest width | |
const largestEntry = entries.reduce((prev, current) => (current.width > prev.width ? current : prev)); | |
return largestEntry.url; | |
} | |
async function findImageLinks(url) { | |
try { | |
// Fetch the HTML content of the provided URL | |
const response = await fetch(url); | |
const html = await response.text(); | |
// Create a DOM parser | |
const parser = new DOMParser(); | |
// Parse the HTML content | |
const doc = parser.parseFromString(html, 'text/html'); | |
// Find image links in the parsed document | |
const imageLinks = doc.querySelector('.detail__preview.detail__preview--vector img').getAttribute('srcset'); | |
return getLargestUrl(imageLinks); | |
} catch (error) { | |
console.error('Error fetching or parsing the HTML: ' + url); | |
return null; | |
} | |
} | |
function delay(milliseconds) { | |
return new Promise(resolve => setTimeout(resolve, milliseconds)); | |
} | |
scrollToBottom(async function() { | |
console.log('Scrolling is complete!'); | |
}); | |
async function main() { | |
await delay(2000); | |
let urls = []; | |
document.querySelectorAll('figure.showcase__item a.js-detail-data-link').forEach(item => { | |
let url = item.getAttribute('href'); | |
urls.push(url); | |
}) | |
console.log(urls) | |
for (const url of urls) { | |
console.log(url) | |
findImageLinks(url) | |
.then(imageLink => { | |
downloadImage(imageLink); | |
console.log('Image Link:', imageLink); | |
}) | |
.catch(error => { | |
console.error('Error:', error); | |
}); | |
await delay(3000); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment