· 4 years ago · Mar 31, 2021, 06:20 PM
1<script type="text/javascript">
2    $(document).ready(function() {
3        function run() {
4            const url = setUpQuery();
5            fetch(url)
6                .then(response => response.json())
7                .then(json => {
8                    showInitialContent(json.id);
9                    const cruxMetrics = {
10                        "First Contentful Paint": json.loadingExperience.metrics.FIRST_CONTENTFUL_PAINT_MS.category,
11                        "First Input Delay": json.loadingExperience.metrics.FIRST_INPUT_DELAY_MS.category
12                    };
13                    showCruxContent(cruxMetrics);
14                    const lighthouse = json.lighthouseResult;
15                    const lighthouseMetrics = {
16                        'First Contentful Paint': lighthouse.audits['first-contentful-paint'].displayValue,
17                        'Speed Index': lighthouse.audits['speed-index'].displayValue,
18                        'Time To Interactive': lighthouse.audits['interactive'].displayValue,
19                        'First Meaningful Paint': lighthouse.audits['first-meaningful-paint'].displayValue,
20                        'First CPU Idle': lighthouse.audits['first-cpu-idle'].displayValue,
21                        'Estimated Input Latency': lighthouse.audits['estimated-input-latency'].displayValue
22                    };
23                    showLighthouseContent(lighthouseMetrics);
24                });
25        }
26
27        function setUpQuery() {
28            const api = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed';
29            const parameters = {
30                url: encodeURIComponent('https://developers.google.com'),
31                api_key : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
32            };
33            let query = `${api}?`;
34            for (key in parameters) {
35                query += `${key}=${parameters[key]}`;
36            }
37            return query;
38        }
39
40        function showInitialContent(id) {
41            document.body.innerHTML = '';
42            const title = document.createElement('h1');
43            title.textContent = 'PageSpeed Insights API Demo';
44            document.body.appendChild(title);
45            const page = document.createElement('p');
46            page.textContent = `Page tested: ${id}`;
47            document.body.appendChild(page);
48        }
49
50        function showCruxContent(cruxMetrics) {
51            const cruxHeader = document.createElement('h2');
52            cruxHeader.textContent = "Chrome User Experience Report Results";
53            document.body.appendChild(cruxHeader);
54            for (key in cruxMetrics) {
55                const p = document.createElement('p');
56                p.textContent = `${key}: ${cruxMetrics[key]}`;
57                document.body.appendChild(p);
58            }
59        }
60
61        function showLighthouseContent(lighthouseMetrics) {
62            const lighthouseHeader = document.createElement('h2');
63            lighthouseHeader.textContent = "Lighthouse Results";
64            document.body.appendChild(lighthouseHeader);
65            for (key in lighthouseMetrics) {
66                const p = document.createElement('p');
67                p.textContent = `${key}: ${lighthouseMetrics[key]}`;
68                document.body.appendChild(p);
69            }
70        }
71
72        run();
73    });
74</script>