-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutlook.js
649 lines (566 loc) · 19.3 KB
/
outlook.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
export class outlookJS {
constructor(localJQuery) {
this.callbacks = new Map();
this.cache = {};
this.cache.rawConversationMap = new Map();
this.cache.processedConversationMap = new Map();
this.observer = this.createObserver();
this.fetchObservers = {
requestObservers: [],
responseObservers: [],
};
this.addFetchObserver("cacheEmailThreads", this.cacheEmailThreads); // Add observer by default to start caching threads
this.initFetchWatcher();
var $;
if (typeof localJQuery !== "undefined") {
$ = localJQuery;
} else if (typeof jQuery !== "undefined") {
$ = jQuery;
} else {
// try load jQuery through node.
try {
$ = require("jquery");
} catch (err) {
// else leave $ undefined, which may be fine for some purposes.
}
}
this.observe = {
on: (eventName, callback) => {
this.on(eventName, callback);
},
off: (eventName, callback) => {
this.off(eventName, callback);
},
on_dom: (eventName, callback) => {
const className = this.getClassNameForEvent(eventName);
if (className) {
this.on_dom(className, callback);
}
},
off_dom: (eventName, callback) => {
const className = this.getClassNameForEvent(eventName);
if (className) {
this.off_dom(className, callback);
}
},
};
this.get = {
userSessions: () => this.getUserSessions(),
currentEmailAddress: () => this.getCurrentEmailAddress(),
userEmail: () => this.getCurrentEmailAddress(),
allEmailAddresses: () => this.getAllEmailAddresses(),
emailData: () => this.getEmailData(),
emailId: () => this.getEmailId(),
threadData: (threadId) => this.getThread(threadId),
threadId: () => this.getSelectedConversationId(),
currentEmail: (thread, emailId) => {
// again both params to standardize AutoReply
return thread?.emails && thread.emails.length > 0
? thread.emails[thread.emails.length - 1]
: null;
},
emailsWithoutDrafts: (thread) =>
thread["emails"].filter((email) => !email.is_draft),
emailReplyButton: () => {
return $(this.selectors.responseButton).first();
},
backupEmailReplyButton: () => this.get.emailReplyButton(),
threadIdFromEmail: (emailId) => this.getSelectedConversationId(),
};
this.check = {
isInsideEmail: () => this.isInsideEmail(),
};
this.dom = {
composes: () => this.getComposeWindows(),
};
}
getComposeWindows() {
let composeElements = [];
// hardcoded need to change this is real compose window
$(".dMm6A").each((i, el) => {
composeElements.push(new Compose(el));
});
return composeElements;
}
getClassNameForEvent(eventName) {
const eventClassMap = {
viewEmail: ".SlLx9.byzS1",
viewThread: ".SlLx9.byzS1",
compose: ".dMm6A, .gXGox",
recipientChange: ".Lbs4W",
};
return eventClassMap[eventName];
}
// object that stores ConversationNodes which form an email thread
// still used if only singular email
getConversation(response) {
return response.Body.ResponseMessages.Items[0].Conversation;
}
getThreadId(conversation) {
return conversation.ConversationId.Id;
}
convertDateToUnix(date) {
const jsDate = new Date(date);
const unixTime = Math.floor(jsDate.getTime() / 1000);
return unixTime;
}
getEmailData(conversationNode) {
const email = conversationNode.Items[0];
const emptyContent = "<html><body></body></html>";
if (!email.UniqueBody && !email.NewBodyContent) return; // Ignore emails with no body
if (email.UniqueBody && email.UniqueBody.Value === emptyContent) return; // Ignore emails with empty body
const recipientMapper = (recipient) => ({
address: recipient.EmailAddress,
name: recipient.Name,
});
try {
const contentHTML = email.UniqueBody
? email.UniqueBody.Value
: email.NewBodyContent.Value;
const from = email.From.Mailbox
? {
address: email.From.Mailbox.EmailAddress,
name: email.From.Mailbox.Name,
}
: { address: this.getCurrentEmailAddress(), name: "" };
const to = email.ToRecipients?.map(recipientMapper) || [];
const cc = email.CcRecipients?.map(recipientMapper) || [];
const bcc = email.BccRecipients?.map(recipientMapper) || [];
const timestamp = email.DateTimeSent
? this.convertDateToUnix(email.DateTimeSent)
: 0;
const emailObject = {
smtpId: email.InternetMessageId || null,
subject: email.Subject,
contentHtml: contentHTML,
isDraft: email.IsDraft || false,
from: from,
to: to,
cc: cc,
bcc: bcc,
createdAt: email.DateTimeReceived || 0,
sentAt: email.DateTimeSent || 0,
threadId: email.ConversationId?.Id || this.getSelectedConversationId(),
operation: email.operation || null,
timestamp: timestamp,
};
return emailObject;
} catch (e) {
return;
}
}
getThreadDataWithFallback(existingThreadId) {
if (!existingThreadId) existingThreadId = "";
let threadData = this.get.threadData(existingThreadId);
// this can be null, which means we want to trigger a parse within the construction to make sure the DOM is fully loaded
return threadData;
}
addFetchObserver(fetchObserver, handler) {
const requestObservers = {
sendMessage: ["createitem", "updateitem"],
};
const responseObservers = {
cacheEmailThreads: ["getconversationitems"],
};
const observer = {
urlPatterns:
requestObservers[fetchObserver] || responseObservers[fetchObserver],
handler,
};
if (requestObservers[fetchObserver]) {
this.fetchObservers.requestObservers.push(observer);
} else if (responseObservers[fetchObserver]) {
this.fetchObservers.responseObservers.push(observer);
} else {
console.error(`Invalid fetchObserver: ${fetchObserver}`);
}
}
handleObserverResponse(url, clonedResponse) {
clonedResponse
.json()
.then((json) => {
this.fetchObservers.responseObservers.forEach((observer) => {
observer.urlPatterns.forEach((urlPattern) => {
if (url.includes(urlPattern)) {
observer.handler(json);
}
});
});
})
.catch((e) => {});
}
handleObserverRequest(url, args) {
this.fetchObservers.requestObservers.forEach((observer) => {
observer.urlPatterns.forEach((urlPattern) => {
if (url.includes(urlPattern)) {
observer.handler(args);
}
});
});
}
initFetchWatcher() {
if (this.fetchInitialized) {
return;
}
this.fetchInitialized = true;
const win = top;
const originalFetch = win.fetch;
win.fetch = new Proxy(originalFetch, {
apply: async (target, thisArg, args) => {
const url = args[0].toLowerCase();
try {
this.handleObserverRequest(url, args);
} catch (error) {
// console.error(error);
}
if (url.includes("getconversationitems")) {
if (!this.conversationRequestOptions) {
this.conversationRequestOptions = args[1];
let newBody = JSON.parse(this.conversationRequestOptions.body);
newBody.Body.SyncState = ""; // clear sync state to get all emails
this.conversationRequestOptions.body = JSON.stringify(newBody);
args[1] = this.conversationRequestOptions;
}
}
const response = await Reflect.apply(target, thisArg, args);
try {
this.handleObserverResponse(url, response.clone());
} catch (error) {
// console.error(error)
}
return response;
},
});
}
async sendConversationRequest(conversationId) {
let newBody = JSON.parse(this.conversationRequestOptions.body);
newBody.Body.Conversations[0].ConversationId.Id = conversationId;
let newOptions = this.conversationRequestOptions;
newOptions.body = JSON.stringify(newBody);
let res = await window.fetch(
"/owa/0/service.svc?action=GetConversationItems&app=Mail&n=1",
newOptions
);
let json = await res.json();
return json;
}
cacheEmailThreads = (response) => {
const conversation = this.getConversation(response);
const conversationId = conversation.ConversationId.Id;
const rawMap = this.cache.rawConversationMap;
const processedMap = this.cache.processedConversationMap;
if (conversation.ConversationNodes && !rawMap.has(conversationId)) {
rawMap.set(conversationId, conversation);
const emailDataList = conversation.ConversationNodes.map((node) => {
return this.getEmailData(node);
}).filter((emailData) => emailData !== undefined);
if (emailDataList.length > 0) {
if (processedMap.has(conversationId)) {
const existingList = processedMap.get(conversationId);
if (emailDataList.length > existingList.length) {
processedMap.set(conversationId, {
emails: emailDataList,
thread_id: conversationId,
}); // naming bc of gmailjs
}
} else {
processedMap.set(conversationId, {
emails: emailDataList,
thread_id: conversationId,
}); // naming bc of gmailjs
}
}
}
};
getAllEmails = () => {
return this.cache.processedConversationMap;
};
getThread(threadId) {
if (!threadId) threadId = this.get.threadId();
const conversation = this.cache.processedConversationMap.get(threadId);
if (conversation) {
return conversation;
}
return null;
}
createObserver() {
return new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "childList") {
const addedNodes = Array.from(mutation.addedNodes);
for (const node of addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
this.callbacks.forEach((callbacks, className) => {
const foundNode = $(node)
.find(className)
.add($(node).filter(className));
if (foundNode.length > 0) {
// only thing currently tracked by library is compose windows
callbacks.forEach((callback) =>
callback(new Compose(foundNode))
);
}
});
}
}
}
}
});
}
on(requestParamList, callback) {
this.addFetchObserver(requestParamList, callback);
}
off(requestParamList, callback) {
this.fetchObservers = this.fetchObservers.filter((observer) => {
return observer.urlPatterns !== requestParamList;
});
}
on_dom(className, callback) {
// Set up the observer to observe the entire document
if (this.callbacks.size === 0) {
this.observer.observe(document.body, {
childList: true,
subtree: true,
});
}
// Register the callback for the specified className
if (!this.callbacks.has(className)) {
this.callbacks.set(className, []);
}
this.callbacks.get(className).push(callback);
// this.checkExistingNodes(className)
}
checkExistingNodes(className) {
const existingNodes = document.querySelectorAll(className);
if (existingNodes.length > 0) {
const jQueryNodes = $(existingNodes);
this.callbacks
.get(className)
.forEach((callback) => callback(jQueryNodes));
}
}
off_dom(className, callback) {
const registeredCallbacks = this.callbacks.get(className);
if (registeredCallbacks) {
const index = registeredCallbacks.indexOf(callback);
if (index !== -1) {
registeredCallbacks.splice(index, 1);
}
// If there are no callbacks left for the specified className, disconnect the observer
if (registeredCallbacks.length === 0) {
this.callbacks.delete(className);
if (this.callbacks.size === 0) {
this.observer.disconnect();
}
}
}
}
// retrieves current session ids and times
getUserSessions() {
const allSessionsObject = JSON.parse(
window.localStorage.sessionTracking_SignedInAccountList
);
return allSessionsObject;
}
getCurrentEmailAddress() {
const sessionIds = this.getUserSessions(window);
// find the largest lastActiveTime value in a list of objects and return the object with the largest value
const mostRecentSession = sessionIds.reduce((a, b) =>
a.lastActiveTime > b.lastActiveTime ? a : b
);
const currentSession = JSON.parse(
window.localStorage[mostRecentSession.sessionTrackingKey]
);
const userEmail = currentSession.upn;
return userEmail;
}
getAllEmailAddresses() {
const sessionIds = this.getUserSessions(window);
const emails = sessionIds.map((session) => {
const currentSession = JSON.parse(
window.localStorage[session.sessionTrackingKey]
);
const userEmail = currentSession.upn;
return userEmail;
});
return emails;
}
// checks if the current page is an email
isInsideEmail() {
// check if the current window contains /id/ in the url
return window.location.href.includes("/id/"); // hardcoded need to change
}
// retrieves the conversation id from the current page
getSelectedConversationId() {
const url = window.location.href;
try {
let conversationId = decodeURIComponent(url.split("/id/")[1]);
return conversationId;
} catch (e) {
// no id return empty string
return "";
}
}
selectors = {
bodyDiv: ".dFCbN.dPKNh.DziEn", // first child of this class
recipientInput: ".vBoqL",
lastGu: ".f1MMn",
leftBar: ".___1cp4gt1",
emailBody: ".aVla3",
emailThread: ".Q8TCC.yyYQP.customScrollBar",
autoReply: ".cIAN3.DNevi",
autoReplyContainer: ".LIQKd.G_Fzw",
responseButtonContainer: ".th6py",
subject: ".f1MMn",
lowestComposeInput: ".yz4r1", // lowestComposeInput differs based on whether it's a compose (subject) or reply (recipientInput)
replyParent: "#editorParent_1",
sendButton: ".b56tW",
responseButton: ".xgQkx.FGlQz",
composeButton: "[aria-label='New mail']",
composeWindow:
".soZTT.UoDR_.customScrollBar.Qmg2Q, .Q8TCC.yyYQP.customScrollBar", // first is for compose, second is for reply .Q8TCC.yyYQP.customScrollBar
replyThread: ".aVla3.gXGox",
messageBody: ".dFCbN.k1Ttj.dPKNh.DziEn, [id^=editorParent_]",
subject: `input[aria-label="Add a subject"]`,
threadSubject: ".full.UAxMv",
threadMessage: ".aVla3",
threadSender: ".OZZZK",
recipientContainer: ".ow896",
recipient: "div.IIvzX",
threadBody: ".XbIp4.jmmB7.GNqVo.yxtKT.allowTextSelection",
minimizedThreadBody: "._nzWz",
threadMessageDate: ".AL_OM.l8Tnu.I1wdR",
emailThreadParent: ".Mq3cC",
composeTabsParent: ".nuum5",
selectedComposeTab: ".WkjaK.KaP9I.GlX5B",
composeTab: ".WkjaK.GlX5B",
dynamicConversationBody: ".L72vd", // updates with new convo: used for detecting root removal
timeIndicator: ".AL_OM.l8Tnu.I1wdR",
emailHeader: ".lDdSm.l8Tnu",
iconButton: ".ms-OverflowSet-item",
};
parseThreadFromDom() {
const emailObjectList = [];
const subjectNode = document.querySelector(this.selectors.threadSubject);
const emailMessages = document.querySelectorAll(
this.selectors.threadMessage
);
if (emailMessages.length === 0) return null;
let subject = "";
if (subjectNode) subject = subjectNode.innerText;
for (let emailNode of emailMessages) {
const emailObject = {
subject: "",
from: { name: "", address: "" },
to: [],
cc: [],
bcc: [],
contentHtml: "",
id:
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15),
timestamp: 0,
sentAt: 0,
};
if (subject) {
emailObject.subject = subject;
}
const sender = emailNode.querySelector(this.selectors.threadSender);
if (sender) {
// use the handle name address string function provided by the parent class
emailObject.from = this.handleNameAddressString(sender.innerText);
}
const recipientBoxes = emailNode.querySelectorAll(
this.selectors.recipientContainer
);
for (let box of recipientBoxes) {
const boxType = box.querySelector("label").innerText;
if (boxType === "To:") {
const recipients = box.querySelectorAll(this.selectors.recipient);
if (recipients.length > 0) {
const recipientList = [];
for (let name of recipients) {
if (name.innerText === "You") {
continue;
}
// remove all punctuation at the ends from name.innerText both ends of string and leading/trailing whitespace
const nameString = name.innerText
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "")
.trim();
recipientList.push({ name: nameString, address: "" });
}
emailObject.to = recipientList;
}
} else if (boxType === "Cc:") {
const recipients = box.querySelectorAll(this.selectors.recipient);
if (recipients.length > 0) {
const recipientList = [];
for (let name of recipients) {
if (name.innerText === "You") {
continue;
}
// remove all punctuation from name.innerText both ends of string and leading/trailing whitespace
const nameString = name.innerText
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "")
.trim();
recipientList.push({ name: nameString, address: "" });
}
emailObject.cc = recipientList;
}
}
}
const body = emailNode.querySelector(this.selectors.threadBody);
if (body) {
emailObject.contentHtml = body.innerHTML;
} else {
// try to parse minimized email (only has preview text)
const minimizedBody = emailNode.querySelector(
this.selectors.minimizedThreadBody
);
if (minimizedBody) {
emailObject.contentHtml = minimizedBody.innerHTML;
}
}
const date = emailNode.querySelector(this.selectors.threadMessageDate);
if (date) {
emailObject.timestamp = this.convertDateToUnix(date.innerText);
}
emailObjectList.push(emailObject);
}
const threadId = this.get.threadId();
const threadObject = {
emails: emailObjectList,
thread_id: threadId,
};
this.cache.processedConversationMap.set(threadId, threadObject);
return threadObject;
}
}
class OutlookElement {
constructor(element) {
this.$el = $(element);
}
}
class Compose extends OutlookElement {
constructor(element) {
super(element);
this.parent = $(element).parent();
}
id() {
// if it has an id, return it, else return 0
if (this.parent.attr("id")) {
let id = this.parent.attr("id");
return parseInt(id.split("_")[2]);
} else return 0;
}
type() {
if (this.$el.find(".f1MMn").length > 0) {
return "compose";
} else {
return "reply";
}
}
find(selector) {
return this.$el.find(selector);
}
}