Skip to content

Instantly share code, notes, and snippets.

@krmax44
Last active May 30, 2022 11:57
Show Gist options
  • Save krmax44/51c612f0216a67d46a93b2bec99a4f70 to your computer and use it in GitHub Desktop.
Save krmax44/51c612f0216a67d46a93b2bec99a4f70 to your computer and use it in GitHub Desktop.
re:publica 22 Schedule iCal export userscript
// ==UserScript==
// @name re:publica iCal Export
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Export sessions to iCal
// @author You
// @match https://re-publica.com/de/session/*
// @match https://re-publica.com/en/node/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=re-publica.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
const downloadLink = document.createElement('a');
downloadLink.href = '#!';
downloadLink.innerText = 'Download iCal';
const dateToIso = d => {
d.setHours(d.getHours() + 2)
return d.toISOString()
.replaceAll('-', '')
.replaceAll(':', '')
.replace(/\.\d+Z$/, 'Z');
};
const escape = s => s.replaceAll('\n', '\\n').replaceAll(',', '\\,').replaceAll(';', '\\;').replaceAll(':', '\\:');
const times = document.querySelectorAll('time');
console.log(times);
const [start, end] = [...times].map(el => dateToIso(new Date(el.getAttribute('datetime'))));
const now = dateToIso(new Date());
const id = (Math.random() + 1).toString(36).substring(7);
const title = escape(document.querySelector('.title.page-title').innerText);
const location = escape(document.querySelector('.field--name-field-room').innerText);
const speakers = escape(document.querySelector('.big-speaker-list').innerText);
const ical = `BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VEVENT
CREATED:${now}
DTSTAMP:${now}
LAST-MODIFIED:${now}
SEQUENCE:2
UID:rp22-${id}
DTSTART;TZID=Europe/Berlin:${start}
DTEND;TZID=Europe/Berlin:${end}
STATUS:CONFIRMED
SUMMARY:re: ${title}
LOCATION:${location}
DESCRIPTION:${speakers}\\n${document.location.href}
END:VEVENT
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
END:VCALENDAR`;
downloadLink.href = window.URL.createObjectURL(new Blob([ical], {type: 'text/calendar'}));
downloadLink.download = `${id}.ical`;
const container = document.createElement('div');
container.classList.add('field', 'field--name-field-language', 'field--type-entity-reference', 'field--label-hidden', 'field__item');
container.appendChild(downloadLink);
document.querySelector('.ticket-meta').appendChild(container);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment