-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathgmail.ts
More file actions
158 lines (138 loc) · 4.22 KB
/
Copy pathgmail.ts
File metadata and controls
158 lines (138 loc) · 4.22 KB
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
import browser from 'webextension-polyfill'
import deepEqual from 'deep-equal'
import config from '../config'
let lastData: any | null = null;
function isExtensionValid() {
return typeof browser !== 'undefined' && !!browser.storage && !!browser.runtime?.id;
}
function getComposeMetadata(form: HTMLElement) {
const getRecipients = (name: string) =>
Array.from(
form.querySelectorAll(`div[name="${name}"] [data-hovercard-id]`),
).map((el) => el.getAttribute('data-hovercard-id'))
.filter(Boolean) as string[];
return {
gmail_activity: 'composing_email',
subject: (form.querySelector('input[name="subjectbox"]') as HTMLInputElement)?.value || '',
to: getRecipients('to'),
cc: getRecipients('cc'),
bcc: getRecipients('bcc'),
};
}
function sendGmailHeartbeat() {
if (!isExtensionValid()) {
// Don't kill tracking — just skip this tick.
// The onChanged listener will handle re-evaluation.
return;
}
if (document.visibilityState === 'hidden') {
return;
}
const hash = window.location.hash;
// for simplity in MVP:
// - if many emails forms are open, we only track the first one
const form = document.querySelector('div[role="dialog"] form') as HTMLElement | null;
let activity = 'reading_inbox';
let meta: any = { gmail_activity: activity };
if (form) {
activity = 'composing_email';
meta = getComposeMetadata(form);
} else if (
hash.includes('inbox/') ||
hash.includes('sent/') ||
hash.includes('all/')
) {
/**
* NOTE on Fragility: The selectors below (span.gD, .gE, h2.hP) are internal
* Gmail class names. These are not part of a stable API and may change
* during Gmail frontend updates. High-fidelity tracking may require
* maintenance if these selectors break.
*/
const fromEl = document.querySelector('span.gD');
const from =
fromEl?.getAttribute('email') ||
fromEl?.getAttribute('data-hovercard-id') ||
(fromEl as HTMLElement)?.innerText ||
'';
const to = Array.from(
document.querySelectorAll('.gE [email], .gE [data-hovercard-id]'),
)
.map(
(el) => el.getAttribute('email') || el.getAttribute('data-hovercard-id'),
)
.filter((e) => e && e !== from) as string[];
activity = 'reading_email';
meta = {
gmail_activity: activity,
subject: (document.querySelector('h2.hP') as HTMLElement)?.innerText || '',
from,
to,
};
}
if (!deepEqual(lastData, meta)) {
lastData = meta;
browser.runtime.sendMessage({
type: 'AW_GMAIL_HEARTBEAT',
data: meta
}).catch(() => {})
}
}
let detectIntervalId: ReturnType<typeof setInterval> | null = null;
let pulseIntervalId: ReturnType<typeof setInterval> | null = null;
function startTracking() {
if (detectIntervalId !== null) {
return;
}
detectIntervalId = setInterval(sendGmailHeartbeat, 5000);
pulseIntervalId = setInterval(() => {
if (!isExtensionValid()) {
return;
}
if (lastData && document.visibilityState === 'visible') {
try {
browser.runtime.sendMessage({
type: 'AW_GMAIL_HEARTBEAT',
data: lastData
}).catch(() => {})
} catch (err) {
// Extension context invalidated
}
}
}, config.heartbeat.intervalInSeconds * 1000);
sendGmailHeartbeat();
}
async function refreshTracking() {
if (!isExtensionValid()) {
return;
}
try {
const settings = await browser.storage.local.get(['gmailEnabled', 'enabled']);
const shouldTrack = Boolean(settings.gmailEnabled && settings.enabled);
if (shouldTrack) {
startTracking();
} else {
stopTracking();
}
} catch (err) {
console.error('[Gmail Content] Failed to refresh tracking state', err);
}
}
function stopTracking() {
if (detectIntervalId !== null) {
clearInterval(detectIntervalId)
detectIntervalId = null;
}
if (pulseIntervalId !== null) {
clearInterval(pulseIntervalId)
pulseIntervalId = null;
}
lastData = null;
}
browser.storage.local.get(['gmailEnabled', 'enabled']).then(() => {
refreshTracking();
})
browser.storage.onChanged.addListener((changes) => {
if ('gmailEnabled' in changes || 'enabled' in changes) {
refreshTracking();
}
})