This repository was archived by the owner on Nov 7, 2024. It is now read-only.
forked from ad3978/flashfocus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicsalert.js
51 lines (39 loc) · 1.44 KB
/
icsalert.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
function formatDate(date) {
return date.toISOString().slice(0, 10).replace(/-/g, '');
}
const startDate = document.querySelector('#startDate');
const endDate = document.querySelector('#endDate');
const today = new Date();
startDate.valueAsDate = today;
endDate.valueAsDate = today;
startDate.addEventListener('change', function() {
endDate.min = startDate.value;
});
function createICS() {
const event = {
start: startDate.value,
end: endDate.value,
summary: document.querySelector('#summary').value,
description: document.querySelector('#description').value
};
const icsContent =
'BEGIN:VCALENDAR\n' +
'VERSION:2.0\n' +
'PRODID:-//Test Cal//EN\n' +
'BEGIN:VEVENT\n' +
'UID:' + Math.random().toString(36).substr(2, 9) + '\n' +
'DTSTAMP:' + formatDate(new Date()) + 'T000000Z\n' +
'DTSTART:' + formatDate(new Date(event.start)) + 'T000000Z\n' +
'DTEND:' + formatDate(new Date(event.end)) + 'T000000Z\n' +
'SUMMARY:' + event.summary + '\n' +
'DESCRIPTION:' + event.description + '\n' +
'END:VEVENT\n' +
'END:VCALENDAR';
const icsBlob = new Blob([icsContent], { type: 'text/calendar;charset=utf-8' });
const downloadLink = document.createElement('a');
downloadLink.download = 'event.ics';
downloadLink.href = window.URL.createObjectURL(icsBlob);
downloadLink.click();
}
const createButton = document.querySelector('#create');
createButton.addEventListener('click', createICS);