-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
384 lines (337 loc) · 11.9 KB
/
popup.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//Frontend JS
let auth_token = "";
// tried using local storage, but it interanally saves the token in the "" else uses the profile present in chrome or uses the cached token present in chrome
chrome.storage.local.clear(() => {
console.log("cleared");
});
document.getElementById("connect-account-btn").onclick = async function () {
await chrome.storage.local.get(["user_auth_token"]).then((result) => {
auth_token = result;
console.log(auth_token);
});
if (auth_token.hasOwnProperty("user_auth_token")) {
console.log("Token already generated and present");
document.getElementById("connect-google-account-container").style.display =
"none";
document.getElementById("accounts-info-container").style.display = "block";
// document.getElementById("logout-btn").style.display = "block";
} else {
console.log("Generating new token");
try {
const token = await chrome.identity.getAuthToken({ interactive: true });
auth_token = token.token;
await chrome.storage.local.set({ user_auth_token: token });
console.log("User token:", auth_token);
} catch (error) {
console.error(error);
}
document.getElementById("connect-google-account-container").style.display =
"none";
document.getElementById("accounts-info-container").style.display = "block";
// document.getElementById("logout-btn").style.display = "block";
}
};
//Google API interaction prerequisites for JS client side app
const CLIENT_ID = "<Enter your oauth(chrome extension)client id>";
const API_KEY = "<Enter your API key >";
const DISCOVERY_DOC =
"https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest";
const SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"];
async function injectCode(src, callback_func) {
const script = document.createElement("script");
// This is why it works!
script.src = src;
script.onload = async function () {
console.log("script injected");
await callback_func();
this.remove();
};
// This script runs before the <head> element is created,
// so we add the script to <html> instead.
(document.head || document.documentElement).appendChild(script);
}
injectCode(
// chrome.runtime.getURL("https://apis.google.com/js/api.js"),
chrome.runtime.getURL("google/api.js")
// gapiLoaded
);
injectCode(
// chrome.runtime.getURL("https://accounts.google.com/gsi/client"),
chrome.runtime.getURL("google/client.js")
// gisLoaded
);
document.getElementById("get-results-btn").onclick = async function () {
try {
console.log(JSON.stringify(auth_token));
const auth = JSON.stringify(auth_token);
const container = document.getElementById("results-div");
container.style.display = "block";
const node_loader = document.createElement("div");
node_loader.className = "loader";
container.appendChild(node_loader);
const response = await getEmails(auth);
console.log(response);
if (response.length === 0) {
container.innerHTML = "";
const no_results_text = document.createElement("p");
const no_results_p_content = document.createTextNode(
"There are no latest OTP emails"
);
no_results_text.appendChild(no_results_p_content);
container.appendChild(no_results_text);
} else {
container.innerHTML = "";
for (var i = 0; i < response.length; i++) {
console.log(response);
//display sender_email
const node_sender_email = document.createElement("p");
node_sender_email.className = "sender_email";
const sender_email_text = document.createTextNode(
"Sender: " + " " + JSON.stringify(response[i]["sender_email"])
);
//display date
const node_date = document.createElement("span");
node_date.className = "date";
const date_text = document.createTextNode(
"Date: " + " " + JSON.stringify(response[i]["date"])
);
//display otp
const node_otp = document.createElement("p");
node_otp.className = "otp";
const otp_text = document.createTextNode(
JSON.stringify(response[i]["otp"])
);
//display break
const node_br = document.createElement("br");
const node_hr = document.createElement("hr");
node_sender_email.appendChild(sender_email_text);
node_date.appendChild(date_text);
node_otp.appendChild(otp_text);
container.appendChild(node_sender_email);
container.appendChild(node_date);
container.appendChild(node_br);
container.appendChild(node_otp);
container.appendChild(node_hr);
}
}
} catch (error) {
console.error("Error fetching data:", error);
}
};
// interaction with the Gmail API
function extractOtp(email_body) {
console.log("i'm extract oTP");
const otpRegex =
// To authenticate your account, please enter the following code:\nHere's your Code/;
/\b\d{6}\b/;
const match = email_body.match(otpRegex);
return match ? match[0] : null;
}
function extractTime(email_received_value) {
const timeRegex = /\b\d{1,2}\s\w{3}\s\d{4}\b/;
const match = email_received_value.match(timeRegex);
return match ? match[0] : null;
}
// function to decode the body of the email
// function processParts(parts) {
// let bodyMessage, bodyHtml;
// for (const part of parts) {
// const body = part.body;
// const data = body.data;
// const mimeType = part.mimeType;
// if (mimeType === "multipart/alternative") {
// const subparts = part.parts;
// [bodyMessage, bodyHtml] = processParts(subparts);
// } else if (mimeType === "text/plain") {
// console.log("text/plain");
// bodyMessage = Buffer.from(data, "base64").toString("utf-8");
// } else if (mimeType === "text/html") {
// console.log("text/html");
// bodyHtml = Buffer.from(data, "base64").toString("utf-8");
// }
// }
// return [bodyMessage, bodyHtml];
// }
// function processParts(parts) {
// let bodyMessage = [];
// let bodyHtml = [];
// for (const part of parts) {
// const body = part.body;
// const data = body.data;
// const mimeType = part.mimeType;
// if (mimeType === "multipart/alternative") {
// const subparts = part.parts;
// const [subBodyMessage, subBodyHtml] = processParts(subparts);
// bodyMessage = bodyMessage.concat(subBodyMessage);
// bodyHtml = bodyHtml.concat(subBodyHtml);
// } else if (mimeType === "text/plain") {
// console.log("text/plain");
// bodyMessage.push(Buffer.from(data, "base64").toString("utf-8"));
// } else if (mimeType === "text/html") {
// console.log("text/html");
// bodyHtml.push(Buffer.from(data, "base64").toString("utf-8"));
// }
// }
// return [bodyMessage, bodyHtml];
// }
var user_inbox_message_id = [];
var user_inbox_messages = [];
async function getEmails_new(auth) {
const url = `https://gmail.googleapis.com/gmail/v1/users/me/messages`;
try {
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${auth}`,
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
console.log(data);
return data;
} catch (error) {
console.error("Error fetching email message:", error);
throw error;
}
}
async function getEmails(auth) {
const data = []; //array of objects
const otp_mail_subjects = [];
const msg_id_url = `https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=20`;
const user_inbox_message_ids = await fetch(msg_id_url, {
method: "GET",
headers: {
Authorization: `Bearer ${auth}`,
"Content-Type": "application/json",
},
});
const ids = await user_inbox_message_ids.json();
// console.log(ids);
for (var i = 0; i < ids.messages.length; i++) {
const final_mail_data = {
sender_email: "",
otp: "",
date: "",
}; //contains subject and OTP
var current_id = ids.messages[i]["id"];
const msg_url = `https://gmail.googleapis.com/gmail/v1/users/me/messages/${current_id}`;
const current_message = await fetch(msg_url, {
method: "GET",
headers: {
Authorization: `Bearer ${auth}`,
"Content-Type": "application/json",
},
});
const message = await current_message.json();
// console.log(message.snippet);
console.log(message.payload.headers);
current_message_subject = message.payload.headers.forEach(function (e) {
if (
(e.name == "Subject" &&
e.value.toLowerCase().includes("here's your authentication code")) ||
(e.name == "From" &&
e.value.toLowerCase().includes("here's your authentication code"))
) {
const payload = message.payload;
const parts = payload.parts;
//to send the message body to extarct OTP
// const [bodyMessage, bodyHtml] = processParts(parts);
// const finalResult = bodyMessage.toString("utf-8");
// final_mail_data["subject"] = e.value;
//to send the message snippet to extarct OTP use - final_mail_data["otp"] = extractOtp(message.snippet);
final_mail_data["otp"] = extractOtp(message.snippet);
payload.headers.forEach((e) => {
e.name == "Date"
? (final_mail_data["date"] = extractTime(e.value))
: null;
//
});
payload.headers.forEach((e) => {
e.name == "Reply-To" || e.name == "From"
? (final_mail_data["sender_email"] = e.value)
: null;
});
data.push(final_mail_data);
}
});
}
return data;
}
// function to list labels - basic function to verify interaction with gmail API
// async function listLabels() {
// let response;
// try {
// response = await gapi.client.gmail.users.labels.list({
// userId: "me",
// auth: {
// headers: {
// Authorization: "Bearer " + tokenClient.token,
// },
// },
// });
// } catch (err) {
// // document.getElementById("get-results-btn").innerText = err.message;
// console.log(
// "error in response = await gapi.client.gmail.users.labels.list " +
// err.message
// );
// return;
// }
// const labels = response.result.labels;
// if (!labels || labels.length == 0) {
// document.getElementById("get-results-btn").innerText = "No labels found.";
// return;
// }
// // Flatten to string to display
// const output = labels.reduce(
// (str, label) => `${str}${label.name}\n`,
// "Labels:\n"
// );
// document.getElementById("get-results-btn").innerText = output;
// }
//js client interaction with gmail, was not working directly for chrome extensin client
// let gapiInited = false;
// let gisInited = false;
// function gapiLoaded() {
// // console.log("gapi called");
// gapi.load("client", initializeGapiClient);
// }
// async function initializeGapiClient() {
// await gapi.client.init({
// apiKey: API_KEY,
// discoveryDocs: [DISCOVERY_DOC],
// });
// gapiInited = true;
// maybeEnableButtons();
// }
// function maybeEnableButtons() {
// if (gapiInited && gisInited) {
// document.getElementById("get-results-btn").style.display = "block";
// // console.log("button enabled");
// }
// }
// async function gisLoaded() {
// // console.log("gisloaded called");
// // tokenClient = google.accounts.oauth2.initTokenClient({
// // client_id: CLIENT_ID,
// // scope: SCOPES,
// // // callback: "", // defined later
// // });
// await chrome.storage.local.get(["user_auth_token"]).then((result) => {
// auth_token = result;
// console.log(auth_token["user_auth_token"]);
// });
// tokenClient = {
// client_id: CLIENT_ID,
// scope: SCOPES,
// // callback: "", // defined later
// token: auth_token["user_auth_token"],
// };
// // console.log(tokenClient.token);
// // console.log(tokenClient.scope);
// gisInited = true;
// maybeEnableButtons();
// }