1
0
mirror of https://github.com/comatory/fb2iCal synced 2025-06-05 22:09:25 +02:00

implement same parsing logic on server and frontend

Server now downloads the HTML file via new endpoint but the parsing logic now happens in
the browser. The reason for this is to have a way same code for both
environments.

If the JavaScript is disabled, it's still possible to call the previous
endpoint and download the file from the server.
This commit is contained in:
Ondřej Synáček
2020-07-17 22:20:48 +02:00
parent 410096398d
commit f577fb6385
5 changed files with 97 additions and 30 deletions

View File

@@ -7,6 +7,9 @@ import {
saveRecord,
deleteRecord,
} from './app/storage'
import logger from './app/logger'
import { extractEventDataFromHTML } from '../../lib/services/ics-retriever'
import generateICS from '../../lib/services/ics-generator'
(() => {
if (!window.fetch || !window.Promise || !window.URLSearchParams || !window.crypto) {
@@ -168,10 +171,10 @@ import {
const postURL = (data) => {
return new Promise((resolve, reject) => {
fetch('/download', {
fetch('/download/html', {
method: 'POST',
headers: {
'Accept': 'text/calendar, application/json',
'Accept': 'text/html, application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: data,
@@ -254,6 +257,43 @@ import {
localStorage.setItem('fb-to-ical-nojs', event.target.checked)
})
const handleHTMLResponse = (html) => {
try {
setStatusParsing()
const eventData = extractEventDataFromHTML(html, { logger })
generateICS(eventData)
.then((text) => {
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:/, '') : ''
const startTimeMatches = text.match(/DTSTART:.*/)
const startTimeMatch = text.length > 0 ?
(startTimeMatches[0] || '').replace(/DTSTART:/, '') :
''
const startTime = parseStartTimeFromiCalString(startTimeMatch)
createRecord(uri, summary, startTime)
clearStatuses()
})
.catch((err) => {
handleError(err)
})
} catch (err) {
handleError(err)
}
}
submitButton.addEventListener('click', (event) => {
if (noJS()) {
return
@@ -273,28 +313,7 @@ import {
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:/, '') : ''
const startTimeMatches = text.match(/DTSTART:.*/)
const startTimeMatch = text.length > 0 ?
(startTimeMatches[0] || '').replace(/DTSTART:/, '') :
''
const startTime = parseStartTimeFromiCalString(startTimeMatch)
createRecord(uri, summary, startTime)
clearStatuses()
})
.then(handleHTMLResponse)
.catch((err) => {
handleError(err)
})