-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlayout-shift-gif.js
377 lines (321 loc) · 11.2 KB
/
layout-shift-gif.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env node
/*
* SPDX-License-Identifier: Apache-2.0
*/
/* Args */
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
/* Puppeteer */
import puppeteer from 'puppeteer-extra'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
/* Misc */
import fs from 'fs'
import tmp from 'tmp'
/* Image generation */
import GIFEncoder from 'gif-encoder-2'
import pkg from 'canvas'
const { createCanvas, loadImage } = pkg
/* Evade bot detection */
puppeteer.use(StealthPlugin())
/* Colors */
const colors = {
reset: '\x1b[0m',
underscore: '\x1b[4m',
magenta: '\x1b[35m',
blue: '\x1b[34m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m'
}
/* About */
const { version } = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url)))
console.log(`layout-shift-gif ${version} / ${colors.blue}@defaced.dev (bluesky)${colors.reset}`)
/* Support */
if (!process.env.WORKEFFORTWASTE_SUPPORTER) {
console.log(`${colors.magenta}
┃
┃ ${colors.underscore}Support this project! ${colors.reset}${colors.magenta}
┃
┃ Help support the work that goes into creating and maintaining my projects
┃ and consider donating via on GitHub Sponsors.
┃
┃ GitHub Sponsors: https://github.com/sponsors/workeffortwaste/
┃${colors.reset}
`)
}
const options = yargs(hideBin(process.argv))
.usage('Usage: layout-shift-gif --url <url>')
.example('layout-shift-gif --url https://blacklivesmatter.com/ --device mobile --output layout-shift.gif')
.default({ d: 'mobile', o: 'layout-shift.gif', t: 'new', r: false })
.describe('u', 'Website URL')
.describe('d', 'Device type')
.describe('w', 'Device viewport width')
.describe('h', 'Device viewport height')
.describe('c', 'Cookie filename')
.describe('o', 'Output filename')
.describe('t', 'CLS calculation method')
.describe('r', 'Save report of shifts and elements')
.alias('u', 'url')
.alias('d', 'device')
.alias('w', 'width')
.alias('h', 'height')
.alias('c', 'cookies')
.alias('o', 'output')
.alias('r', 'report')
.alias('t', 'type')
.number(['h', 'w'])
.string(['u', 'd', 'c', 'o', 't'])
.boolean(['r'])
.demandOption(['url'])
.epilogue('For more information visit documentation at: \nhttp://github.com/workeffortwaste/layout-shift-gif')
.argv
/* Network conditions */
const Good3G = {
offline: false,
downloadThroughput: 1.5 * 1024 * 1024 / 8,
uploadThroughput: 750 * 1024 / 8,
latency: 40
}
/* Device for mobile emulation */
const phone = {
name: 'Nexus 5X',
userAgent: 'Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR4.170623.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36',
viewport: {
width: 412,
height: 732,
deviceScaleFactor: 2.625,
isMobile: true,
hasTouch: true,
isLandscape: false
}
}
/* Detect layout shift */
const clsDetection = (type) => {
const getElementSelector = (element) => {
if (!element) return null
const parts = []
while (element.parentElement) {
let part = element.tagName.toLowerCase() // Tag name in lowercase
// Add ID if present
if (element.id) {
part += `#${element.id}`
}
// Add classes if present
if (element.className) {
const classes = element.className.trim().split(/\s+/).join('.')
part += `.${classes}`
}
// Add :nth-child if necessary
const siblings = Array.from(element.parentElement.children)
if (siblings.filter(el => el.tagName === element.tagName).length > 1) {
const index = siblings.indexOf(element) + 1
part += `:nth-child(${index})`
}
parts.unshift(part) // Add the part to the beginning of the array
element = element.parentElement // Move up to the parent
}
// Include `body` or `html` as the root
parts.unshift(element.tagName.toLowerCase())
return parts.join(' > ')
}
window.cumulativeLayoutShiftScore = 0
window.previousRect = []
window.currentRect = []
window.shifts = []
window.sources = []
let firstTs = Number.NEGATIVE_INFINITY
let prevTs = Number.NEGATIVE_INFINITY
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.hadRecentInput) continue
if (type === 'new') {
if (entry.startTime - firstTs > 5000 || entry.startTime - prevTs > 1000) {
firstTs = entry.startTime
window.cumulativeLayoutShiftScore = 0
window.shifts = []
}
prevTs = entry.startTime
}
entry.sources.forEach((e) => {
window.previousRect.push(JSON.parse(JSON.stringify(e.previousRect)))
window.currentRect.push(JSON.parse(JSON.stringify(e.currentRect)))
window.shifts.push(entry.value)
})
window.sources.push({
value: entry.value,
elements: entry.sources.map((s) => getElementSelector(s.node))
})
window.cumulativeLayoutShiftScore += entry.value
window.onload = (event) => {
observer.takeRecords()
observer.disconnect()
}
}
})
observer.observe({ type: 'layout-shift', buffered: true })
}
// Return the colours we're using for the CLS
const getColor = (cls) => {
let c = {
stroke: 'rgba(0,128,0,.7)',
fill: 'rgba(0,128,0,.1)',
solid: 'rgb(0,128,0,1)'
}
if (cls > 0.1) {
c = {
stroke: 'rgba(255,125,0,.5)',
fill: 'rgba(255,125,0,.05)',
solid: 'rgba(255,125,0,1)'
}
}
if (cls > 0.25) {
c = {
stroke: 'rgba(255,0,0,.5)',
fill: 'rgba(255,0,0,.05)',
solid: 'rgba(255,0,0,1)'
}
}
return c
}
const createGif = async (url, device) => {
// Launch puppeteer
const browser = await puppeteer.launch({ ignoreHTTPSErrors: true, args: ['--no-sandbox'], timeout: 10000 })
try {
const page = await browser.newPage()
if (options.cookies) {
const cookies = JSON.parse(fs.readFileSync(options.cookies))
await page.setCookie(...cookies)
}
const client = await page.target().createCDPSession()
await client.send('Network.enable')
await client.send('ServiceWorker.enable')
// Throttle the network and CPU to give us a chance to actually capture layout shifts.
await client.send('Network.emulateNetworkConditions', Good3G)
await client.send('Emulation.setCPUThrottlingRate', { rate: 4 })
// Emulate a phone or standard desktop size.
if (device === 'mobile') {
await page.emulate(phone)
await page.setViewport({
width: 412,
height: 732,
deviceScaleFactor: 1
})
// Override viewport resolution if a width or height are manually supplied
if (options.width || options.height) {
await page.setViewport({
width: options.width || page.viewport().width,
height: options.height || page.viewport().height,
deviceScaleFactor: 1
})
}
} else {
// Override viewport resolution if a width or height are manually supplied
await page.setViewport({
width: options.width || 1920,
height: options.height || 1080
})
}
// Initiate clsDetection at the earliest possible moment
await page.evaluateOnNewDocument(clsDetection, options.type)
// Navigate to the page and wait until it's hit the load event, 120s timeout for tries
await page.goto(url, { waitUntil: 'load', timeout: 120000 })
// Populate an object for everything we need to draw our final images
const output = await page.evaluate(() => {
return {
score: window.cumulativeLayoutShiftScore,
previousRect: window.previousRect,
currentRect: window.currentRect,
shifts: window.shifts,
report: {
url: window.location.href,
cls: window.cumulativeLayoutShiftScore,
entries: window.sources
}
}
})
output.scaleFactor = page.viewport().deviceScaleFactor || 1
// Set up the tmp directory
const tmpobj = tmp.dirSync()
// Take a screenshot of the page after it's loaded.
await page.screenshot({ path: tmpobj.name + '/temp-screenshot.png' })
// Close the browser.
browser.close()
// Load the puppeteer screenshot from the fs
const image = await loadImage(tmpobj.name + '/temp-screenshot.png')
// Start a gif encoder at the resolution of our screenshot
const encoder = new GIFEncoder(image.width, image.height)
// GIF encoder settings
encoder.start()
encoder.setRepeat(0) // 0 for repeat, -1 for no-repeat
encoder.setDelay(500) // frame delay in ms
encoder.setQuality(20) // image quality. 10 is default.
// Create our canvas
const canvas = createCanvas(image.width, image.height)
const ctx = canvas.getContext('2d')
// Canvas setup function
const canvasSetup = () => {
// Add the screenshot to each frame
ctx.drawImage(image, 0, 0, image.width, image.height)
// Add the CLS score in the top left corner
ctx.beginPath()
ctx.rect(0, 0, 110 * output.scaleFactor, 36 * output.scaleFactor)
ctx.fillStyle = getColor(output.score).solid
ctx.fill()
ctx.lineWidth = 2 * output.scaleFactor
ctx.fillStyle = 'white'
ctx.font = 18 * output.scaleFactor + 'px Arial'
ctx.fillText('CLS: ' + output.score.toFixed(3), 8 * output.scaleFactor, 24 * output.scaleFactor)
}
// Setup the canvas for the first frame
canvasSetup()
// Output the first frame rects
output.currentRect.forEach((d, k) => {
ctx.strokeStyle = getColor(output.shifts[k]).stroke
ctx.fillStyle = getColor(output.shifts[k]).fill
ctx.beginPath()
ctx.rect(d.x * output.scaleFactor, d.y * output.scaleFactor, d.width * output.scaleFactor, d.height * output.scaleFactor)
ctx.stroke()
ctx.fill()
})
// Add frame to the GIF
encoder.addFrame(ctx)
// Clear the first frame
ctx.clearRect(0, 0, canvas.width, canvas.height)
// Setup the canvas for the second frame
canvasSetup()
// Output the the second frame rects
ctx.setLineDash([5 * output.scaleFactor, 3 * output.scaleFactor])
output.previousRect.forEach((d, k) => {
ctx.strokeStyle = getColor(output.shifts[k]).stroke
ctx.fillStyle = getColor(output.shifts[k]).fill
ctx.beginPath()
ctx.rect(d.x * output.scaleFactor, d.y * output.scaleFactor, d.width * output.scaleFactor, d.height * output.scaleFactor)
ctx.stroke()
ctx.fill()
})
// Add frame to the GIF
encoder.addFrame(ctx)
// Write the GIF
encoder.finish()
fs.writeFileSync(options.output, encoder.out.getData())
// Save the report if requested
if (options.report) {
fs.writeFileSync(options.output.replace('.gif', '.json'), JSON.stringify(output.report))
}
// Pass back the CLS score
return output.score.toFixed(3)
} catch (error) {
browser.close()
throw (error)
}
}
createGif(options.url, options.device, options.filename)
.then(e => {
let scoreColor = colors.green
if (e > 0.1) scoreColor = colors.yellow
if (e > 0.25) scoreColor = colors.red
console.log(`Cumulative layout shift (CLS) ${scoreColor}${e}${colors.reset}`)
console.log(`Image succesfully saved as ${colors.blue}${options.output}${colors.reset}`)
if (options.report) console.log(`Report succesfully saved as ${colors.blue}${options.output.replace('.gif', '.json')}${colors.reset}`)
})
.catch(e => console.log(e))