Created
November 23, 2023 16:33
-
-
Save jaesbit/8ad14eb45e7610f2985eeaff40e6502a to your computer and use it in GitHub Desktop.
[tampermonkey] CLS logger using PerformanceObserver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Cumulative Layout Shift Measure | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Try to improve your CWV score | |
// @author @jaesbit | |
// @match *://*/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
let CLS = 0; | |
const observer = new PerformanceObserver((list) => { | |
list.getEntries().forEach((entry) => { | |
if (entry.hadRecentInput) { return; } // Ignore shifts after recent input. | |
CLS += entry.value; | |
if (entry.value < 0.01) { return; } // Ignore small shifts. | |
console.log(`[CLS] Layout shift of ${entry.value} at ${entry.startTime} with computed score ${CLS}`); | |
entry.sources.forEach((source) => { | |
const element = source.node; | |
if (element) { | |
console.log('[CLS] Element causing shift:', element); | |
} | |
}); | |
}); | |
}).observe({ type: 'layout-shift', buffered: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment