-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathics-gen.js
More file actions
77 lines (67 loc) · 2.32 KB
/
Copy pathics-gen.js
File metadata and controls
77 lines (67 loc) · 2.32 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
$(document).ready(function () {
if ($("[data-ics-link-gen=true]").length > 0) {
$("[data-ics-link-gen=true]").on("click", function (event) {
event.preventDefault();
let hiddenContent = $(this)
.closest(".calendar-card")
.find(".calendar-content.hide")
.text();
let parts = hiddenContent.split(";");
let title = "Echo Community E-waste Collection Day";
let address = parts[1].trim();
let date = new Date(parts[2].trim());
// Add a day to the date
date.setDate(date.getDate() + 1);
let isoDate = date.toISOString();
let dateFormatted = isoDate.slice(0, 10).replace(/-/g, "");
let timings = parts[4].trim();
let mapLink = parts[3].trim();
// Split timings into start and end times
let [startTime, endTime] = timings.split(" — ").map((time) => {
// Convert time to 24-hour format
let [hour, minutePart] = time.split(":");
let minute = minutePart.slice(0, 2);
let period = minutePart.slice(2);
hour = +hour;
if (period === "PM" && hour < 12) hour += 12;
if (period === "AM" && hour === 12) hour -= 12;
// Return time in HHmmss format
return `${hour.toString().padStart(2, "0")}${minute}00`;
});
// Append times to date
let startDate = dateFormatted + "T" + startTime;
let endDate = dateFormatted + "T" + endTime;
let icsContent = `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Echo//E-days//EN//druh.in
BEGIN:VEVENT
UID:${generateUID()}@echotech.co.nz
DTSTAMP:${
new Date()
.toISOString()
.replace(/-/g, "")
.replace(/:/g, "")
.split(".")[0]
}Z
DTSTART;TZID=Pacific/Auckland:${startDate}
DTEND;TZID=Pacific/Auckland:${endDate}
SUMMARY:${title}
DESCRIPTION:Address: ${address}\\nTimings: ${timings}
LOCATION:${address}
URL:${mapLink}
ORGANIZER;CN=Echo:mailto:info@echotech.co.nz
END:VEVENT
END:VCALENDAR`;
let downloadLink = document.createElement("a");
downloadLink.href =
"data:text/calendar;charset=utf-8," + encodeURIComponent(icsContent);
downloadLink.download = "Echo Community E-waste Collection Day.ics";
downloadLink.click();
});
}
});
function generateUID() {
return (
Math.random().toString(36).substring(2) + new Date().getTime().toString(36)
);
}