File size: 16,130 Bytes
82fdec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c90140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82fdec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c90140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// background.js
const LOG_LEVEL = {
  DEBUG: 0,
  INFO: 1,
  WARN: 2,
  ERROR: 3
};
// Set the current log level for the extension.
// For example, to see DEBUG, INFO, WARN, ERROR messages, set to LOG_LEVEL.DEBUG.
// To see only INFO, WARN, ERROR, set to LOG_LEVEL.INFO, and so on.
const CURRENT_LOG_LEVEL = LOG_LEVEL.DEBUG; // Default to showing all logs for now

function log(level, ...args) {
  if (level < CURRENT_LOG_LEVEL) {
    return;
  }
  // Preserve the existing prefix style
  const prefix = '[Rebrowse BG]';
  switch (level) {
    case LOG_LEVEL.DEBUG:
      console.debug(prefix, ...args);
      break;
    case LOG_LEVEL.INFO:
      // console.info often styles the same as console.log, but is semantically correct.
      console.info(prefix, ...args);
      break;
    case LOG_LEVEL.WARN:
      console.warn(prefix, ...args);
      break;
    case LOG_LEVEL.ERROR:
      console.error(prefix, ...args);
      break;
    default:
      // Fallback for unknown levels, though ideally this shouldn't be hit.
      console.log(prefix, `[UnknownLevel:${level}]`, ...args);
  }
}

let NATIVE_PORT = null;
let portConnectInProgress = false;
const HOST_NAME = 'com.rebrowse.host';
let isNativeHostReady = false;
// Simple FIFO queue to buffer outbound messages when the native host is not ready.
// Each entry is a plain JS object that will be passed as-is to NATIVE_PORT.postMessage when flushed.
// FIX: The outbound queue in background.js had been disabled, if native post isn't ready yet.
const MESSAGE_QUEUE_MAX = 500;
let messageQueue = [];

// NEW: Variable to hold the last known recording status
let currentRecordingStatus = { status: 'idle', message: 'Awaiting host status.' };

function setupNativePortListeners(port) {
  port.onMessage.addListener(msg => {
    if (port !== NATIVE_PORT && NATIVE_PORT !== null) { 
        log(LOG_LEVEL.WARN, "Received message from an old/stale port. Ignoring type:", msg.type);
        return;
    }

    if (msg.type === 'inject') {
      if (msg.tabId && msg.payload) {
        chrome.tabs.sendMessage(msg.tabId, msg.payload).catch(e => log(LOG_LEVEL.WARN, "⚠️ Error sending inject message to tab:", e.message));
      }
    } else if (msg.type === 'status' && msg.message === 'Native host ready and listening for CDP.') {
      log(LOG_LEVEL.INFO, 'βœ“ Native host signaled ready. Current NATIVE_PORT matched. Sending ack.');
      isNativeHostReady = true; 
      portConnectInProgress = false;
      try {
          if (NATIVE_PORT === port) { 
            NATIVE_PORT.postMessage({ type: "client_ready_ack", message: "Extension acknowledged host readiness." });
            log(LOG_LEVEL.INFO, "β–Ί Sending initial PING to host after ACK.");
            NATIVE_PORT.postMessage({ type: "extension_ping", data: "keep_alive" });
            processMessageQueue();
          } else {
            log(LOG_LEVEL.WARN, "⚠️ Port changed before client_ready_ack/ping could be sent (in host ready handler).");
            isNativeHostReady = false; 
          }
      } catch (e) {
          log(LOG_LEVEL.ERROR, "❌ Error sending client_ready_ack or ping:", e.message);
          isNativeHostReady = false; 
          if (NATIVE_PORT === port) { try { port.disconnect(); } catch (ex){} NATIVE_PORT = null; portConnectInProgress = false; setTimeout(ensureNativeConnection, 500); }
      }
    } else if (msg.type === 'ack') {
      // Specific request to move cdp_event and ui_event ACKs to DEBUG
      if (msg.received_event_type === 'cdp_event' || msg.received_event_type === 'ui_event') {
        log(LOG_LEVEL.DEBUG, `βœ“ ACK: Host ➜ ${msg.received_event_type}: ${msg.details}`);
      } else {
        log(LOG_LEVEL.INFO, `βœ“ ACK: Host ➜ ${msg.received_event_type}: ${msg.details}`);
      }
    } else if (msg.type === 'recording_status_update') {
      log(LOG_LEVEL.INFO, `[Rebrowse BG] Received recording_status_update from host:`, msg.payload);
      // Store the latest status
      currentRecordingStatus = msg.payload;
      // Forward this to the popup
      chrome.runtime.sendMessage({ type: 'recording_status', data: msg.payload }).catch(e => {
        log(LOG_LEVEL.WARN, "[Rebrowse BG] Error sending recording_status to popup (popup might be closed):", e.message);
      });
    } else if (msg.type === 'replay-step') {
      log(LOG_LEVEL.INFO, `[Rebrowse BG] Received replay-step from host:`, msg);
      (async () => {
        try {
          const { action, selector, value } = msg;
          const [tab] = await chrome.tabs.query({active: true, currentWindow: true});
          if (tab) {
            chrome.tabs.sendMessage(tab.id, { action, selector, value });
          } else {
            log(LOG_LEVEL.WARN, 'Could not find active tab to send replay step.');
          }
        } catch (e) {
          log(LOG_LEVEL.ERROR, 'Error processing replay-step:', e.message);
        }
      })();
    }
  });

  port.onDisconnect.addListener(() => {
    log(LOG_LEVEL.ERROR, `πŸ”Œ Native port disconnected.`);
    if (chrome.runtime.lastError) {
      log(LOG_LEVEL.ERROR, 'Disconnect reason:', chrome.runtime.lastError.message);
    } else {
      log(LOG_LEVEL.WARN, '⚠️ Native port disconnected without a specific chrome.runtime.lastError.');
    }

    if (NATIVE_PORT === port || NATIVE_PORT === null) {
      NATIVE_PORT = null; 
      isNativeHostReady = false; 
      portConnectInProgress = false;
      // Also reset recording status on disconnect, as we don't know the state anymore.
      currentRecordingStatus = { status: 'error', message: 'Host disconnected.' };
      log(LOG_LEVEL.INFO, 'β–Ί Current native port nulled by onDisconnect. Attempting to reconnect in 1 second...');
      setTimeout(ensureNativeConnection, 1000);
    }
  });
}

