This repository has been archived by the owner on Dec 3, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (130 loc) · 3.04 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
let queue = [];
let knownPortals = {};
let loadedPortals = {};
let attempts = 0;
let portals = [];
let results = [];
let status = {};
function getID(id) { return document.getElementById(id); }
function countValue(o, v) {
let count = 0;
for(let k in o) {
if(o[k]==v) { ++count; }
}
return count;
}
function userCount() {
return countValue(status, "found");
}
function fetching() {
return countValue(status, "fetching");
}
function missCount() {
return countValue(status, "error");
}
function addPortal(portal) {
if(loadedPortals[portal.dat]) { return; }
loadedPortals[portal.dat] = portal;
portals.push(portal);
getID("portal-count").innerHTML = userCount();
}
async function cleanURL(url) {
url = url.trim();
while(url[url.length-1] == '/') {
url = url.slice(0, -1);
}
return 'dat://'+(await DatArchive.resolveName(url)) + '/';
}
async function loadSite(url) {
if(loadedPortals[url]) return;
status[url] = "fetching";
++attempts;
try {
getID("fetch-count").innerHTML = fetching();
let archive = new DatArchive(url);
let data = await archive.readFile('/portal.json');
status[url] = "found";
let portal = JSON.parse(data);
addPortal(portal);
for(let i=0; i<portal.port.length; ++i) {
let p = await cleanURL(portal.port[i]);
if(!knownPortals[p]) {
knownPortals[p] = true;
queue.push(p);
}
}
} catch(err) {
getID("fetch-count").innerHTML = fetching();
status[url] = "error";
}
}
function escapeHTML(m)
{
return m
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function addResult(portal, message) {
let li = document.createElement('li');
li.innerHTML = `<a href="${portal.dat}">@${portal.name}</a>: ${escapeHTML(message)}`;
getID("results").appendChild(li);
}
function tick() {
while(queue.length > 0) {
let url = queue.shift();
loadSite(url).catch((e)=> {
console.log(e);
});
}
requestAnimationFrame(tick);
}
async function loadSites() {
let sitesList = new DatArchive(window.location.toString());
let data = await sitesList.readFile("/sites");
let lines = data.split('\n');
for(let l of lines) {
l = l.trim();
if(l == '' || l[0] != '@') {
continue;
}
let c = 0;
while(c < l.length && l[c] != ' ') {
++c;
}
while(c < l.length && l[c] == ' ') {
++c;
}
let url = await cleanURL(l.substring(c));
queue.push(url);
}
}
async function main() {
await loadSites();
tick();
getID("search-form").onsubmit = (e)=>{
e.preventDefault();
let time = Date.now();
getID("results").innerHTML = '';
results = [];
let pattern = getID("search").value;
for(let i=0; i<portals.length; ++i) {
let p = portals[i];
try {
for(let post of p.feed) {
if(!post.whisper && post.message.match(new RegExp(pattern, 'i')) && post.timestamp < time) {
results.push({portal:p, post:post});
}
}
} catch(e) { console.log(e); }
}
results.sort((l, r)=>{ return r.post.timestamp - l.post.timestamp; });
for(let r of results) {
addResult(r.portal, r.post.message);
}
};
}
main()