-
Notifications
You must be signed in to change notification settings - Fork 29
/
screenshot-changes.js
90 lines (76 loc) · 2.37 KB
/
screenshot-changes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
/* eslint-disable no-console */
/**
* External dependencies.
*/
const path = require( 'path' );
const puppeteer = require( 'puppeteer' );
/**
* Internal dependencies.
*/
const manifest = require( './page-manifest.json' );
const IMAGE_PATH = path.resolve( process.env.GITHUB_WORKSPACE || '.', 'artifacts' );
// node ./env/screenshot-changes.js [...files]
const [ , , ...files ] = process.argv;
async function getPageDetails( slug ) {
const apiUrl = `https://wordpress.org/wp-json/wp/v2/pages?context=wporg_export&slug=${ slug }`;
let post = false;
try {
const response = await fetch( apiUrl );
[ post ] = await response.json();
post.localLink = post.link.replace( 'https://wordpress.org/', 'http://localhost:8888/' );
} catch ( error ) {
console.error( error.message );
}
return post;
}
( async () => {
const browser = await puppeteer.launch( { headless: true } );
const page = await browser.newPage();
await page.setViewport( {
width: 1440,
height: 800,
deviceScaleFactor: 1,
} );
for ( let i = 0; i < files.length; i++ ) {
const file = files[ i ];
const found = manifest.find(
( entry ) => entry.pattern === path.basename( file ) || `${ entry.slug }.php` === path.basename( file )
);
if ( found ) {
const post = await getPageDetails( found.slug );
console.log( `${ post.title.rendered } [${ post.link }]` );
await page.goto( post.localLink, { waitUntil: 'networkidle0' } );
await page.evaluate( async () => {
// eslint-disable-next-line no-undef
const images = document.querySelectorAll( 'img[class*=wp-image]' );
for ( let img = 0; img < images.length; img++ ) {
images[ img ].scrollIntoView();
await new Promise( ( r ) => setTimeout( r, 100 ) );
}
// Wait for all remaining lazy loading images to load
await Promise.all(
Array.from( images, ( image ) => {
if ( image.complete ) {
return;
}
return new Promise( ( resolve, reject ) => {
image.addEventListener( 'load', resolve );
image.addEventListener( 'error', reject );
} );
} )
);
} );
await page.evaluate( async () => {
// eslint-disable-next-line no-undef
document.body.scrollIntoView( true );
} );
await page.waitForNetworkIdle();
await page.screenshot( {
path: `${ IMAGE_PATH }/${ found.slug }.png`,
fullPage: true,
} );
}
}
await browser.close();
} )();