-
-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathconversation-list.ts
306 lines (241 loc) · 8.77 KB
/
conversation-list.ts
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
import {ipcRenderer as ipc} from 'electron-better-ipc';
import elementReady from 'element-ready';
import {isNull} from 'lodash';
import selectors from './selectors';
const icon = {
read: 'data-caprine-icon',
unread: 'data-caprine-icon-unread',
};
const padding = {
top: 3,
right: 0,
bottom: 3,
left: 0,
};
function drawIcon(size: number, img?: HTMLImageElement): HTMLCanvasElement {
const canvas = document.createElement('canvas');
if (img) {
canvas.width = size + padding.left + padding.right;
canvas.height = size + padding.top + padding.bottom;
const ctx = canvas.getContext('2d')!;
ctx.beginPath();
ctx.arc((size / 2) + padding.left, (size / 2) + padding.top, (size / 2), 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();
ctx.drawImage(img, padding.left, padding.top, size, size);
} else {
canvas.width = 0;
canvas.height = 0;
}
return canvas;
}
// Return canvas with rounded image
async function urlToCanvas(url: string, size: number): Promise<HTMLCanvasElement> {
return new Promise(resolve => {
const img = new Image();
img.setAttribute('crossorigin', 'anonymous');
img.addEventListener('load', () => {
resolve(drawIcon(size, img));
});
img.addEventListener('error', () => {
console.error('Image not found', url);
resolve(drawIcon(size));
});
img.src = url;
});
}
async function createIcons(element: HTMLElement, url: string): Promise<void> {
const canvas = await urlToCanvas(url, 50);
element.setAttribute(icon.read, canvas.toDataURL());
const markerSize = 8;
const ctx = canvas.getContext('2d')!;
ctx.fillStyle = '#f42020';
ctx.beginPath();
ctx.ellipse(canvas.width - markerSize, markerSize, markerSize, markerSize, 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
element.setAttribute(icon.unread, canvas.toDataURL());
}
async function discoverIcons(element: HTMLElement): Promise<void> {
if (element) {
return createIcons(element, element.getAttribute('src')!);
}
console.warn('Could not discover profile picture. Falling back to default image.');
// Fall back to messenger favicon
const messengerIcon = document.querySelector('link[rel~="icon"]');
if (messengerIcon) {
return createIcons(element, messengerIcon.getAttribute('href')!);
}
// Fall back to facebook favicon
return createIcons(element, 'https://facebook.com/favicon.ico');
}
async function getIcon(element: HTMLElement, unread: boolean): Promise<string> {
if (element === null) {
return icon.read;
}
if (!element.getAttribute(icon.read)) {
await discoverIcons(element);
}
return element.getAttribute(unread ? icon.unread : icon.read)!;
}
async function getLabel(element: HTMLElement): Promise<string> {
if (isNull(element)) {
return '';
}
const emojis: HTMLElement[] = [];
if (element !== null) {
for (const element_curr of element.children) {
emojis.push(element_curr as HTMLElement);
}
}
for (const emoji of emojis) {
emoji.outerHTML = emoji.querySelector('img')?.getAttribute('alt') ?? '';
}
return element.textContent ?? '';
}
async function createConversationNewDesign(element: HTMLElement): Promise<Conversation> {
const conversation: Partial<Conversation> = {};
// TODO: Exclude muted conversations
/*
const muted = Boolean(element.querySelector(selectors.muteIconNewDesign));
*/
conversation.selected = Boolean(element.querySelector('[role=row] [role=link] > div:only-child'));
conversation.unread = Boolean(element.querySelector('[aria-label="Mark as Read"]'));
const unparsedLabel = element.querySelector<HTMLElement>('.a8c37x1j.ni8dbmo4.stjgntxs.l9j0dhe7 > span > span')!;
conversation.label = await getLabel(unparsedLabel);
const iconElement = element.querySelector<HTMLElement>('img')!;
conversation.icon = await getIcon(iconElement, conversation.unread);
return conversation as Conversation;
}
async function createConversationList(): Promise<Conversation[]> {
const conversationListSelector = selectors.conversationList;
const list = await elementReady(conversationListSelector, {
stopOnDomReady: false,
});
if (!list) {
console.error('Could not find conversation list', conversationListSelector);
return [];
}
const elements: HTMLElement[] = [...list.children] as HTMLElement[];
// Remove last element from childer list
elements.splice(-1, 1);
const conversations: Conversation[] = await Promise.all(elements.map(async element => createConversationNewDesign(element)));
return conversations;
}
export async function sendConversationList(): Promise<void> {
const conversationsToRender: Conversation[] = await createConversationList();
ipc.callMain('conversations', conversationsToRender);
}
function genStringFromNode(element: Element): string | undefined {
const cloneElement = element.cloneNode(true) as Element;
let emojiString;
const images = cloneElement.querySelectorAll('img');
for (const image of images) {
emojiString = image.alt;
// Replace facebook's thumbs up with emoji
if (emojiString === '(Y)' || emojiString === '(y)') {
emojiString = '👍';
}
image.parentElement?.replaceWith(document.createTextNode(emojiString));
}
return cloneElement.textContent ?? undefined;
}
function countUnread(mutationsList: MutationRecord[]): void {
const alreadyChecked: string[] = [];
const unreadMutations = mutationsList.filter(mutation =>
// When a conversations "becomes unread".
(
mutation.type === 'childList'
&& mutation.addedNodes.length > 0
&& ((mutation.addedNodes[0] as Element).className === selectors.conversationSidebarUnreadDot)
)
// When text is received
|| (
mutation.type === 'characterData'
// Make sure the text corresponds to a conversation
&& mutation.target.parentElement?.parentElement?.parentElement?.className === selectors.conversationSidebarTextParent
)
// When an emoji is received, node(s) are added
|| (
mutation.type === 'childList'
// Make sure the mutation corresponds to a conversation
&& mutation.target.parentElement?.parentElement?.className === selectors.conversationSidebarTextParent
)
// Emoji change
|| (
mutation.type === 'attributes'
&& mutation.target.parentElement?.parentElement?.parentElement?.parentElement?.className === selectors.conversationSidebarTextParent
));
// Check latest mutation first
for (const mutation of unreadMutations.reverse()) {
const curr = (mutation.target.parentElement as Element).closest(selectors.conversationSidebarSelector)!;
const href = curr.closest('[role="link"]')?.getAttribute('href');
if (!href) {
continue;
}
// It is possible to have multiple mutations for the same conversation, but we only want one notification.
// So if the current conversation has already been checked, continue.
// Additionally if the conversation is not unread, then also continue.
if (alreadyChecked.includes(href) || !curr.querySelector('.' + selectors.conversationSidebarUnreadDot.replace(/ /g, '.'))) {
continue;
}
alreadyChecked.push(href);
// Get the image data URI from the parent of the author/text
const imgUrl = curr.querySelector('img')?.dataset.caprineIcon;
const textOptions = curr.querySelectorAll(selectors.conversationSidebarTextSelector);
// Get the author and text of the new message
const titleTextNode = textOptions[0];
const bodyTextNode = textOptions[1];
const titleText = genStringFromNode(titleTextNode);
const bodyText = genStringFromNode(bodyTextNode);
if (!bodyText || !titleText || !imgUrl) {
continue;
}
// Send a notification
ipc.callMain('notification', {
id: 0,
title: titleText,
body: bodyText,
icon: imgUrl,
silent: false,
});
}
}
async function updateTrayIcon(): Promise<void> {
const chatsIcon = await elementReady(selectors.chatsIcon, {
stopOnDomReady: false,
});
// Extract messageCount from ariaLabel
const messageCount = chatsIcon?.ariaLabel?.match(/\d+/g) ?? 0;
ipc.callMain('update-tray-icon', messageCount);
}
window.addEventListener('load', async () => {
const sidebar = await elementReady('[role=navigation]:has([role=grid])', {stopOnDomReady: false});
const leftSidebar = await elementReady(`${selectors.leftSidebar}:has(${selectors.chatsIcon})`, {stopOnDomReady: false});
if (sidebar) {
const conversationListObserver = new MutationObserver(async () => sendConversationList());
const conversationCountObserver = new MutationObserver(countUnread);
conversationListObserver.observe(sidebar, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: ['class'],
});
conversationCountObserver.observe(sidebar, {
characterData: true,
subtree: true,
childList: true,
attributes: true,
attributeFilter: ['src', 'alt'],
});
}
if (leftSidebar) {
const chatsIconObserver = new MutationObserver(async () => updateTrayIcon());
chatsIconObserver.observe(leftSidebar, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: ['aria-label'],
});
}
});