Skip to content

Instantly share code, notes, and snippets.

@ktaragorn
Created April 4, 2018 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktaragorn/a243a9e1caebd3fcc1cb978ad7a2d1b3 to your computer and use it in GitHub Desktop.
Save ktaragorn/a243a9e1caebd3fcc1cb978ad7a2d1b3 to your computer and use it in GitHub Desktop.
Add CDC (ComfortDelgro Driving Centre) class bookings to google calendar
// ==UserScript==
// @name Add CDC (ComfortDelgro Driving Centre) class bookings to google calendar
// @namespace ktaragorn
// @description Adds a link to export the class booking to google calendar so that you dont make mistakes in manual entry of date/time and miss the class
// @include https://www.cdc.com.sg/NewPortal/Booking/ReportPrView.aspx?ReceiptNo=*
// @version 2
// @grant none
// ==/UserScript==
function make_date(time_array){
var dateStr = document.querySelector("#ctl00_ContentPlaceHolder1_panDetails tbody tr:nth-child(2) td").innerText
var dateTimeArr = dateStr.split("/").reverse().concat(time_array)
dateTimeArr[1] = dateTimeArr[1] - 1 // date constructor for month is 0 based ffs
return new Date(...dateTimeArr);
}
function time_array(time_str){
var pm = time_str.endsWith("PM")
time_str = time_str.replace(" PM", "").replace(" AM","")
time_arr = time_str.split(":")
if(pm){
time_arr[0] = String(parseInt(time_arr[0]) + 12)
}
return time_arr;
}
function dateTime(time_str){
return make_date(time_array(time_str));
}
function title(){
return document.querySelector("#ctl00_ContentPlaceHolder1_panDetails tbody tr td").innerText;
}
//function description(){
//return "";
//}
// "borrowed" from https://greasyfork.org/en/scripts/1913-trademe-google-reminder/code , thank you
/*
* Return a date string as yyyymmddThhmmssZ in UTC.
* based on http://stackoverflow.com/questions/5661487/converting-date-time-to-rfc3339-format-using-either-jquery-or-java-script
*/
// Add leading zero to single digit numbers
function addZ(n) {
return (n<10) ? '0'+n : ''+n;
}
function dateToUTCString(d) {
return d.getUTCFullYear() +
addZ(d.getUTCMonth() + 1) +
addZ(d.getUTCDate()) +
'T' +
addZ(d.getUTCHours()) +
addZ(d.getUTCMinutes()) +
addZ(d.getUTCSeconds()) +
'Z';
}
function reminder_url(){
var timesArr = document.querySelector("#ctl00_ContentPlaceHolder1_panDetails tbody tr:nth-child(3) td:nth-child(2)").innerText.split(" - ")
var fromDate = dateToUTCString(dateTime(timesArr[0]));
var toDate = dateToUTCString(dateTime(timesArr[1]));
return "https://www.google.com/calendar/event?action=TEMPLATE" +
"&text=" + escape("CDC Lesson: " +title()) +
"&dates=" + fromDate + "/" + toDate
// "&details=" + escape(description());
}
//finished borrowing
function add_to_calenadar_link(){
return "<a href=" + reminder_url()+" target='_blank' onclick='return confirm(\'Are you sure?\')'><img src=\"https://www.google.com/calendar/images/ext/gc_button2.gif\"></a>"
}
document.querySelector("td span#ctl00_ContentPlaceHolder1_lblWelcome").insertAdjacentHTML("afterend", add_to_calenadar_link())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment