const uuid = require('uuid'); const {log} = require('clew-logger'); module.exports = function (processorConfig) { downloadGallery = async (vin) => { var galleryUrls = []; async function ensureCarouselVisible() { var imgWrap; do { imgWrap = document.elementFromPoint(100, 100); 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(window.innerWidth / 2, 300); const children = imgWrap.querySelectorAll('.pswp__img'); const img = children[children.length - 1]; // debugger; // Full image hasn't loaded yet const src = imgWrap.src.split('?')[0]// get rid of querystring //check for a duplicated src for(var i = 0;i < galleryUrls.length;i++) { if(src === galleryUrls[i].url && externalCounter > 1) { //we're using the fallback counter, reset it to 1 to trigger the exit. externalCounter = 0; return; } } return downloadSrc(src, id); } function downloadSrc(src, id) { galleryUrls.push({ fileName: id, url: src }) } function nextImage() { //get the navigation button element const navigation = document.querySelector('.pswp__button.pswp__button--arrow--right') || document.querySelector('.pswp__button.pswp__button--arrow--next') navigation.click(); externalCounter += 1; } var externalCounter = 1; function getCounterValue() { const internalCounterElement = document.querySelector('.pswp__counter'); if(!internalCounterElement) return externalCounter; const [position, total] = internalCounterElement.textContent.split('/'); return parseInt(position.trim(), 10); } function run() { return new Promise(async (resolve, reject) => { await ensureCarouselVisible(); await delay(500); const firstValue = getCounterValue(); do { await downloadImage(`${vin}-${getCounterValue()}`); nextImage(); await delay(100); } while (getCounterValue() !== firstValue); return resolve(galleryUrls) }) } return new Promise((resolve, reject) => { run().then(resolve); }); } return async function (page) { log.info('Running generic boostrap extractor') const allResultsSelector = processorConfig.pageLoadIndicator; await page.waitForSelector(allResultsSelector); var vin; const vinSelector = processorConfig.vinSelector; await page.waitForSelector(vinSelector).then(async () => { let element = await page.$(vinSelector) vin = await page.evaluate(el => el.textContent, element); }).catch(error => { log.error({ message: 'Unable to grab VIN, falling back to UUID', error: error }); vin = uuid.v4(); }); log.info(vin); const firstImageLinkSelector = processorConfig.carouselTrigger; await page.click(firstImageLinkSelector); await page.waitForSelector('.pswp__img') log.info('Gallery is loaded, fetching URLS') const galleryUrls = await page.evaluate(downloadGallery, vin); return galleryUrls } }