function ensureNativeConnection() {
  if (NATIVE_PORT && isNativeHostReady) { 
    return true;
  }
  if (portConnectInProgress) {
    log(LOG_LEVEL.DEBUG, "β–Ί Port connection attempt already in progress. Not starting new one.");
    return false; 
  }

  log(LOG_LEVEL.INFO, `β–Ί Attempting to connect to native host '${HOST_NAME}'...`);
  isNativeHostReady = false; 
  portConnectInProgress = true;
  try {
    const newPort = chrome.runtime.connectNative(HOST_NAME); 
    NATIVE_PORT = newPort; 
    log(LOG_LEVEL.INFO, "βœ“ Native port object created. Setting up listeners.");
    setupNativePortListeners(newPort); 
    return true; 
  } catch (e) {
    log(LOG_LEVEL.ERROR, "❌ CRITICAL ERROR during chrome.runtime.connectNative:", e.message);
    if (NATIVE_PORT) { 
        try { NATIVE_PORT.disconnect(); } catch(ex){ log(LOG_LEVEL.WARN, "⚠️ Error disconnecting potentially bad port during connectNative failure:", ex.message); }
    }
    NATIVE_PORT = null;
    isNativeHostReady = false;
    portConnectInProgress = false;
    log(LOG_LEVEL.INFO, 'β–Ί Scheduling retry connection in 2 seconds due to critical connection error...');
    setTimeout(ensureNativeConnection, 2000); 
    return false;
  }
}

// DIAGNOSTIC: Simplified postMessageToNativeHost without queueing
function postMessageToNativeHost(messageObject) {
  if (!NATIVE_PORT || !isNativeHostReady) {
    // Host is not currently ready. Buffer the message for later.
    if (messageQueue.length < MESSAGE_QUEUE_MAX) {
      messageQueue.push(messageObject);
      log(LOG_LEVEL.WARN, `⚠️ postMessage: Host not ready – queued message of type ${messageObject.type}. Queue length now ${messageQueue.length}.`);
    } else {
      log(LOG_LEVEL.ERROR, `❌ postMessage: MESSAGE_QUEUE_MAX (${MESSAGE_QUEUE_MAX}) reached, dropping message of type ${messageObject.type}.`);
    }

    // Kick off (or retry) connection attempts so the queue will eventually flush.
    ensureNativeConnection();
    return;
  }

  log(LOG_LEVEL.DEBUG, `β–Ίβ–Ί postMessage: Port valid & host ready. Attempting send for type: ${messageObject.type}`);
  try {
    NATIVE_PORT.postMessage(messageObject);
    log(LOG_LEVEL.DEBUG, `βœ“βœ“ postMessage: Successfully posted message to Host: ${messageObject.type}`);
  } catch (e) {
    log(LOG_LEVEL.ERROR, `❌ postMessage: IMMEDIATE ERROR posting message ${messageObject.type}:`, e.message);
    if (chrome.runtime.lastError) {
        log(LOG_LEVEL.ERROR, `❌ chrome.runtime.lastError after post:`, chrome.runtime.lastError.message);
    }
    isNativeHostReady = false; 
    if (NATIVE_PORT) {
        try { NATIVE_PORT.disconnect(); } catch (ex) { /* ignore */ }
    }
    NATIVE_PORT = null;
    portConnectInProgress = false;
    log(LOG_LEVEL.INFO, 'β–Ί Error during post. Triggering ensureNativeConnection immediately.');
    ensureNativeConnection();
    // The send failed; push the message back onto the head of the queue for a retry.
    messageQueue.unshift(messageObject);
  }
}

