const puppeteer = require('puppeteer') var url = ''; process.argv.forEach((val, index, array) => { console.log(val) if(val.startsWith('url=')){ url = val.substring(4); } }) if(!url || url.length === 0) { console.log('Need to supply url'); process.exit(); } console.log("hello?") const run = async () => { const browser = await puppeteer.launch({ headless:false }); const page = await browser.newPage(); await page.goto(url); // Type into search box. // await page.type('.devsite-search-field', 'Headless Chrome'); // Wait for suggest overlay to appear and click "show all results". const allResultsSelector = '.gallery'; await page.waitForSelector(allResultsSelector); const vinSelector = 'body > main > div.listing > div:nth-child(3) > div.column.column-right.column-right-force > div.essentials > div:nth-child(5) > ul > li:nth-child(1) > a'; await page.waitForSelector(vinSelector); let element = await page.$(vinSelector) let vin = await page.evaluate(el => el.textContent, element); console.log(vin); // await page.click(allResultsSelector); const client = await page.target().createCDPSession() await client.send('Page.setDownloadBehavior', { behavior: 'allow', downloadPath: './images', }) const firstImageLinkSelector = '.gallery > a:nth-child(1)'; await page.click(firstImageLinkSelector); await page.waitForSelector('.pswp__img') console.log('image ready?') await page.evaluate(downloadGallery, vin); console.log('all done'); // Print all the files. // console.log(links.join('\n')); await browser.close(); }; downloadGallery = (vin) => { async function ensureCarouselVisible() { var imgWrap; do { imgWrap = document.elementFromPoint(100, 100); console.log(imgWrap.classList); await delay(50); } while(imgWrap.classList.contains('pswp__img--placeholder')); } function delay(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } // Find the active image, surround it in an anchor tag, then click it. async function downloadImage(id) { const imgWrap = document.elementFromPoint(300, 300); const children = imgWrap.querySelectorAll('.pswp__img'); const img = children[children.length - 1]; console.log(img, imgWrap); // Full image hasn't loaded yet const src = imgWrap.src.split('?')[0]// get rid of querystring return downloadSrc(src, id); } function downloadSrc(src, id) { const a = document.createElement('a'); a.href = src; a.download = `${id}`; console.log(src, a); a.click(); } function nextImage() { document.querySelector('.pswp__button.pswp__button--arrow--right').click(); } function getCounterValue() { const [position, total] = document.querySelector('.pswp__counter').textContent.split('/'); return parseInt(position.trim(), 10); } async function run() { await ensureCarouselVisible(); await delay(500); const firstValue = getCounterValue(); async function imageLoop() { await downloadImage(`${vin}-${getCounterValue()}`); nextImage(); await delay(100); // console.log('counter', getCounterValue()); if (getCounterValue() !== firstValue) return imageLoop(); // recurse } return imageLoop(); } return run().then(async () => await delay(1000)); } run();