-
Notifications
You must be signed in to change notification settings - Fork 1
/
holobytes.js
70 lines (61 loc) · 2.05 KB
/
holobytes.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
const rootURL = "https://holopin.io";
const blogURL = "https://blog.holopin.io";
const holoBytesPath = "/holobyte/collect";
const ul = document.getElementById("holobytes");
const list = document.createDocumentFragment();
const holoBytesLinks = {};
const getHoloBytesLinks = (doc = document, path = holoBytesPath) => {
const hbLinks = {};
let holobytesLinks = doc.getElementsByTagName("a");
for (let counter = 0; counter < holobytesLinks.length; counter++) {
const _url = new URL(holobytesLinks[counter].href, rootURL);
if (holobytesLinks[counter].href.includes(path)) {
hbLinks[_url.pathname] = holobytesLinks[counter];
hbLinks[_url.pathname].style.background = "red";
}
}
return hbLinks;
};
const getDocumentObject = async (url) => {
return await fetch(url)
.then(function (response) {
return response.text();
})
.then(function (html) {
let parser = new DOMParser();
return parser.parseFromString(html, "text/html");
})
.catch(function (err) {
console.log("Failed to fetch document: ", err);
});
};
const main = async () => {
let doc = await getDocumentObject(blogURL);
let home = getHoloBytesLinks(doc);
for (const [_url, element] of Object.entries(home)) {
holoBytesLinks[_url] = element;
}
const posts = getHoloBytesLinks(doc, "/posts/");
for (const postURL of Object.keys(posts)) {
let doc = await getDocumentObject(`${blogURL}${postURL}`);
let postLinks = getHoloBytesLinks(doc);
for (const [_url, element] of Object.entries(postLinks)) {
holoBytesLinks[_url] = element;
}
}
console.log(`HoloBytes hunted: ${Object.keys(holoBytesLinks).length}`);
for (const [_url, element] of Object.entries(holoBytesLinks)) {
let li = document.createElement("li");
let a = document.createElement("a");
let _uri = `${rootURL}${_url}`;
a.href = _uri;
a.innerText = element.innerText;
a.target = "_blank";
a.referrerPolicy = "no-referrer";
li.appendChild(a);
list.appendChild(li);
console.log(_uri);
}
ul.appendChild(list);
};
main();