function processMessageQueue() {
  if (!NATIVE_PORT || !isNativeHostReady) {
    return; // Nothing to do, will retry when connection established.
  }

  while (messageQueue.length > 0) {
    const msg = messageQueue.shift();
    try {
      NATIVE_PORT.postMessage(msg);
      log(LOG_LEVEL.INFO, `βœ“βœ“ Flushed queued message to Host: ${msg.type}. Remaining queue length: ${messageQueue.length}`);
    } catch (e) {
      log(LOG_LEVEL.ERROR, `❌ Error flushing queued message of type ${msg.type}:`, e.message);
      // Put the message back and break – we'll retry later.
      messageQueue.unshift(msg);
      if (NATIVE_PORT) {
        try { NATIVE_PORT.disconnect(); } catch (_) {}
      }
      NATIVE_PORT = null;
      isNativeHostReady = false;
      ensureNativeConnection();
      break;
    }
  }
}

log(LOG_LEVEL.INFO, "Script evaluated. Initializing native connection...");
ensureNativeConnection(); 

chrome.runtime.onInstalled.addListener(() => {
  log(LOG_LEVEL.INFO, '✨ Extension installed/updated - background.js ready');
  ensureNativeConnection(); 
});

chrome.tabs.onActivated.addListener(({ tabId }) => {
  log(LOG_LEVEL.INFO, `β–Ί Tab activated: ${tabId}. Triggering attach logic.`);
  attemptToAttachDebugger(tabId);
});

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  if (changeInfo.status === 'complete' && tab.url) {
    log(LOG_LEVEL.INFO, `β–Ί Tab updated: ${tabId}, status: 'complete', url: ${tab.url}. Triggering attach logic.`);
    attemptToAttachDebugger(tabId);
  }
});

async function attemptToAttachDebugger(tabId) {
  let tabInfo;
  try {
    tabInfo = await chrome.tabs.get(tabId);
  } catch (e) {
    log(LOG_LEVEL.WARN, `⚠️ Failed to get tab info for tabId ${tabId}:`, e.message);
    return; 
  }

  if (!tabInfo || !tabInfo.url) {
    log(LOG_LEVEL.DEBUG, `⏭️ Skipping debugger attach for tabId ${tabId}: No URL info or tab closed.`);
    return;
  }

  if (tabInfo.url.startsWith('chrome://') || tabInfo.url.startsWith('devtools://') || tabInfo.url.startsWith('chrome-extension://')) {
    log(LOG_LEVEL.INFO, `⏭️ Skipping debugger attach for protected URL: ${tabInfo.url} on tab ${tabId}`);
    return;
  }

  try {
    const attachedTargets = await chrome.debugger.getTargets();
    const isAttached = attachedTargets.some(target => target.tabId === tabId && target.attached);

    if (isAttached) {
      log(LOG_LEVEL.DEBUG, `βœ“ Debugger already attached to tab ${tabId} (${tabInfo.url}).`);
      return;
    }

    log(LOG_LEVEL.INFO, `β–Ί Attaching debugger to tab ${tabId} (${tabInfo.url}).`);
    await chrome.debugger.attach({ tabId }, '1.3');
    log(LOG_LEVEL.INFO, `βœ“ Successfully attached CDP to tab ${tabId} (${tabInfo.url})`);

    try {
      await chrome.debugger.sendCommand({ tabId }, "Page.enable");
      log(LOG_LEVEL.DEBUG, `βœ“ Page domain enabled for tab ${tabId}`);
      await chrome.debugger.sendCommand({ tabId }, "Network.enable");
      log(LOG_LEVEL.DEBUG, `βœ“ Network domain enabled for tab ${tabId}`);
      await chrome.debugger.sendCommand({ tabId }, "Runtime.enable");
      log(LOG_LEVEL.DEBUG, `βœ“ Runtime domain enabled for tab ${tabId}`);
    } catch (e) {
      log(LOG_LEVEL.ERROR, `❌ Error enabling CDP domains for tab ${tabId}:`, e.message);
    }

  } catch (e) {
    log(LOG_LEVEL.ERROR, `❌ Debugger attach failed for tab ${tabId} (${tabInfo.url}):`, e.message);
  }
}

