-
Notifications
You must be signed in to change notification settings - Fork 1
/
message-observer.ts
42 lines (34 loc) · 1.3 KB
/
message-observer.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
import { type Browser } from 'webextension-polyfill';
import { type FoundTwoFactorAuthenticationCodePayload } from './common/payload';
declare const browser: Browser;
function initMessageObserver() {
console.log('Initializing MutationObserver to process incoming messages');
let lastFoundCode = '';
const observer = new MutationObserver(async () => {
const topMessageElement = document.querySelector(
'mws-conversations-list mws-conversation-list-item:first-of-type .text-content.unread',
);
const sender = topMessageElement?.querySelector('.name')?.textContent;
const fullMessage =
topMessageElement?.querySelector('.snippet-text')?.textContent;
const code = fullMessage?.match(/:\s+.*(\b\d{4,8}\b)/)?.[1];
if (!sender || !fullMessage || !code) {
return;
}
// Prevent repeats because `document` changes a lot.
if (code === lastFoundCode) {
return;
}
lastFoundCode = code;
const payload: FoundTwoFactorAuthenticationCodePayload = {
code,
sender,
fullMessage,
};
console.log('Found possible 2FA code. Payload:', payload);
browser.runtime.sendMessage(payload);
});
observer.observe(document, { childList: true, subtree: true });
}
console.log('Copy 2FA from Android Messages starting');
initMessageObserver();