(trackingLog, popupWindowId, actionOnlyMode = false, initialState = null)
| 11 | * @param {Object} initialState - Initial webpage state (window, scroll, viewport) |
| 12 | */ |
| 13 | export async function sendTrackToBackend(trackingLog, popupWindowId, actionOnlyMode = false, initialState = null) { |
| 14 | // Additional failsafe check for actionOnlyMode |
| 15 | if (actionOnlyMode) { |
| 16 | console.log('FAILSAFE: actionOnlyMode is active in sendTrackToBackend - aborting API call'); |
| 17 | return { success: false, error: 'Action only mode is active' }; |
| 18 | } |
| 19 | |
| 20 | // Check authentication first |
| 21 | const authStatus = await checkAuth(); |
| 22 | |
| 23 | if (!authStatus.isAuthenticated) { |
| 24 | // This shouldn't happen as auth is checked before recording starts |
| 25 | console.error('Not authenticated when trying to save track'); |
| 26 | |
| 27 | // Close processing popup |
| 28 | if (popupWindowId) { |
| 29 | chrome.windows.remove(popupWindowId).catch(() => {}); |
| 30 | } |
| 31 | |
| 32 | // Show error notification |
| 33 | chrome.notifications.create({ |
| 34 | type: 'basic', |
| 35 | iconUrl: chrome.runtime.getURL('icons/icon-48.png'), |
| 36 | title: 'Authentication Error', |
| 37 | message: 'Please log in and try recording again.', |
| 38 | priority: 2 |
| 39 | }); |
| 40 | |
| 41 | return { success: false, error: 'Not authenticated' }; |
| 42 | } |
| 43 | |
| 44 | console.log('Sending trackingLog to /track/save:', trackingLog); |
| 45 | console.log('Number of actions:', trackingLog.length); |
| 46 | try { |
| 47 | // Get current tab info |
| 48 | const [activeTab] = await chrome.tabs.query({active: true, currentWindow: true}); |
| 49 | |
| 50 | // Get auth token |
| 51 | const token = await getAuthToken(); |
| 52 | |
| 53 | // Capture session data for the recorded site |
| 54 | const sessionData = await captureSessionData(); |
| 55 | const authMethods = detectAuthMethod(sessionData); |
| 56 | |
| 57 | // Capture comprehensive auth data from all domains visited during recording |
| 58 | const comprehensiveAuthData = await captureComprehensiveAuthData(trackingLog); |
| 59 | |
| 60 | |
| 61 | // Prepare request body matching server expectations |
| 62 | const requestBody = { |
| 63 | url: activeTab?.url || '', |
| 64 | title: activeTab?.title || '', |
| 65 | actions: trackingLog, |
| 66 | // Optional metadata can be included if server supports it |
| 67 | metadata: { |
| 68 | timestamp: Date.now(), |
| 69 | extensionVersion: chrome.runtime.getManifest().version, |
| 70 | actionsCount: trackingLog.length |
no test coverage detected