const cdpEventListener = (debuggeeId, method, params) => {
  const tabId = debuggeeId.tabId;
  if (tabId) {
    postMessageToNativeHost({ type: 'cdp', method, params, tabId });
  } else {
    log(LOG_LEVEL.WARN, "⚠️ CDP Event received without tabId in debuggeeId:", debuggeeId, method);
  }
};

try {
  if (chrome.debugger.onEvent.hasListener(cdpEventListener)) {
    chrome.debugger.onEvent.removeListener(cdpEventListener);
  }
} catch (e) { /* Best effort */ }
chrome.debugger.onEvent.addListener(cdpEventListener);
log(LOG_LEVEL.INFO, "Global CDP event listener set up.");

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.source === 'popup' && message.action === 'start_recording') {
    log(LOG_LEVEL.INFO, '[Rebrowse BG] Received start_recording command from popup.');
    postMessageToNativeHost({ type: 'recording_command', command: 'START' });
    // Optional: send an immediate acknowledgement back to popup if needed, 
    // but actual status will come from host.py later.
    sendResponse({ status: 'start_recording_command_sent_to_host' });
    return true; // Indicates you wish to send a response asynchronously (or synchronously)
  } else if (message.source === 'popup' && message.action === 'stop_recording') {
    log(LOG_LEVEL.INFO, '[Rebrowse BG] Received stop_recording command from popup.');
    postMessageToNativeHost({ type: 'recording_command', command: 'STOP' });
    sendResponse({ status: 'stop_recording_command_sent_to_host' });
    return true;
  } else if (message.source === 'popup' && message.action === 'get_recording_status') {
    log(LOG_LEVEL.INFO, `[Rebrowse BG] Popup requested recording status. Sending current status:`, currentRecordingStatus);
    sendResponse({ type: 'recording_status', data: currentRecordingStatus });
    // To prevent the "message port closed" error, it's safest to always
    // return true from a listener that sends a response.
    return true;
  }
  // Keep existing UI event handling
  else if (message.type === 'rebrowse_ui_event' && message.data) {
    const uiEvent = message.data;
    let eventDetails = `type: ${uiEvent.type}`;
    if (uiEvent.type === 'keydown' && typeof uiEvent.key !== 'undefined') {
      eventDetails += `, key: '${uiEvent.key}'`;
    } else if (uiEvent.type === 'mousedown' && typeof uiEvent.selector !== 'undefined') {
      eventDetails += `, selector: '${uiEvent.selector}', button: ${uiEvent.button}`;
    }
    log(LOG_LEVEL.INFO, `β–Ίβ–Ί UI Event (Tab: ${sender.tab ? sender.tab.id : 'N/A'}): ${eventDetails}`);
    postMessageToNativeHost({ type: 'ui_event_to_host', payload: uiEvent });
    sendResponse({status: `UI event '${uiEvent.type}' received by background.js and attempt to forward was made`});
    return false; // No async response needed beyond this for UI events handled here.
  }
  // If the message is not handled by the new popup commands or existing UI event handling,
  // return false or nothing to indicate no response will be sent.
  // log(LOG_LEVEL.DEBUG, "[Rebrowse BG] onMessage: No handler for this message type", message);
  return false; 
});

////////////////////////////////////////////////////////////
// Driver for Huggin Face Space UI
////////////////////////////////////////////////////////////

// 1️⃣ talking to the native host (recording & replay)

// send user events from content-script β†’ host.py β†’ trace
chrome.runtime.onMessage.addListener((msg, sender) => {
  if (msg.type === "user-event") {
    log(LOG_LEVEL.INFO, `[Rebrowse BG] Forwarding 'user-event' to host:`, msg);
    postMessageToNativeHost(msg);
  }
});

// 3️⃣ simple ACK so host.py can await step completion
chrome.runtime.onMessage.addListener((msg, sender, sendResp) => {
  if (msg.type === "step-done") {
    log(LOG_LEVEL.INFO, `[Rebrowse BG] Received 'step-done' from content script. Acknowledging to host. ID: ${msg.id}`);
    postMessageToNativeHost({ type: "ack", id: msg.id });
  }
});