-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (36 loc) · 1.31 KB
/
Copy pathscript.js
File metadata and controls
39 lines (36 loc) · 1.31 KB
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
function syntaxHighlight(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
let cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
document.querySelector('.links table').addEventListener('click', event => {
const {target} = event;
if (target.tagName.toLowerCase() !== 'button') {
return;
}
const tr = target.closest('tr');
const url = tr.querySelector('a').href;
const codeBox = document.querySelector('.result code');
document.querySelector('.result .url').innerText = url;
codeBox.innerHTML = '';
fetch(url).then(res => res.json())
.then(arr => {
if (arr.length > 500) {
arr.length = 500;
}
codeBox.innerHTML = syntaxHighlight(JSON.stringify(arr, null, 4));
});
});