Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed image waiting method on fullPage option, +TravisCI fix #49

Closed
wants to merge 8 commits into from
27 changes: 13 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ const parseCookie = (url, cookie) => {
return returnValue;
};

const imagesHaveLoaded = () => [...document.images].map(element => element.complete);

const captureWebsite = async (input, options) => {
options = {
inputType: 'url',
Expand Down Expand Up @@ -321,34 +319,35 @@ const captureWebsite = async (input, options) => {
if (screenshotOptions.fullPage) {
// Get the height of the rendered page
const bodyHandle = await page.$('body');
const bodyBoundingHeight = await bodyHandle.boundingBox();
const {height: bodyBoundingHeight} = await bodyHandle.boundingBox();
await bodyHandle.dispose();

// Scroll one viewport at a time, pausing to let content load
const viewportHeight = viewportOptions.height;
let viewportIncrement = 0;
while (viewportIncrement + viewportHeight < bodyBoundingHeight) {
const navigationPromise = page.waitForNavigation({waitUntil: 'networkidle0'});
/* eslint-disable no-await-in-loop */
await page.evaluate(_viewportHeight => {
/* eslint-disable no-undef */
/* eslint-disable-next-line no-undef */
window.scrollBy(0, _viewportHeight);
/* eslint-enable no-undef */
}, viewportHeight);
await navigationPromise;
/* eslint-enable no-await-in-loop */
viewportIncrement += viewportHeight;
}

// Scroll back to top
await page.evaluate(_ => {
/* eslint-disable no-undef */
// Wait for images to be complete and scroll back to top
await page.evaluate(async _ => {
const selectors = [...document.images];

await Promise.all(selectors.filter(img => !img.complete).map(img => {
return Promise.race([
promisify(img.addEventListener)('load'),
promisify(img.addEventListener)('error')
]);
}));
/* eslint-disable-next-line no-undef */
window.scrollTo(0, 0);
/* eslint-enable no-undef */
});

// Some extra delay to let images load
await page.waitForFunction(imagesHaveLoaded, {timeout: 60});
}

const buffer = await page.screenshot(screenshotOptions);
Expand Down