I am getting the plain text of outlook calendar events and converting them to an ics for download with Tampermonkey.
The button worked when I wrote this a couple of years ago, but now it is not. I am not sure if I need to grant permissions now or if something else broke it. The button is added just fine, but I can’t get the event listener to fire.
JavaScript
x
43
43
1
// .....
2
// @grant none
3
//===== convert text only calendar to a donwloadable ICS file
4
elementReady('#desc-content').then((element) => {
5
//grab the relevant text
6
if (element.innerHTML.includes("BEGIN:VCALENDAR")) {
7
//clean up some bit
8
var calendar = element.innerHTML.substring(0, element.innerHTML.length - '<div class="clearfix"></div>'.length).replace(/<brs*[/]?>/gi, 'n');
9
//Create a button element
10
var CalendarEventButton = document.createElement("button");
11
CalendarEventButton.innerHTML = 'Download Calendar Event';
12
13
CalendarEventButton.addEventListener("click", function() {
14
var filename = "CalendarInvite.ics"; //File name that will be downloaded
15
download(filename, calendar); //passing in the file name and text to be downloaded
16
}, false);
17
18
element.appendChild(CalendarEventButton); //append the button to the document
19
}
20
});
21
22
23
/* Download an embedded file/text*/
24
function download(file, text) {
25
26
//creating an invisible element
27
var element = document.createElement('a');
28
element.setAttribute('href',
29
'data:text/calendar;charset=utf-8, ' +
30
encodeURIComponent(text));
31
element.setAttribute('download', file);
32
33
// Above code is equivalent to
34
// <a href="path of file" download="file name">
35
36
document.body.appendChild(element);
37
38
//onClick property
39
element.click();
40
41
document.body.removeChild(element);
42
}
43
Advertisement
Answer
- Used
const
(ES2015+) instead ofvar
(<= ES5). - Cloned the element in order to remove the
div.clearfix
in a more reliable way. - Used template strings (`….`).
- Used native
GM_download
method for easier/reliable downloading.
JavaScript
1
38
38
1
// .....
2
// @grant GM_download
3
//===== convert text only calendar to a donwloadable ICS file
4
elementReady('#desc-content').then((element) => {
5
// grab the relevant text
6
const cloned = element.cloneNode(true);
7
8
if (!cloned.innerText.includes("BEGIN:VCALENDAR")) return;
9
10
// clean up some bit
11
12
// use the cloned element so that removing clearfix div
13
// won't affect DOM
14
cloned.querySelector('.clearfix')?.remove();
15
const calendar = cloned.innerHTML.replace(/<brs*[/]?>/gi, 'n');
16
17
// Create a button element
18
const calendarEventButton = document.createElement("button");
19
calendarEventButton.innerHTML = 'Download Calendar Event';
20
21
calendarEventButton.addEventListener("click", function() {
22
const filename = "CalendarInvite.ics"; // File name that will be downloaded
23
24
download(filename, calendar); // passing in the file name and text to be downloaded
25
}, false);
26
27
element.appendChild(calendarEventButton); //append the button to the document
28
});
29
30
31
/* Download an embedded file/text*/
32
function download(file, text) {
33
const encoded = encodeURIComponent(text);
34
const downloadable = `data:text/calendar;charset=utf-8, ${encoded}`;
35
36
GM_download(downloadable, file);
37
}
38