-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
238 lines (223 loc) · 7.19 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Listens for request to URL https://vtop.vit.ac.in/vtop/examinations/doDigitalAssignment and notifies content.js
// let chrome_() = chrome && browser ? browser : chrome; // Automatically chooses between chrome or firefox
function chrome_() {
try {
chrome !== undefined && browser !== undefined;
return browser;
} catch (e) {
console.log(e.message);
return chrome;
}
}
async function getVtopUri() {
return new Promise((resolve) => {
chrome_().storage.local.get(["VTOP_URI"], async function (data) {
resolve(data.VTOP_URI);
});
});
}
chrome_().webRequest.onCompleted.addListener(
async (details) => {
const URI = await getVtopUri().then((uri) => uri);
if (details.url === `${URI}/vtop/examinations/doDigitalAssignment`) {
// This line adds the content script to the page as vtop uses history.push() to remove history.
chrome_().tabs.executeScript(null, { file: "content.js" });
chrome_().tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
chrome_().tabs.sendMessage(tabs[0].id, { urlVisited: true });
}
);
}
},
{ urls: ["https://vtop.vit.ac.in/vtop/*", "https://vtopcc.vit.ac.in/vtop/*"] }
);
function calendar(date, event) {
var map = {
Jan: "01",
Feb: "02",
Mar: "03",
Apr: "04",
May: "05",
Jun: "06",
Jul: "07",
Aug: "08",
Sep: "09",
Sept: "09",
Oct: "10",
Nov: "11",
Dec: "12",
};
var tmp = date.split("-");
var dte = `${tmp[2]}-${map[tmp[1]]}-${tmp[0]}`;
return new Promise((resolve, reject) => {
chrome_().storage.local.get(["token"], async function (token) {
await fetch(
"https://www.googleapis.com/calendar/v3/calendars/primary/events?sendUpdates=all&sendNotifications=true&alt=json&key=<API_KEY>",
{
method: "POST",
headers: {
Authorization: "Bearer " + token.token,
Accept: "application/json",
},
body: JSON.stringify({
end: {
dateTime: dte + "T23:59:00.000+05:30",
},
start: {
dateTime: dte + "T23:00:00.000+05:30",
},
eventType: "default",
description: event,
summary: "Digital Assignment",
}),
}
)
.then(async (res) => {
return res.json();
})
.then((data) => {
resolve();
console.log(data);
})
.catch((err) => {
console.log(err);
reject("Error");
});
});
});
}
function get_dates(table_inner) {
return new Promise((resolve) => {
var dates = [];
for (let i = 0; i < table_inner.children.length; i++) {
var check = table_inner.children[i].children[6].children[0].innerHTML;
if (
check === "" &&
table_inner.children[i].children[4].children[0].innerHTML !== "-"
) {
dates.push({
name: table_inner.children[i].children[1].innerHTML,
date: table_inner.children[i].children[4].children[0].innerHTML,
});
}
}
resolve(dates);
});
}
async function assignments(DOM) {
try {
var regNo = DOM.getElementById("authorizedIDX").value;
var table = DOM.getElementsByClassName("customTable")[0].children[0];
var scripts = DOM.getElementsByTagName("noscript");
var csrf = scripts[0].nextElementSibling.textContent.split('"')[3];
// calendar("05-Jun-2021", "blah");
for (let i = 1; i < table.children.length; i++) {
var classid = table.children[i].children[1].innerHTML;
console.log(table.children[i].children[3].children);
table.children[i].children[3].children.length !== 0 &&
table.children[i].children[3].children[0].remove();
var desc_str =
table.children[i].children[3].textContent.trim() +
" " +
table.children[i].children[4].innerHTML;
console.log(desc_str);
const URI = await getVtopUri().then((uri) => uri);
await fetch(`${URI}/vtop/examinations/processDigitalAssignment`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
},
body: `authorizedID=${regNo}&x=${new Date().toGMTString()}&classId=${classid}&_csrf=${csrf}`,
})
.then((res) => res.text())
.then(async (data) => {
var parser = new DOMParser();
var doc = parser.parseFromString(data, "text/html");
var table_inner =
doc.getElementsByClassName("customTable")[1].children[1];
var due_date = await get_dates(table_inner)
.then((data) => data)
.catch((err) => console.log(err));
if (due_date.length > 0) {
due_date.forEach(async (due) => {
await calendar(due.date, `${desc_str} ${due.name}`);
});
chrome_().notifications.create("Done", {
type: "basic",
iconUrl: "logo.png",
title: "Sync done for " + classid,
message: desc_str,
});
}
})
.catch((err) => console.log(err));
}
} catch (err) {
console.log(err);
}
}
chrome_().runtime.onMessage.addListener(async function (
request,
sender,
sendResponse
) {
if (request.message === "sync") {
var parser = new DOMParser();
var DOM = parser.parseFromString(request.DOM, "text/html");
assignments(DOM);
} else if (request.message === "login") {
var redirect_uri = "";
try {
redirect_uri =
chrome && browser
? "http://127.0.0.1/mozoauth2/acd05041bb94a471df45afa67715fcbc49508039/"
: chrome_().identity.getRedirectURL();
} catch (e) {
console.log(e.message);
redirect_uri = chrome_().identity.getRedirectURL();
}
console.log(redirect_uri);
var url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${encodeURIComponent(
"<CLIENT_ID>.apps.googleusercontent.com"
)}&response_type=${encodeURIComponent(
"token"
)}&redirect_uri=${encodeURIComponent(
redirect_uri
)}&scope=${encodeURIComponent(
"https://www.googleapis.com/auth/calendar"
)}&state=${encodeURIComponent(
"cal" + Math.random().toString(36).substring(2, 15)
)}&prompt=${encodeURIComponent("consent")}`;
console.log(url);
chrome_().identity.launchWebAuthFlow(
{
url: url,
interactive: true,
},
function (redirect_url) {
if (chrome_().runtime.lastError) {
console.log(chrome_().runtime.lastError);
} else {
console.log(redirect_url);
let id_token = redirect_url.substring(
redirect_url.indexOf("access_token=") + 13
);
id_token = id_token.substring(0, id_token.indexOf("&"));
console.log(id_token);
chrome_().storage.local.set({ token: id_token }, () => {
console.log("set");
chrome_().notifications.create("#", {
type: "basic",
iconUrl: "logo.png",
title: "#",
message: "# Success",
});
sendResponse("success");
});
}
}
);
}
});