How do I append this JSON
data to e.g. Grid
layout.
I’m pulling Google PageSpeed JSON
data through following script:
function clicked () {
const url = document.getElementById('url').value;
document.getElementById('urlerror').style.display = 'none';
if (url.indexOf('https://') === -1) {
document.getElementById('urlerror').style.display = 'block';
return;
}
const xhr = new XMLHttpRequest();
xhr.open('GET', `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=${encodeURIComponent(url)}&fields=lighthouseResult%2Fcategories%2F*%2Fscore&prettyPrint=false&strategy=desktop&category=performance&category=pwa&category=best-practices&category=accessibility&category=seo&key={YOUR_API_KEY}`);
xhr.onload = function () {
document.getElementById('data').innerHTML = xhr.responseText;
}
xhr.send();
}
The JSON data is sent to the following div
:
<div class="pagespeed">
<input type="text" placeholder="Enter webpage URL e.g.http://www.example.com" id="url" />
<input type="button" id="button" value="PageSpeed Data" onclick="clicked();" />
<div id="urlerror">Please Enter a Valid URL e.g. http://www.example.com</div>
<pre id="data"></pre>
</div>
This is the JSON data. Categories remain the same but scores are dynamic:
{
"lighthouseResult": {
"categories": {
"performance": {
"score": 0.99
},
"accessibility": {
"score": 0.7
},
"best-practices": {
"score": 0.77
},
"seo": {
"score": 0.9
},
"pwa": {
"score": 0.56
}
}
}
}
I’d like to see 2 columns, with categories on the left and the scores on the right. Each category and score has its own id or class inside the HTML
2
Answers
Maybe like this:
Use Template String ES6 for write html in js.