Linux + iPhone Continuity / iMessage / SMS
further refinements
Zack Bartel79dca7d1afef
diff --git a/extension/src/content/autofill.js b/extension/src/content/autofill.jsindex 29c40dd..d1daf22 100644--- a/extension/src/content/autofill.js+++ b/extension/src/content/autofill.js@@ -1,20 +1,53 @@// Content script for browsers (Chrome/Firefox)// This runs in the context of webpages and looks for OTP input fields-function findOtpFields() {- const inputs = document.querySelectorAll('input[type="text"], input[type="number"], input[autocomplete="one-time-code"]');- const otpFields = [];-- for (const input of inputs) {- const name = (input.name || '').toLowerCase();- const id = (input.id || '').toLowerCase();-- if (name.includes('otp') || id.includes('otp') || name.includes('code') || input.autocomplete === 'one-time-code') {- otpFields.push(input);- }+function findOtpInputs(doc) {+ const inputs = [...doc.querySelectorAll('input')];++ return inputs.filter(input => {+ // Tier 1: explicit standard attribute+ if (input.autocomplete === 'one-time-code') return true;++ // Tier 2: type/inputmode signals+ if (input.inputMode === 'numeric') return scoreInput(input) > 5;+ if (input.type === 'tel' || input.type === 'number') return scoreInput(input) > 5;++ // Tier 3: attribute keyword matching+ const attrs = [input.id, input.name, input.placeholder, input.className]+ .join(' ').toLowerCase();+ if (/otp|passcode|verif|code|token|pin|2fa/.test(attrs)) return true;++ // Tier 4: maxlength in OTP range (4–8)+ const ml = parseInt(input.maxLength);+ if (ml >= 4 && ml <= 8 && input.type !== 'password') return scoreInput(input) > 3;++ return false;+ });+}++function scoreInput(input) {+ let score = 0;+ // Look at nearby label text+ const label = input.labels?.[0]?.textContent+ || input.closest('form')?.querySelector('label')?.textContent || '';+ if (/code|otp|verify|token|pin/i.test(label)) score += 10;++ // Surrounding text in parent+ const parentText = input.parentElement?.textContent?.toLowerCase() || '';+ if (/enter.*code|verification|one.time/i.test(parentText)) score += 5;++ return score;+}++function detectSplitOtp(doc) {+ const singles = [...doc.querySelectorAll('input[maxlength="1"]')]+ .filter(i => i.type === 'text' || i.type === 'tel' || i.type === 'number' || !i.type);++ // Check if they're siblings/adjacent — likely a split OTP+ if (singles.length >= 4 && singles.length <= 8) {+ return singles; // fill character by character}-- return otpFields;+ return null;}let otpInterval = null;@@ -26,17 +59,26 @@ function requestOtp() {});}+function hasOtpFields() {+ if (detectSplitOtp(document)) return true;+ return findOtpInputs(document).length > 0;+}+// Check every 2 seconds for up to 2 minutes-const fields = findOtpFields();-if (fields.length > 0) {+if (hasOtpFields()) {console.log("Found potential OTP fields on page, requesting OTP from Tether");requestOtp();let attempts = 0;otpInterval = setInterval(() => {attempts++;+ const splits = detectSplitOtp(document);+ const regular = findOtpInputs(document);+// Stop trying after 2 minutes or if the user manually filled the field- if (attempts > 60 || (fields[0] && fields[0].value.length > 4)) {+ if (attempts > 60 ||+ (splits && splits[0].value !== "") ||+ (regular.length > 0 && regular[0].value.length > 3)) {clearInterval(otpInterval);return;}@@ -47,18 +89,32 @@ if (fields.length > 0) {// Listen for OTPs sent from the daemonchrome.runtime.onMessage.addListener((request, sender, sendResponse) => {if (request.action === "fill_otp" && request.otp) {- const fields = findOtpFields();- if (fields.length > 0 && fields[0].value === "") {- console.log("Autofilling OTP:", request.otp);- // Basic autofill logic- fields[0].value = request.otp;+ const otp = request.otp.toString();+ const splitFields = detectSplitOtp(document);- // Dispatch events so React/Vue/Angular pick up the change- fields[0].dispatchEvent(new Event('input', { bubbles: true }));- fields[0].dispatchEvent(new Event('change', { bubbles: true }));+ if (splitFields) {+ console.log("Autofilling split OTP:", otp);+ for (let i = 0; i < Math.min(otp.length, splitFields.length); i++) {+ splitFields[i].value = otp[i];- // Clean up the interval+ // Dispatch events so React/Vue/Angular pick up the change+ splitFields[i].dispatchEvent(new Event('input', { bubbles: true }));+ splitFields[i].dispatchEvent(new Event('change', { bubbles: true }));+ }if (otpInterval) clearInterval(otpInterval);+ } else {+ const regularFields = findOtpInputs(document);+ if (regularFields.length > 0 && regularFields[0].value === "") {+ console.log("Autofilling OTP:", otp);+ regularFields[0].value = otp;++ // Dispatch events so React/Vue/Angular pick up the change+ regularFields[0].dispatchEvent(new Event('input', { bubbles: true }));+ regularFields[0].dispatchEvent(new Event('change', { bubbles: true }));++ // Clean up the interval+ if (otpInterval) clearInterval(otpInterval);+ }}}});diff --git a/extension/src/mail/extractor.js b/extension/src/mail/extractor.jsindex d10a840..c265fff 100644--- a/extension/src/mail/extractor.js+++ b/extension/src/mail/extractor.js@@ -61,20 +61,55 @@ function isFalsePositive(num, context) {if (typeof messenger !== 'undefined') {console.log("Tether Mail Extractor loaded in Thunderbird/Betterbird");- // Listen for new messages arriving in the background completely silently+ // Track message IDs we've already processed to avoid duplicates+ const seenMessageIds = new Set();++ async function processNewMessagesInFolder(folder) {+ try {+ const page = await messenger.messages.list(folder);+ for (const message of page.messages) {+ if (!seenMessageIds.has(message.id)) {+ seenMessageIds.add(message.id);+ processMessage(message);+ }+ }+ } catch (e) {+ // Folder may not support listing (e.g. virtual folders)+ }+ }++ // Tier 1: onNewMailReceived — works for POP3 / local delivery / filtersif (messenger.messages.onNewMailReceived) {messenger.messages.onNewMailReceived.addListener(async (folder, messages) => {for (const message of messages.messages) {- processMessage(message);+ if (!seenMessageIds.has(message.id)) {+ seenMessageIds.add(message.id);+ processMessage(message);+ }}});}- // Also process manually opened messages just in case it missed it+ // Tier 2: onFolderInfoChanged — catches IMAP synced mail that onNewMailReceived misses+ // Fires when folder unread/total counts change, i.e. new messages landed via IMAP+ if (messenger.folders?.onFolderInfoChanged) {+ messenger.folders.onFolderInfoChanged.addListener(async (folder, folderInfo) => {+ // Only scan inbox-type folders where new mail typically lands+ if (folder.type === 'inbox' || folder.type === 'other') {+ await processNewMessagesInFolder(folder);+ }+ });+ }++ // Tier 3: onMessageDisplayed — final fallback when opening an email manuallymessenger.messageDisplay.onMessageDisplayed.addListener(async (tabId, message) => {- processMessage(message);+ if (!seenMessageIds.has(message.id)) {+ seenMessageIds.add(message.id);+ processMessage(message);+ }});+async function processMessage(message) {const full = await messenger.messages.getFull(message.id);const bodyText = extractTextFromParts(full.parts);