-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
61 lines (52 loc) · 1.83 KB
/
app.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
var addresses = []
var archives = []
var lastMessages = {}
let setup = async function() {
let me = new DatArchive(window.location.host)
setTimeout(async function() {
addressesJson = await me.readFile('addresses.json')
setupPartTwo(addressesJson)
}, 1000)
}
let setupPartTwo = async function(addressesJson) {
addresses = JSON.parse(addressesJson)
addresses.push(window.location.host)
for (let address of addresses) {
lastMessages[address] = ''
archives[address] = new DatArchive(address)
}
messageForm.addEventListener('submit', async function(event) {
event.preventDefault()
let newMessage = event.target.elements.message.value
event.target.elements.message.value = ''
await archives[window.location.host].writeFile('message.txt', newMessage)
await archives[window.location.host].commit()
})
subscribeForm.addEventListener('submit', async function(event) {
event.preventDefault()
let newAddress = event.target.elements.address.value
event.target.elements.address.value = ''
addresses.push(newAddress)
await archives[window.location.host].writeFile('addresses.json', JSON.stringify(addresses))
await archives[window.location.host].commit()
window.location.reload()
})
}
var appendNewMessage = function(message) {
let newMessageEl = document.createElement('div')
newMessageEl.innerHTML = message
window.messages.appendChild(newMessageEl)
}
let fetchMessages = async function() {
for (let address of addresses) {
let message = await archives[address].readFile('message.txt')
let nick = await archives[address].readFile('nick.txt')
if (lastMessages[address] !== message) {
appendNewMessage(`${nick} (${address.substr(0,5)}): ${message}`)
lastMessages[address] = message
}
}
}
setTimeout(() => {
setInterval(fetchMessages, 500)
},2000)