-
-
Notifications
You must be signed in to change notification settings - Fork 81
Implement Gmail activity tracking with optional metadata extraction #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
RaoufGhrissi
wants to merge
1
commit into
ActivityWatch:master
Choose a base branch
from
RaoufGhrissi:feat/gmail-tracking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/usr/bin/env bash | ||
| make build-chrome && \ | ||
| mkdir -p artifacts/chrome && \ | ||
| unzip -o artifacts/chrome.zip -d artifacts/chrome | ||
|
|
||
| make build-firefox && \ | ||
| mkdir -p artifacts/firefox && \ | ||
| unzip -o artifacts/firefox.zip -d artifacts/firefox | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import browser from 'webextension-polyfill' | ||
| import deepEqual from 'deep-equal' | ||
| import config from '../config' | ||
|
|
||
| let lastData: any | null = null | ||
|
|
||
| if (window.top === window.self) { | ||
|
|
||
| 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()) { | ||
| stopTracking() | ||
| 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, | ||
| } | ||
RaoufGhrissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (!deepEqual(lastData, meta)) { | ||
| lastData = meta; | ||
| if (!isExtensionValid()) return; | ||
| 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, 1000) | ||
| pulseIntervalId = setInterval(() => { | ||
| if (!isExtensionValid()) { | ||
| stopTracking() | ||
| 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 | ||
| } | ||
|
|
||
| if (isExtensionValid()) { | ||
| refreshTracking() | ||
|
|
||
| browser.storage.onChanged.addListener((changes) => { | ||
| if ('gmailEnabled' in changes || 'enabled' in changes) { | ||
| refreshTracking() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| } // if (window.top === window.self) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.