-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
101 lines (87 loc) · 2.31 KB
/
script.js
File metadata and controls
101 lines (87 loc) · 2.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
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
97
98
99
100
import {
$
} from './util.js'
import {
addLinkToPage,
addWebRingMemberToPage,
setGlobalError
} from './ui.js'
import * as config from './config.js'
async function readPortalLinks(archive) {
try {
var json = await archive.readFile('/portal.json', 'utf8')
var portal = JSON.parse(json)
portal.forEach(link => {
link.date = new Date(link.date)
})
return portal
} catch (e) {
console.error('Failed to load portal.json', archive.url, e)
return []
}
}
async function readWebRing(archive) {
try {
var json = await archive.readFile('/web-ring.json', 'utf8')
var webRing = JSON.parse(json)
return webRing
} catch (e) {
console.error('Failed to load web-ring.json', archive.url, e)
return []
}
}
async function readPortal(archive, sourceName, maxDepth = 0, depth = 0) {
// fetch the portal's data
var webRing = await readWebRing(archive)
var links = await readPortalLinks(archive)
// attach the origin archive to each
links.forEach(link => {
link.archive = archive
link.webRingName = sourceName
link.webRingDepth = depth
})
if (depth < maxDepth) {
for (var webRingMember of webRing) {
let wrmArchive = new DatArchive(webRingMember.url)
let res = await readPortal(wrmArchive, webRingMember.name, maxDepth, depth + 1)
links = links.concat(res.links)
}
}
// return
return {
webRing,
links
}
}
function onChangeHops(e) {
config.setHops(e.target.value)
window.location.reload()
}
async function onPageLoad() {
var archive = new DatArchive(window.location)
var archiveInfo = await archive.getInfo()
document.title = archiveInfo.title
$('#page-title').textContent = archiveInfo.title
// attach UI events
$('#hops-config').value = config.getHops()
$('#hops-config').addEventListener('change', onChangeHops)
// load data
var {
webRing,
links
} = await readPortal(archive, 'Paul', config.getHops())
if (links.length === 0) {
setGlobalError('Sorry! We had trouble loading the content for the page. Try refreshing.')
}
links.sort((a, b) => {
var dateDiff = (b.date.getTime() - a.date.getTime())
if (dateDiff !== 0) return dateDiff
return b.webRingDepth < a.webRingDepth ? 1 : 0
})
// render
webRing.forEach(addWebRingMemberToPage)
links.forEach(addLinkToPage)
document.getElementById("loadMore").click();
document.getElementById("loadMore").click();
}
onPageLoad()