Created
February 3, 2012 12:36
-
-
Save jelmervdl/1729968 to your computer and use it in GitHub Desktop.
Userscript that adds Google Calendar links to the De Oosterpoort website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GCal links for De Oosterpoort | |
// @namespace http://ikhoefgeen.nl/userscripts | |
// @description Create Google Calendar events directly from De Oosterpoort website | |
// @include http://www.de-oosterpoort.nl/programma/* | |
// ==/UserScript== | |
var DeOosterpoort = {} | |
DeOosterpoort.findData = function(root) | |
{ | |
var header = root.querySelector('#voorstelling h1'), | |
specs = root.querySelectorAll("#voorstelling .detail-info li > span"), | |
event = new iCal.Event; | |
if (!header || specs.size == 0) | |
return null; | |
event.name = header.firstChild.data.trim(); | |
for (var i = 0; i < specs.length; ++i) | |
{ | |
if (!specs[i].firstChild || !specs[i].nextSibling) | |
continue; | |
var label = specs[i].firstChild.data.trim(), | |
value = specs[i].nextSibling.data.trim(); | |
if (label == "Datum") { | |
if (!DeOosterpoort.parseDatum(value, event)) | |
return null; | |
} | |
else if (label == "Tijdstip") { | |
if (!DeOosterpoort.parseTijdstip(value, event)) | |
return null; | |
} | |
else if (label == "Locatie") { | |
event.location = value; | |
} | |
} | |
return event; | |
}; | |
DeOosterpoort.parseDatum = function(datum, event) | |
{ | |
var parts = datum.match(/^\w+\ (\d+) (\w+) (\d+)$/); | |
if (!parts) | |
return false; | |
var month = [ | |
'januari', 'februari', 'maart', | |
'april', 'mei', 'juni', | |
'juli', 'augustus', 'september', | |
'oktober', 'november', 'december' | |
].indexOf(parts[2]); | |
['from', 'till'].forEach(function(range) { | |
event[range].setYear(parts[3]); | |
event[range].setMonth(month); | |
event[range].setDate(parts[1]); | |
}); | |
return true; | |
}; | |
DeOosterpoort.parseTijdstip = function(tijdstip, event) | |
{ | |
var parts = tijdstip.match(/^(\d{1,2}):(\d\d)(?:\s+tot (\d{1,2}):(\d\d))?$/); | |
if (!parts) | |
return false; | |
event.from.setHours(parts[1]); | |
event.from.setMinutes(parts[2]); | |
if (typeof parts[3] !== 'undefined' && typeof parts[4] !== 'undefined') | |
{ | |
event.till.setHours(parts[3]); | |
event.till.setMinutes(parts[4]); | |
} | |
else | |
{ | |
event.till.setHours(parseInt(parts[1]) + 1); | |
event.till.setMinutes(parts[2]); | |
} | |
return true; | |
} | |
function align(length, filler, value) | |
{ | |
value = "" + value; // now it is a string for sure! :P | |
return repeat(filler, length - value.length) + value; | |
} | |
function repeat(character, times) | |
{ | |
var output = ''; | |
while (times-- > 0) | |
output += character; | |
return output; | |
} | |
var iCal = {}; | |
iCal.Event = function() { | |
this.from = new Date; | |
this.till = new Date; | |
this.name = '[name]'; | |
this.location = '[location]'; | |
this.link = document.location.href; | |
} | |
iCal.Event.prototype.toGoogleCalendarLink = function() | |
{ | |
var ical_format = 'Ymd\THis\Z'; | |
return [ | |
'http://www.google.com/calendar/event?action=TEMPLATE', | |
'text=' + encodeURIComponent(this.name), | |
'dates=' + iCal.formatDate(ical_format, this.from) + '/' + iCal.formatDate(ical_format, this.till), | |
'location=' + encodeURIComponent(this.location), | |
'trp=true', | |
'sprop=' + encodeURIComponent(this.link) | |
].join("&"); | |
} | |
iCal.formatDate = function(format, date) | |
{ | |
var output = ''; | |
for (var i = 0; i < format.length; ++i) | |
{ | |
switch (format[i]) | |
{ | |
case '\\': | |
output += format[++i]; | |
break; | |
case 'Y': | |
output += date.getUTCFullYear(); | |
break; | |
case 'm': | |
output += align(2, '0', date.getUTCMonth() + 1); | |
break; | |
case 'd': | |
output += align(2, '0', date.getUTCDate()); | |
break; | |
case 'H': | |
output += align(2, '0', date.getUTCHours()); | |
break; | |
case 'i': | |
output += align(2, '0', date.getUTCMinutes()); | |
break; | |
case 's': | |
output += align(2, '0', date.getUTCSeconds()); | |
break; | |
default: | |
output += format[i]; | |
break; | |
} | |
} | |
return output; | |
} | |
function main() | |
{ | |
var event = DeOosterpoort.findData(document); | |
if (!event) | |
return; | |
var link = document.createElement('a'); | |
link.href = event.toGoogleCalendarLink(); | |
link.target = '_blank'; | |
link.appendChild(document.createTextNode('GCal')); | |
document.querySelector('#voorstelling .detail-info li').appendChild(link); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment