2019-10-20 22:01:53 +02:00
|
|
|
(() => {
|
|
|
|
const noJS = () => {
|
|
|
|
return Boolean(document.querySelector("#nojs").checked)
|
|
|
|
}
|
|
|
|
|
|
|
|
const useStorage = Boolean(window.localStorage)
|
|
|
|
|
2019-11-24 21:33:26 +01:00
|
|
|
if (!window.fetch || !window.Promise || !window.URLSearchParams || !window.crypto) {
|
2019-10-20 22:01:53 +02:00
|
|
|
console.info('JS features not available.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-24 21:33:26 +01:00
|
|
|
// NOTE: Generate random IDs: https://stackoverflow.com/a/2117523/3056783
|
|
|
|
const uuidv4 = () => {
|
|
|
|
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
|
|
|
|
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
const parseStartTimeFromiCalString = (text = '') => {
|
|
|
|
const [ dateStr, timeStr ] = text.split('T')
|
|
|
|
const rawDate = dateStr || ''
|
|
|
|
const rawTime = timeStr || ''
|
|
|
|
|
|
|
|
const year = Number(rawDate.slice(0, 4))
|
|
|
|
const month = Number(Math.max(rawDate.slice(4, 6) - 1), 0)
|
|
|
|
const date = Number(rawDate.slice(6, 8))
|
|
|
|
const hour = Number(rawTime.slice(0, 2))
|
|
|
|
const minutes = Number(rawTime.slice(2, 4))
|
|
|
|
const seconds = Number(rawTime.slice(4, 6))
|
|
|
|
|
|
|
|
const parsedDate = new Date(year, month, date, hour, minutes, seconds)
|
|
|
|
return parsedDate.toString()
|
|
|
|
}
|
|
|
|
|
2019-11-24 21:33:26 +01:00
|
|
|
const migrateRecord = (record) => {
|
2019-11-24 22:22:02 +01:00
|
|
|
// NOTE: v3 records
|
2019-11-24 21:33:26 +01:00
|
|
|
const id = record.id || record.order
|
2019-11-24 22:22:02 +01:00
|
|
|
const startTime = record.startTime || null
|
2019-11-24 21:33:26 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
...record,
|
|
|
|
id,
|
2019-11-24 22:22:02 +01:00
|
|
|
startTime,
|
2019-11-24 21:33:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:01:53 +02:00
|
|
|
const getStorage = () => {
|
|
|
|
if (!useStorage) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const storage = localStorage.getItem('fb-to-ical-events')
|
|
|
|
|
|
|
|
if (!storage) {
|
|
|
|
localStorage.setItem('fb-to-ical-events', JSON.stringify([]))
|
|
|
|
return "[]"
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage
|
|
|
|
}
|
|
|
|
|
|
|
|
const getStorageContents = (storage) => {
|
|
|
|
return JSON.parse(storage)
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateStorage = (storageContents) => {
|
|
|
|
const encodedStorage = JSON.stringify(storageContents)
|
|
|
|
|
|
|
|
localStorage.setItem('fb-to-ical-events', encodedStorage)
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
const saveRecord = ({ id, link, createdAt, startTime, title }) => {
|
2019-10-20 22:01:53 +02:00
|
|
|
if (!useStorage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const storage = getStorage()
|
|
|
|
const storageContents = getStorageContents(storage)
|
|
|
|
|
|
|
|
const record = {
|
2019-11-24 21:33:26 +01:00
|
|
|
id,
|
2019-10-20 22:01:53 +02:00
|
|
|
link,
|
|
|
|
createdAt: createdAt.toString(),
|
2019-11-24 22:22:02 +01:00
|
|
|
startTime: startTime.toString(),
|
2019-10-20 22:01:53 +02:00
|
|
|
title,
|
|
|
|
}
|
|
|
|
|
|
|
|
updateStorage([ ...storageContents, record ])
|
|
|
|
}
|
|
|
|
|
2019-11-24 21:33:26 +01:00
|
|
|
const deleteRecord = (id) => {
|
2019-11-10 16:35:54 +01:00
|
|
|
if (!useStorage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const storage = getStorage()
|
|
|
|
const storageContents = getStorageContents(storage)
|
|
|
|
const index = storageContents.findIndex((record) => {
|
2019-11-24 21:33:26 +01:00
|
|
|
return record.id === id
|
2019-11-10 16:35:54 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!Number.isFinite(index)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextStorage = [ ...storageContents ]
|
|
|
|
nextStorage.splice(index, 1)
|
|
|
|
|
|
|
|
updateStorage(nextStorage)
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:01:53 +02:00
|
|
|
const showTable = () => {
|
|
|
|
list.classList.remove('hidden')
|
|
|
|
}
|
|
|
|
|
2019-11-24 21:33:26 +01:00
|
|
|
const deleteTableRow = (id, row) => {
|
|
|
|
deleteRecord(id)
|
2019-11-10 16:35:54 +01:00
|
|
|
|
|
|
|
if (!useStorage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
row.remove()
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
const insertTableRow = ({ id, link, createdAt, startTime, title }) => {
|
2019-10-20 22:01:53 +02:00
|
|
|
showTable()
|
|
|
|
|
2019-11-10 16:35:54 +01:00
|
|
|
const newRow = document.createElement('tr')
|
2019-10-20 22:01:53 +02:00
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
const startTimeCol = document.createElement('td')
|
|
|
|
startTimeCol.innerText = startTime ?
|
|
|
|
new Date(startTime).toLocaleString() :
|
|
|
|
'N/A\xa0\xa0\xa0\xa0\xa0'
|
2019-10-20 22:01:53 +02:00
|
|
|
|
2019-11-10 16:35:54 +01:00
|
|
|
const downloadEl = document.createElement('a')
|
|
|
|
downloadEl.setAttribute('href', link)
|
2019-11-24 22:37:36 +01:00
|
|
|
downloadEl.innerText = title
|
|
|
|
|
|
|
|
const titleCol = document.createElement('td')
|
|
|
|
titleCol.appendChild(downloadEl)
|
2019-11-10 16:35:54 +01:00
|
|
|
|
|
|
|
const deleteEl = document.createElement('a')
|
|
|
|
deleteEl.setAttribute('href', 'javascript:void(0)')
|
|
|
|
deleteEl.innerText = '✖︎'
|
2019-11-10 16:48:04 +01:00
|
|
|
deleteEl.classList.add('delete-record')
|
2019-11-10 16:35:54 +01:00
|
|
|
deleteEl.addEventListener('click', (event) => {
|
|
|
|
event.preventDefault()
|
2019-11-24 21:33:26 +01:00
|
|
|
deleteTableRow(id, newRow)
|
2019-11-10 16:35:54 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const actionCol = document.createElement('td')
|
2019-11-10 16:48:04 +01:00
|
|
|
actionCol.classList.add('actions')
|
2019-11-10 16:35:54 +01:00
|
|
|
actionCol.appendChild(deleteEl)
|
2019-10-20 22:01:53 +02:00
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
newRow.appendChild(startTimeCol)
|
2019-10-20 22:01:53 +02:00
|
|
|
newRow.appendChild(titleCol)
|
2019-11-10 16:35:54 +01:00
|
|
|
newRow.appendChild(actionCol)
|
2019-10-20 22:01:53 +02:00
|
|
|
|
|
|
|
tableBody.prepend(newRow)
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
const createRecord = (uri, summary, startTime) => {
|
2019-11-24 21:33:26 +01:00
|
|
|
const id = uuidv4()
|
2019-10-20 22:01:53 +02:00
|
|
|
const createdAt = new Date()
|
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
saveRecord({
|
|
|
|
id,
|
|
|
|
link: uri,
|
|
|
|
createdAt,
|
|
|
|
startTime,
|
|
|
|
title: summary,
|
|
|
|
})
|
2019-10-20 22:01:53 +02:00
|
|
|
insertTableRow({
|
2019-11-24 21:33:26 +01:00
|
|
|
id,
|
2019-10-20 22:01:53 +02:00
|
|
|
link: uri,
|
|
|
|
createdAt,
|
|
|
|
title: summary,
|
2019-11-24 22:22:02 +01:00
|
|
|
startTime
|
2019-10-20 22:01:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const hydrateList = () => {
|
|
|
|
if (!useStorage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-24 21:33:26 +01:00
|
|
|
const prevStorage = getStorage()
|
|
|
|
const migratedStorageContents = getStorageContents(prevStorage).map((record) => {
|
|
|
|
return migrateRecord(record)
|
|
|
|
})
|
|
|
|
updateStorage(migratedStorageContents)
|
2019-10-20 22:01:53 +02:00
|
|
|
const storage = getStorage()
|
|
|
|
const storageContents = getStorageContents(storage)
|
|
|
|
|
|
|
|
if (storageContents.length > 0) {
|
|
|
|
showTable()
|
|
|
|
}
|
|
|
|
|
|
|
|
storageContents
|
|
|
|
.sort((a, b) => {
|
2019-11-24 21:36:15 +01:00
|
|
|
const aDate = new Date(a.createdAt)
|
|
|
|
const bDate = new Date(b.createdAt)
|
|
|
|
if (aDate < bDate) {
|
2019-10-20 22:01:53 +02:00
|
|
|
return -1
|
|
|
|
}
|
2019-11-24 21:36:15 +01:00
|
|
|
if (aDate > bDate) {
|
2019-10-20 22:01:53 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
.forEach((record) => {
|
|
|
|
insertTableRow(record)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const clearStatuses = () => {
|
|
|
|
document.querySelectorAll('.status-item').forEach((item) => {
|
|
|
|
item.classList.remove('show')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const setStatusDownloading = () => {
|
|
|
|
clearStatuses()
|
|
|
|
document.querySelector('#network').classList.add('show')
|
|
|
|
}
|
|
|
|
|
|
|
|
const setStatusParsing = () => {
|
|
|
|
clearStatuses()
|
|
|
|
document.querySelector('#parsing').classList.add('show')
|
|
|
|
}
|
|
|
|
|
|
|
|
const setStatusError = (err) => {
|
|
|
|
clearStatuses()
|
|
|
|
const error = document.querySelector('#error')
|
|
|
|
error.innerText = err.toString()
|
|
|
|
error.classList.add('show')
|
|
|
|
}
|
|
|
|
|
|
|
|
const setServiceWorkerStatus = (status) => {
|
|
|
|
clearStatuses()
|
|
|
|
const sw = document.querySelector('#service-worker')
|
|
|
|
sw.innerText = status
|
|
|
|
status ? sw.classList.add('show') : sw.classList.remove('show')
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingRequest = () => {
|
|
|
|
input.disabled = true
|
|
|
|
submitButton.disabled = true
|
|
|
|
setStatusDownloading()
|
|
|
|
}
|
|
|
|
|
|
|
|
const finishedRequest = () => {
|
|
|
|
input.disabled = false
|
|
|
|
submitButton.disabled = false
|
|
|
|
clearStatuses()
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleError = (error) => {
|
|
|
|
finishedRequest()
|
|
|
|
setStatusError(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const postURL = (data) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fetch('/download', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2019-10-21 21:20:03 +02:00
|
|
|
'Accept': 'text/calendar, application/json',
|
2019-10-20 22:01:53 +02:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
body: data,
|
|
|
|
}).then((response) => {
|
2019-10-21 21:20:03 +02:00
|
|
|
if (response.status !== 200) {
|
|
|
|
if (response.body.constructor === ReadableStream) {
|
|
|
|
response.json().then((json) => reject(json.error || response.statusText))
|
|
|
|
return
|
|
|
|
}
|
2019-10-20 22:01:53 +02:00
|
|
|
reject(response.statusText)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
finishedRequest()
|
|
|
|
resolve(response)
|
|
|
|
}).catch(reject)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const form = document.querySelector('form')
|
|
|
|
const submitButton = document.querySelector("#submit")
|
|
|
|
const input = document.querySelector("#url")
|
|
|
|
const link = document.querySelector("#current-download")
|
|
|
|
const table = document.querySelector('#list')
|
|
|
|
const tableBody = table.querySelector('tbody')
|
|
|
|
const noJScheckbox = document.querySelector('#nojs')
|
|
|
|
|
|
|
|
const loadNoJS = () => {
|
|
|
|
if (!useStorage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const value = localStorage.getItem('fb-to-ical-nojs')
|
|
|
|
noJScheckbox.checked = value ? JSON.parse(value) : false
|
|
|
|
}
|
|
|
|
|
|
|
|
loadNoJS()
|
|
|
|
|
|
|
|
if (window.navigator && window.navigator.serviceWorker && !noJS()) {
|
|
|
|
const serviceWorker = window.navigator.serviceWorker
|
|
|
|
serviceWorker.register('service-worker.js', {
|
|
|
|
scope: './',
|
|
|
|
}).then((registration) => {
|
|
|
|
setServiceWorkerStatus(`Service worker registered with scope ${registration.scope}`)
|
|
|
|
setTimeout(() => {
|
|
|
|
setServiceWorkerStatus('')
|
|
|
|
}, 4500)
|
2019-10-20 22:29:00 +02:00
|
|
|
|
|
|
|
registration.addEventListener('updatefound', () => {
|
|
|
|
console.info('Service worker will be updated...')
|
|
|
|
const newWorker = registration.installing
|
|
|
|
|
|
|
|
newWorker.addEventListener('statechange', () => {
|
|
|
|
if (newWorker.state === 'installed') {
|
|
|
|
newWorker.postMessage({ action: 'skipWaiting' })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-10-20 22:01:53 +02:00
|
|
|
}).catch((err) => {
|
|
|
|
setServiceWorkerStatus(`Service worker error: ${err.toString()}`)
|
|
|
|
})
|
2019-10-20 22:29:00 +02:00
|
|
|
|
|
|
|
let refreshing
|
|
|
|
serviceWorker.addEventListener('controllerchange', () => {
|
|
|
|
if (refreshing) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
window.location.reload()
|
|
|
|
refreshing = true
|
|
|
|
})
|
2019-10-20 22:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!noJS()) {
|
|
|
|
hydrateList()
|
|
|
|
}
|
|
|
|
|
|
|
|
noJScheckbox.addEventListener('click', (event) => {
|
|
|
|
if (!useStorage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem('fb-to-ical-nojs', event.target.checked)
|
|
|
|
})
|
|
|
|
|
|
|
|
submitButton.addEventListener('click', (event) => {
|
|
|
|
if (noJS()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-22 20:25:09 +02:00
|
|
|
if (!form.reportValidity()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:01:53 +02:00
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
const formData = new URLSearchParams()
|
|
|
|
formData.set('url', input.value)
|
|
|
|
|
|
|
|
pendingRequest()
|
|
|
|
|
|
|
|
postURL(formData)
|
|
|
|
.then((res) => {
|
|
|
|
res.text()
|
|
|
|
.then((text) => {
|
|
|
|
setStatusParsing()
|
|
|
|
const dataUri = encodeURIComponent(text)
|
|
|
|
const uri = `data:text/calendar;charset=utf-8,${dataUri}`
|
|
|
|
|
|
|
|
link.setAttribute('href', uri)
|
|
|
|
link.setAttribute('download', 'download.ics')
|
|
|
|
link.click()
|
|
|
|
|
|
|
|
input.value = ''
|
|
|
|
|
|
|
|
const summaryMatch = text.match(/SUMMARY:.*/)[0]
|
|
|
|
const summary = summaryMatch ? summaryMatch.replace(/SUMMARY:/, '') : ''
|
2019-11-24 22:22:02 +01:00
|
|
|
const startTimeMatches = text.match(/DTSTART:.*/)
|
|
|
|
const startTimeMatch = text.length > 0 ?
|
|
|
|
(startTimeMatches[0] || '').replace(/DTSTART:/, '') :
|
|
|
|
''
|
|
|
|
const startTime = parseStartTimeFromiCalString(startTimeMatch)
|
2019-10-20 22:01:53 +02:00
|
|
|
|
2019-11-24 22:22:02 +01:00
|
|
|
createRecord(uri, summary, startTime)
|
2019-10-20 22:01:53 +02:00
|
|
|
clearStatuses()
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
handleError(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
handleError(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})()
|