-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (107 loc) · 4.13 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const LOAD_UT_SCHED_MSG = "Click to load UT semester schedule. Re-open this once you are logged in.";
const ADD_TO_CAL_MSG = "Add schedule to Google Calendar";
const INVALID_INPUT = "Please enter a name, a start date, and an end date.";
const SCHED_URL = "https://utdirect.utexas.edu/registration/classlist.WBX";
let tabId;
window.onload = function () {
// Register request handlers
chrome.runtime.onMessage.addListener(
function(request, _, sendResponse) {
switch (request.contentScriptQuery) {
case "createNewCal":
createNewCal(request.token, request.calName, id => sendResponse(id));
return true;
case "addEventToCalendar":
addEventToCalendar(request.id, request.event, request.token);
return true;
default:
return false;
}
}
);
// State based on current tabs
chrome.tabs.query({'url': SCHED_URL + "*"}, function (tabs) {
let btn = document.getElementById("btn");
let inputFields = document.getElementById("in");
if (tabs.length === 0) {
// If no schedule tab exists, open one
btn.textContent = LOAD_UT_SCHED_MSG;
btn.onclick = function () {
chrome.tabs.create({'url': SCHED_URL});
};
} else if (!tabs[0].active) {
// Tab is not highlighted, button click will highlight it
btn.textContent = LOAD_UT_SCHED_MSG;
btn.onclick = function () {
chrome.tabs.update(tabs[0].id, {highlighted: true});
};
} else {
// Ready to process schedule
tabId = tabs[0].id;
btn.textContent = ADD_TO_CAL_MSG;
btn.onclick = execScript;
inputFields.style.display = "inherit";
}
});
};
// Execute script
function execScript() {
// Authenticate to get the token we need for Google Calendar
chrome.identity.getAuthToken({'interactive': true}, function (token) {
if (token != null) {
let name = document.getElementById("cal").value;
let start = document.getElementById("start").value;
let end = document.getElementById("end").value;
if (name == null || name === '' ||
start == null || start === '' ||
end == null || end === '') {
alert(INVALID_INPUT);
return;
}
// Format message data
let data = {
token: token,
name: name,
start: start,
end: end
};
// Disable button and add loading indicator
document.getElementById("btn").disabled = true;
document.getElementById("loader").style.display = "block";
// Inject script
chrome.tabs.executeScript({file: 'process.js'}, function () {
// Send message of data to target tab
chrome.tabs.sendMessage(tabId, data);
});
}
});
}
// Request handlers
// Google Calendar object rep
function getNewCalObj(name) {
return {
"summary": name,
"timeZone": Intl.DateTimeFormat().resolvedOptions().timeZone
};
}
// Creates new Google Calendar with given name, return the id
function createNewCal(token, calName, callback) {
let body = getNewCalObj(calName);
let xhr = new XMLHttpRequest();
let url = "https://www.googleapis.com/calendar/v3/calendars?access_token=" + token;
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
let obj = JSON.parse(xhr.responseText);
callback(obj.id);
};
xhr.send(JSON.stringify(body));
}
// Async to add event to calendar
function addEventToCalendar(id, event, token) {
let eventUrl = "https://www.googleapis.com/calendar/v3/calendars/" + id + "/events?access_token=" + token;
let xhr = new XMLHttpRequest();
xhr.open("POST", eventUrl);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(event));
}