-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
47 lines (38 loc) · 1.16 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// YYYYMMDDTHHMMSSZ
function formatDt(date) {
return date.toISOString().slice(0,-5).replace(/[-:]/g, "") + "Z";
}
// https://github.com/wanasit/chrono#parsedcomponents
// YYYYMMDDTHHMMSSZ/YYYYMMDDTHHMMSSZ
function timeIntervalStr(parsedComponent) {
var start = parsedComponent.start.date();
var end = null;
if (parsedComponent.end) {
end = parsedComponent.end.date();
} else {
end = parsedComponent.start.date();
end.setHours(end.getHours() + 1);
}
return formatDt(start) + '/' + formatDt(end);
}
function openEventCreationTab(info) {
var desc = info.selectionText;
var datetimes = chrono.casual.parse(desc);
var gCalParams = new URLSearchParams({
"action": 'TEMPLATE',
// "text": titleText,
"dates": timeIntervalStr(datetimes[0]),
// "location": locationText,
"details": desc + "\n\n--\n" + encodeURI(info.pageUrl),
"trp;": true,
'gsessionid': 'OK',
'output': 'xml'
});
var url = "https://calendar.google.com/calendar/render?" + gCalParams.toString();
chrome.tabs.create({"url": url})
}
chrome.contextMenus.create({
title: "Add to GCal",
contexts: ["selection"],
onclick: openEventCreationTab
});