-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (82 loc) · 2.75 KB
/
index.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
window.addEventListener('DOMContentLoaded', function() {
const queryString = require('query-string');
const inputEl = document.getElementById('url-input');
const formEl = document.getElementById('url-form');
// Output Els
const titleEl = document.getElementById('title');
const descriptionEl = document.getElementById('description');
const iconEl = document.getElementById('icon');
const imageEl = document.getElementById('image');
const keywordsEl = document.getElementById('keywords');
const siteNameEl = document.getElementById('siteName');
const typeEl = document.getElementById('type');
const fullDataEl = document.getElementById('full-data');
const spinnerContainer = document.getElementById('spinner');
const outputContainer = document.getElementById('output');
function fetchAndDisplayMetadata(url) {
spinnerContainer.classList.remove('hidden');
spinnerContainer.classList.remove('hidden-faded');
fetch('https://tengam.org/api/v1/metadata', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
"objects": [
{ "url": url }
]
})
})
.then((response) => {
if (response.ok) {
return response.json();
}
throw new Error("Response not OK");
})
.then((metadata) => {
outputContainer.classList.remove('hidden');
spinnerContainer.classList.add('hidden-faded');
if (!metadata[0]) {
throw new Error('No metadata response, possible API error?');
}
const data = metadata[0];
titleEl.innerText = data.title;
descriptionEl.innerText = data.description;
iconEl.src = data.icon;
imageEl.src = data.image;
keywordsEl.innerText = data.keywords.join(', ');
siteNameEl.innerText = data.siteName;
typeEl.innerText = data.type;
const value = JSON.stringify(data, null, 2);
fullDataEl.value = value;
const longestLineLength = value.split('\n').reduce((longest, current) => {
if (longest < current.length) {
return current.length;
} else {
return longest;
}
}, 0);
fullDataEl.rows = value.split('\n').length;
fullDataEl.cols = longestLineLength;
});
}
formEl.addEventListener('submit', function(event) {
const urlValue = inputEl.value;
if (!urlValue) {
event.preventDefault();
return false;
}
fetchAndDisplayMetadata(urlValue);
location.search = queryString.stringify({
url: urlValue
});
event.preventDefault();
return false;
});
const parsed = queryString.parse(location.search);
if (parsed.url) {
const url = decodeURIComponent(parsed.url);
inputEl.value = url;
fetchAndDisplayMetadata(url);
}
});