// Isolated-world content script. Listens for tagged window messages from
// inject.js (which runs in the MAIN world) and forwards them to the
// background service worker, which persists them to chrome.storage.local.

(() => {
  const TAG = "__casino_org_inspector__";

  window.addEventListener("message", (e) => {
    if (e.source !== window) return;
    const d = e.data;
    if (!d || typeof d !== "object" || !d[TAG]) return;

    const entry = d.payload;
    if (!entry || typeof entry !== "object") return;

    try {
      chrome.runtime.sendMessage(
        {
          kind: "captured",
          entry,
          pageUrl: location.href,
          pageTitle: document.title
        },
        () => void chrome.runtime.lastError
      );
    } catch (_) {
      // Extension context invalidated (e.g. during reload). Drop silently.
    }
  }, true);
})();
