-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
76 lines (64 loc) · 1.92 KB
/
background.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* To make it a global extension
* chromium does not understand browser, but requires chrome
*/
const API = chrome || browser;
// Debugging
function onCreated(){
if(API.runtime.lastError) {
console.log(`Error: ${API.runtime.lastError}`)
} else {
console.log(`Item created successfully`)
}
}
// Selected text is date?
function findDates(str) {
const dateRegex = /\b\d{4}[-/]\d{2}[-/]\d{2}|\d{2}[-/]\d{2}[-/]\d{4}|\d{2}[-/]\d{2}[-/]\d{4}\b/g;
const matchDate = str.match(dateRegex);
if (!str || !matchDate || !matchDate[0]) {
return false;
}
const formattedDates = matchDate.map(dateString => {
const dateObject = new Date(dateString);
if (isNaN(dateObject.getTime())) {
return null;
}
return (
dateObject.getFullYear().toString() +
('0' + (dateObject.getMonth() + 1)).slice(-2) +
('0' + dateObject.getDate()).slice(-2)
);
})
.filter(Boolean);
if (formattedDates.length === 0) {
return false;
}
return formattedDates;
}
// Items to contextMenu
API.contextMenus.create({
id: "text-selection",
title:API.i18n.getMessage("menuItemTextSelect"),
contexts: ["selection"],
},onCreated)
// Context Menu Listeners
API.contextMenus.onClicked.addListener((info, tab) => {
if(info.menuItemId === "text-selection") {
let eventTitle, date;
let currentUrl = tab.url;
let selectedText = info.selectionText;
date = findDates(selectedText);
if(date) {
date = date[0]
eventTitle = "New Event";
} else {
eventTitle = selectedText;
let dateToday = new Date()
date = dateToday.getFullYear().toString() +
('0' + (dateToday.getMonth() + 1)).slice(-2) +
('0' + dateToday.getDate()).slice(-2)
}
let calUrl= `https://calendar.google.com/calendar/u/0/r/eventedit?text=${eventTitle}&details=Event+added+from+${currentUrl}&dates=${date}T100000/${date}T110000` ;
API.tabs.create({'url':calUrl})
}
});