Skip to content

Instantly share code, notes, and snippets.

@astrarudra
Last active May 11, 2024 13:12
Show Gist options
  • Save astrarudra/b76c45d8790f08ab515bcd7905e07c69 to your computer and use it in GitHub Desktop.
Save astrarudra/b76c45d8790f08ab515bcd7905e07c69 to your computer and use it in GitHub Desktop.
This script exports greytHR holidays in iCal format, Used for syncing holidays to calendars - Apple, Google, Outlook, etc.
/*
Author: Rudra Roy
Description:
This script exports greytHR holidays in iCal format.
Used for syncing holidays to calendars - Apple, Google, Outlook, etc.
*/
// Constants for customizing holiday Reason
const prefix = 'Holiday: '
const optPostFix = ' (Optional)'
// Constants for greytHR holiday calendar - Needs update if greytHR changes their UI
const GHRConst = {
month: '.month-container',
title: '.month-title',
holiday: '.month-holiday-list-item',
optIdentifier: 'Apply'
}
// Function to format date in iCal format
function formatDateForICal(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}${month}${day}`;
}
// Function to generate iCal content
function generateICalContent(holidays) {
let c = 'BEGIN:VCALENDAR\n';
c += 'VERSION:2.0\n';
c += 'PRODID:-//Greythr//Holiday Calendar//EN\n';
holidays.forEach(holiday => {
let icalDate = formatDateForICal(holiday.date)
c += 'BEGIN:VEVENT\n';
c += `UID:${icalDate}@greythr.com\n`;
c += `DTSTAMP:${icalDate}\n`;
c += `DTSTART;VALUE=DATE:${icalDate}\nDTEND;VALUE=DATE:${icalDate};\n`
c += `SUMMARY:${holiday.reason}\n`;
c += `DESCRIPTION:${holiday.reason}\n`;
c += 'END:VEVENT\n';
});
c += 'END:VCALENDAR\n';
return c;
}
// Function to download iCal file
function downloadICalFile(content, filename) {
const blob = new Blob([content], { type: 'text/calendar' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.setAttribute('download', filename);
link.click();
}
// Function to parse greythr holiday calendar
function parseGreythrHolidays(reasonPrefix = prefix){
let year_holidays = []
let monthDivs = document.querySelectorAll(GHRConst.month)
monthDivs.forEach(month => {
let monthYear = month.querySelector(GHRConst.title).textContent
let holidays = month.querySelectorAll(GHRConst.holiday)
holidays.forEach(holiday => {
let details = holiday.textContent
let splitDetails = details.trim().split(/\s+/)
let day = splitDetails[0]
let optional = false
if(splitDetails[splitDetails.length-1] === GHRConst.optIdentifier){
optional = true
splitDetails = splitDetails.slice(0, splitDetails.length-1)
}
let reason = reasonPrefix + splitDetails.slice(2).join(' ')
if(optional) reason += optPostFix
const date = new Date(day + " " + monthYear)
year_holidays.push({date,reason})
})
})
return year_holidays
}
// Driver to download greythr holidays iCal
const holidays = parseGreythrHolidays(prefix)
const ical = generateICalContent(holidays);
downloadICalFile(ical, 'greytHRHolidays.ics');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment