-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddToCalendar.js
43 lines (39 loc) · 1.07 KB
/
addToCalendar.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
(() => {
window.addToCalendar = (data) => {
const iOSRegexpr = /OS ((\d+_?){2,3})\s/;
if (iOSRegexpr.test(window.navigator.userAgent)) {
window.open(`data:text/calendar;charset=utf8,${createICalLink(data)}`)
} else {
window.open(createGoogleCalendarLink(data), '_blank');
}
};
const createICalLink = ({ starttime, endtime, title, location, description }) => {
const link = ([
'BEGIN:VCALENDAR',
'VERSION:2.0',
'BEGIN:VEVENT',
'DTSTAMP:20190421T170000Z',
`DTSTART:${starttime}`,
`DTEND:${endtime}`,
`LOCATION:${location}`,
`SUMMARY:${title}`,
`DESCRIPTION:${description}`,
'END:VEVENT',
'END:VCALENDAR',
]).join('\n');
return encodeURIComponent(link);
};
const createGoogleCalendarLink = ({ starttime, endtime, title, location, description }) => {
const query = {
action: 'TEMPLATE',
dates: `${starttime}/${endtime}`,
text: title,
location,
details: description,
};
return `http://www.google.com/calendar/event?${Object
.keys(query)
.map((k) => `${k}=${encodeURIComponent(query[k])}`)
.join('&')}`;
}
})();