|
| 1 | +import browser from 'webextension-polyfill' |
| 2 | + |
| 3 | + |
| 4 | +function getComposeMetadata(form: HTMLElement) { |
| 5 | + const getRecipients = (name: string) => |
| 6 | + Array.from( |
| 7 | + form.querySelectorAll(`div[name="${name}"] [data-hovercard-id]`), |
| 8 | + ).map((el) => el.getAttribute('data-hovercard-id')) |
| 9 | + .filter(Boolean) as string[] |
| 10 | + |
| 11 | + return { |
| 12 | + subject: (form.querySelector('input[name="subjectbox"]') as HTMLInputElement)?.value || '', |
| 13 | + to: getRecipients('to'), |
| 14 | + cc: getRecipients('cc'), |
| 15 | + bcc: getRecipients('bcc'), |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +let lastData: string | null = null |
| 20 | +let lastSentTime = 0 |
| 21 | + |
| 22 | +function sendGmailHeartbeat() { |
| 23 | + if (document.visibilityState === 'hidden') { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + const hash = window.location.hash |
| 28 | + const form = document.querySelector('div[role="dialog"] form') as HTMLElement | null |
| 29 | + |
| 30 | + let activity = 'reading_inbox' |
| 31 | + let meta: any = {} |
| 32 | + |
| 33 | + if (form) { |
| 34 | + // for simplity in MVP: |
| 35 | + // - if many emails forms are open, we only track the first one |
| 36 | + // - if subject, to, cc, bcc changes when composing the email, we send a new heartbeat |
| 37 | + activity = 'composing_email' |
| 38 | + meta = getComposeMetadata(form) |
| 39 | + } else if ( |
| 40 | + hash.includes('inbox/') || |
| 41 | + hash.includes('sent/') || |
| 42 | + hash.includes('all/') |
| 43 | + ) { |
| 44 | + /** |
| 45 | + * NOTE on Fragility: The selectors below (span.gD, .gE, h2.hP) are internal |
| 46 | + * Gmail class names. These are not part of a stable API and may change |
| 47 | + * during Gmail frontend updates. High-fidelity tracking may require |
| 48 | + * maintenance if these selectors break. |
| 49 | + */ |
| 50 | + const fromEl = document.querySelector('span.gD') |
| 51 | + const from = |
| 52 | + fromEl?.getAttribute('email') || |
| 53 | + fromEl?.getAttribute('data-hovercard-id') || |
| 54 | + (fromEl as HTMLElement)?.innerText || |
| 55 | + '' |
| 56 | + const to = Array.from( |
| 57 | + document.querySelectorAll('.gE [email], .gE [data-hovercard-id]'), |
| 58 | + ) |
| 59 | + .map( |
| 60 | + (el) => el.getAttribute('email') || el.getAttribute('data-hovercard-id'), |
| 61 | + ) |
| 62 | + .filter((e) => e && e !== from) as string[] |
| 63 | + |
| 64 | + activity = 'reading_email' |
| 65 | + meta = { |
| 66 | + subject: (document.querySelector('h2.hP') as HTMLElement)?.innerText || '', |
| 67 | + from, |
| 68 | + to, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const data = { |
| 73 | + gmail_activity: activity, |
| 74 | + ...meta, |
| 75 | + url: window.location.href, |
| 76 | + title: document.title, |
| 77 | + } |
| 78 | + |
| 79 | + const dataString = JSON.stringify(data) |
| 80 | + const now = Date.now() |
| 81 | + if (lastData === dataString && now - lastSentTime < 30000) { |
| 82 | + return |
| 83 | + } |
| 84 | + lastData = dataString |
| 85 | + lastSentTime = now |
| 86 | + |
| 87 | + browser.runtime.sendMessage({ type: 'AW_GMAIL_HEARTBEAT', data }).catch(() => {}) |
| 88 | +} |
| 89 | + |
| 90 | +let intervalId: ReturnType<typeof setInterval> | null = null |
| 91 | + |
| 92 | +function startTracking() { |
| 93 | + if (intervalId !== null) { |
| 94 | + return |
| 95 | + } |
| 96 | + sendGmailHeartbeat() |
| 97 | + sendGmailHeartbeat() |
| 98 | + intervalId = setInterval(sendGmailHeartbeat, 1000) |
| 99 | +} |
| 100 | + |
| 101 | +function stopTracking() { |
| 102 | + if (intervalId === null) { |
| 103 | + return |
| 104 | + } |
| 105 | + clearInterval(intervalId) |
| 106 | + intervalId = null |
| 107 | +} |
| 108 | + |
| 109 | +browser.storage.local.get('gmailEnabled').then((settings) => { |
| 110 | + if (settings.gmailEnabled) { |
| 111 | + startTracking() |
| 112 | + } |
| 113 | +}) |
| 114 | + |
| 115 | +browser.storage.onChanged.addListener((changes) => { |
| 116 | + if ('gmailEnabled' in changes) { |
| 117 | + if (changes.gmailEnabled.newValue) { |
| 118 | + startTracking() |
| 119 | + } else { |
| 120 | + stopTracking() |
| 121 | + } |
| 122 | + } |
| 123 | +}) |
0 commit comments