# Integration Plan for Enhanced Thread Extractor ## Overview This document outlines how to integrate the enhanced thread extraction system with the existing Gmail Auto-Draft extension. ## Key Improvements in Enhanced System ### 1. **Batch Extraction Enhancements** - Dedicated `extractBatchThreadIds()` method with multiple fallback strategies - Detailed logging of extraction methods and success rates - Option to return full thread info (subject, sender) for better debugging - Click-based extraction as last resort (optional) ### 2. **One-off Draft Improvements** - More reliable thread ID extraction with 7+ fallback methods - Better handling of different Gmail views (conversation, search, label) - Automatic token refresh and retry logic - Enhanced message ID extraction when thread ID fails ### 3. **Performance & Reliability** - Intelligent caching with automatic invalidation - Session storage for thread history tracking - Continuous monitoring with event dispatching - Graceful degradation when methods fail ## Integration Steps ### Step 1: Add Enhanced Extractor to Extension 1. **Add to manifest.json** (web_accessible_resources): ```json { "resources": ["email_parser.js", "enhanced_thread_extractor.js"], "matches": ["https://mail.google.com/*"] } ``` 2. **Load in content.js** (at the top): ```javascript // Load enhanced thread extractor const script = document.createElement('script'); script.src = chrome.runtime.getURL('enhanced_thread_extractor.js'); script.onload = function() { console.log('Enhanced thread extractor loaded'); // The extractor auto-initializes and starts monitoring }; (document.head || document.documentElement).appendChild(script); ``` ### Step 2: Update Existing Functions Replace current `extractThreadId` and `getSelectedThreadIds` functions with the enhanced versions: ```javascript // In content.js - Remove old implementations and use enhanced ones // The enhanced extractor provides backward-compatible functions: // - extractThreadId(context) // - getSelectedThreadIds() ``` ### Step 3: Enhance Batch Draft Function Update `generateDraftsForThreadsHybrid` to use enhanced extraction: ```javascript async function generateDraftsForThreadsHybrid() { try { updateProgressUI('Starting', 'Preparing to generate drafts...'); const token = await getOAuthTokenFromBackground(); if (!token) throw new Error('Failed to get OAuth token'); // Use enhanced extraction with full info const threadInfo = await threadExtractor.extractThreadId('selected', { returnFullInfo: true, allowClickExtraction: false // Disable for batch to avoid UI disruption }); if (!threadInfo || !threadInfo.length) { updateProgressUI('Error', 'Please select at least one email thread.', true); return; } updateProgressUI('Processing', `Found ${threadInfo.length} selected threads`); // Process each thread with better error handling let successCount = 0; let errorCount = 0; for (let i = 0; i < threadInfo.length; i++) { const info = threadInfo[i]; try { updateProgressUI('Processing', `Generating draft ${i + 1}/${threadInfo.length}: "${info.subject}" from ${info.sender}...`); // Continue with existing logic using info.threadId // ...existing code... } catch (error) { console.error(`Failed to process thread ${info.threadId}:`, error); errorCount++; } } // ... rest of function } catch (error) { console.error('Batch draft generation failed:', error); updateProgressUI('Error', error.message, true); } } ``` ### Step 4: Enhance One-off Draft Function Update `oneOffAutoDraftForCurrentThread` with retry logic: ```javascript async function oneOffAutoDraftForCurrentThread(contextPrompt = null) { try { updateProgressUI('Initializing', 'Getting authentication token...'); const token = await getOAuthTokenFromBackground(); if (!token) throw new Error('Failed to get OAuth token'); updateProgressUI('Analyzing', 'Extracting thread ID...'); // Use enhanced extraction with retries let threadId = null; let attempts = 0; const maxAttempts = 3; while (!threadId && attempts < maxAttempts) { attempts++; try { threadId = await threadExtractor.extractThreadId('current', { forceRefresh: attempts > 1, // Force refresh on retry token: token // Provide token for API fallback }); } catch (error) { console.warn(`Attempt ${attempts} failed:`, error); if (attempts < maxAttempts) { updateProgressUI('Analyzing', `Retrying thread extraction (attempt ${attempts + 1})...`); await new Promise(r => setTimeout(r, 1000)); } } } if (!threadId) { updateProgressUI('Error', 'Could not determine current thread ID', true); throw new Error('Could not determine current thread ID. Make sure you are viewing an email thread.'); } console.log('Extracted thread ID:', threadId); // ... rest of existing function } catch (error) { console.error('Failed to create one-off autodraft:', error); updateProgressUI('Error', error.message, true); throw error; } } ``` ### Step 5: Add Event Listeners Listen for thread ID detection events: ```javascript // In content.js main() document.addEventListener('threadIdDetected', (event) => { console.log('Thread ID detected:', event.detail.threadId); // Update any UI elements that depend on thread ID const threadIdDisplay = document.querySelector('.thread-id-display'); if (threadIdDisplay) { threadIdDisplay.textContent = event.detail.threadId; } // Enable/disable buttons based on thread availability const draftButton = document.querySelector('.gpt-autodraft-button'); if (draftButton) { draftButton.disabled = false; } }); ``` ### Step 6: Add Fallback UI When thread extraction fails completely: ```javascript function showThreadSelectionFallback() { const fallbackUI = document.createElement('div'); fallbackUI.innerHTML = `
Please try one of the following: