Skip to content

Instantly share code, notes, and snippets.

@tregusti
Created April 14, 2016 20:56
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 tregusti/3d9cddef75980308c4a98877cea4ca4c to your computer and use it in GitHub Desktop.
Save tregusti/3d9cddef75980308c4a98877cea4ca4c to your computer and use it in GitHub Desktop.
(function() {
// Run this script in the console to add the foo cae event to the calendar.
'use strict';
let url = document.location.href;
let title = document.querySelector('h1').textContent;
let timestamp = document.querySelector('.eventtime p').textContent.trim();
let datematch = timestamp.match(/, (\w+) (\d+)/);
let from = new Date();
from.setMonth('January Februari March April May June July August September October November December'.split(' ').indexOf(datematch[1]));
from.setDate(datematch[2]);
from.setHours(timestamp.match(/(\d+)\.\d+ -/)[1]);
from.setMinutes(timestamp.match(/\d+\.(\d+) -/)[1]);
from.setSeconds(0);
from.setMilliseconds(0);
let to = new Date(from);
to.setHours(timestamp.match(/- (\d+)\.\d+/)[1]);
to.setMinutes(timestamp.match(/- \d+\.(\d+)/)[1]);
let columns = Array.from(document.querySelectorAll('.column'));
let text = columns.map(element => element.querySelector(':scope > p').textContent).join(' ').replace(/\n/g, '\\n');
let card = `
BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
DTSTART:${format(from)}
DTEND:${format(to)}
SUMMARY:${title}
DESCRIPTION:${text}
LOCATION:Foo Café
URL;VALUE=URI:${url}
END:VEVENT
END:VCALENDAR
`.trim();
document.location = `data:text/calendar,${encodeURIComponent(card)}`;
function format(date) {
return date.toISOString().replace(/\.\d\d\dZ/, 'Z').replace(/[-:\.]/g, '');
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment