File size: 10,345 Bytes
0f27de7
3337c5d
4c0afe5
3337c5d
 
 
 
0f27de7
 
3337c5d
4c0afe5
3337c5d
4c0afe5
3337c5d
4c0afe5
 
 
3337c5d
 
4c0afe5
 
 
3337c5d
 
 
 
 
 
 
 
 
 
4c0afe5
3337c5d
4c0afe5
0f27de7
 
4c0afe5
 
0f27de7
4c0afe5
0f27de7
3337c5d
 
0f27de7
 
 
 
 
 
3337c5d
 
0f27de7
 
 
 
 
3337c5d
0f27de7
 
3337c5d
0f27de7
 
 
 
 
 
 
3337c5d
 
 
0f27de7
3337c5d
 
 
4c0afe5
0f27de7
 
 
3337c5d
 
7902802
3337c5d
0f27de7
 
3337c5d
0f27de7
 
 
 
 
 
 
 
 
 
 
 
 
 
3337c5d
0f27de7
4c0afe5
0f27de7
 
 
 
3337c5d
0f27de7
 
 
 
 
 
3337c5d
0f27de7
 
 
 
 
3337c5d
0f27de7
 
3337c5d
4c0afe5
25b4391
3337c5d
4c0afe5
 
3337c5d
4c0afe5
 
3337c5d
 
 
0f27de7
 
3337c5d
4c0afe5
 
 
 
0f27de7
4c0afe5
 
 
 
 
 
0f27de7
4c0afe5
 
 
 
 
 
 
0f27de7
 
3337c5d
4c0afe5
0f27de7
3337c5d
0f27de7
 
3337c5d
0f27de7
4c0afe5
3337c5d
 
4c0afe5
 
 
0f27de7
3337c5d
0f27de7
4c0afe5
0f27de7
3337c5d
4c0afe5
 
0f27de7
3337c5d
0f27de7
4c0afe5
 
 
 
 
 
 
0f27de7
3337c5d
0f27de7
4c0afe5
 
 
 
 
0f27de7
3337c5d
0f27de7
4c0afe5
3337c5d
0f27de7
3337c5d
4c0afe5
 
 
 
 
 
3337c5d
 
 
4c0afe5
 
0f27de7
 
 
3337c5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f27de7
 
 
3337c5d
4c0afe5
3337c5d
4c0afe5
 
0f27de7
3337c5d
 
 
 
 
 
 
25b4391
3337c5d
 
 
0f27de7
 
3337c5d
0f27de7
3337c5d
 
 
 
 
0f27de7
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
/**
 * Authrix Extension β€” Content Script v3
 *
 * Responsibilities:
 *  - Render the overlay UI (loading, result, error states)
 *  - Relay messages from background to the overlay
 *  - Recording is handled by offscreen.js (MV3 requirement)
 */

// ── Message listener ──────────────────────────────────────────────────────────
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {

  if (msg.type === 'SHOW_CAPTURE_OVERLAY') {
    showOverlay('capture');
    sendResponse({ ok: true });
  }

  if (msg.type === 'SHOW_URL_ANALYSIS_OVERLAY') {
    showOverlay('url', msg.url);
    sendResponse({ ok: true });
  }

  if (msg.type === 'CAPTURE_PROGRESS') {
    const pct = Math.min(95, Math.round((msg.elapsed / msg.total) * 65));
    updateLoadingText(`Recording: ${msg.elapsed}s / ${msg.total}s`);
    if (msg.elapsed === 1) activateStep(0);
    if (msg.elapsed >= msg.total) {
      activateStep(1);
      updateLoadingText('Processing frames β€” running AI analysis…');
    }
  }

  if (msg.type === 'ANALYSIS_RESULT') {
    activateStep(3);
    renderResult(msg.result);
  }

  if (msg.type === 'ANALYSIS_ERROR') {
    showError(msg.error);
  }
});

// ── Show overlay ──────────────────────────────────────────────────────────────
function showOverlay(mode = 'capture', url = '') {
  document.getElementById('authrix-overlay')?.remove();

  const overlay = document.createElement('div');
  overlay.id = 'authrix-overlay';
  overlay.innerHTML = `
    <div id="authrix-panel">

      <!-- Header -->
      <div id="authrix-header">
        <div id="authrix-logo">
          <span id="authrix-logo-icon">β—‰</span>
          <span>AUTHRIX AI</span>
        </div>
        <button id="authrix-close" title="Close">βœ•</button>
      </div>

      <!-- Loading state -->
      <div id="authrix-loading">
        <div id="authrix-radar">
          <div class="authrix-ring r1"></div>
          <div class="authrix-ring r2"></div>
          <div class="authrix-ring r3"></div>
          <div id="authrix-radar-dot"></div>
        </div>
        <div id="authrix-loading-text">
          ${mode === 'url' ? 'Downloading &amp; analyzing video…' : 'Initializing capture…'}
        </div>
        <div id="authrix-steps">
          <div class="authrix-step" id="as0">
            ${mode === 'url' ? '⬇️ Downloading video' : '🎬 Recording tab stream'}
          </div>
          <div class="authrix-step" id="as1">πŸ–ΌοΈ Extracting frames</div>
          <div class="authrix-step" id="as2">🧠 Running AI models</div>
          <div class="authrix-step" id="as3">πŸ“Š Generating report</div>
        </div>
        ${mode === 'url'
          ? `<div id="authrix-note" style="font-family:monospace;font-size:10px;word-break:break-all;">${escHtml(url.slice(0, 80))}${url.length > 80 ? '…' : ''}</div>`
          : `<div id="authrix-note">Recording ~8 seconds of video for analysis</div>`
        }
      </div>

      <!-- Result state -->
      <div id="authrix-result" style="display:none;">
        <div id="authrix-verdict-row">
          <div id="authrix-badge"></div>
          <div id="authrix-verdict-text"></div>
          <div id="authrix-conf"></div>
        </div>
        <div id="authrix-conf-bar-wrap">
          <div id="authrix-conf-bar"></div>
        </div>
        <div id="authrix-details"></div>
        <div id="authrix-audio-row" style="display:none;">
          <span id="authrix-audio-icon">πŸŽ™οΈ</span>
          <span id="authrix-audio-label"></span>
        </div>
        <div id="authrix-meta"></div>
        <div id="authrix-actions">
          <button id="authrix-reanalyze">β†Ί Capture Again</button>
          <button id="authrix-open-app">Open Authrix App β†—</button>
        </div>
      </div>

      <!-- Error state -->
      <div id="authrix-error" style="display:none;">
        <div id="authrix-error-icon">⚠</div>
        <div id="authrix-error-msg"></div>
        <div id="authrix-error-hint"></div>
        <button id="authrix-retry">β†Ί Retry</button>
      </div>

    </div>
  `;

  document.body.appendChild(overlay);

  // Wire up buttons
  document.getElementById('authrix-close').onclick = () => overlay.remove();
  overlay.addEventListener('click', e => { if (e.target === overlay) overlay.remove(); });

  document.getElementById('authrix-open-app').onclick = () =>
    window.open('https://aarav13-authrix.hf.space', '_blank');

  document.getElementById('authrix-reanalyze').onclick = () =>
    chrome.runtime.sendMessage({ type: 'START_CAPTURE' });

  document.getElementById('authrix-retry').onclick = () =>
    chrome.runtime.sendMessage({ type: 'START_CAPTURE' });

  // Auto-activate first step for URL mode (no recording phase)
  if (mode === 'url') activateStep(1);
}

// ── Loading helpers ───────────────────────────────────────────────────────────
function updateLoadingText(text) {
  const el = document.getElementById('authrix-loading-text');
  if (el) el.textContent = text;
}

function activateStep(idx) {
  document.querySelectorAll('.authrix-step').forEach((el, i) => {
    el.classList.toggle('active', i === idx);
    el.classList.toggle('done',   i < idx);
  });
}

function showState(state) {
  const loading = document.getElementById('authrix-loading');
  const result  = document.getElementById('authrix-result');
  const error   = document.getElementById('authrix-error');
  if (loading) loading.style.display = state === 'loading' ? 'flex' : 'none';
  if (result)  result.style.display  = state === 'result'  ? 'block' : 'none';
  if (error)   error.style.display   = state === 'error'   ? 'flex'  : 'none';
}

// ── Render result ─────────────────────────────────────────────────────────────
function renderResult(data) {
  const isFake = data.result === 'FAKE';
  const conf   = data.confidence ?? 0;
  const color  = isFake ? '#ff4466' : '#00ff9c';

  // Badge
  const badge = document.getElementById('authrix-badge');
  if (badge) {
    badge.textContent       = isFake ? '⚠' : 'βœ“';
    badge.style.color       = color;
    badge.style.borderColor = color;
    badge.style.boxShadow   = `0 0 16px ${color}44`;
  }

  // Verdict text
  const vt = document.getElementById('authrix-verdict-text');
  if (vt) { vt.textContent = isFake ? 'DEEPFAKE DETECTED' : 'AUTHENTIC VIDEO'; vt.style.color = color; }

  // Confidence number
  const cv = document.getElementById('authrix-conf');
  if (cv) { cv.textContent = conf + '%'; cv.style.color = color; }

  // Confidence bar
  const bar = document.getElementById('authrix-conf-bar');
  if (bar) {
    bar.style.background = isFake
      ? 'linear-gradient(90deg,#880022,#ff4466)'
      : 'linear-gradient(90deg,#006633,#00ff9c)';
    bar.style.boxShadow = `0 0 8px ${color}66`;
    setTimeout(() => { bar.style.width = conf + '%'; }, 80);
  }

  // Detail bullets (max 3)
  const dl = document.getElementById('authrix-details');
  if (dl) {
    dl.innerHTML = (data.details || []).slice(0, 3).map(d =>
      `<div class="authrix-detail" style="border-left-color:${color};">${escHtml(d)}</div>`
    ).join('');
  }

  // Audio row
  const audioRow = document.getElementById('authrix-audio-row');
  if (audioRow && data.audio?.available) {
    const isAI      = data.audio.result === 'AI_VOICE';
    const isMismatch = data.audio.result === 'AV_MISMATCH';
    const aColor    = (isAI || isMismatch) ? '#ff4466' : '#00ff9c';
    const audioIcon  = document.getElementById('authrix-audio-icon');
    const audioLabel = document.getElementById('authrix-audio-label');
    if (audioIcon)  audioIcon.textContent  = (isAI || isMismatch) ? 'πŸ€–' : 'πŸŽ™οΈ';
    if (audioLabel) {
      audioLabel.textContent = isMismatch
        ? 'AV Mismatch β€” face-swap detected'
        : isAI
          ? `AI Voice (${data.audio.confidence}%)`
          : `Human Voice (${data.audio.confidence}%)`;
      audioLabel.style.color = aColor;
    }
    audioRow.style.display = 'flex';
  }

  // Metadata row
  const meta = document.getElementById('authrix-meta');
  if (meta && data.metadata) {
    const m = data.metadata;
    const parts = [];
    if (m.video_duration_sec) parts.push(`${m.video_duration_sec}s`);
    if (m.resolution && m.resolution !== '0x0') parts.push(m.resolution);
    if (m.frames_with_faces != null) parts.push(`${m.frames_with_faces} faces`);
    if (data.processing_time_sec) parts.push(`${data.processing_time_sec}s analysis`);
    if (parts.length) {
      meta.textContent = parts.join(' Β· ');
      meta.style.cssText = 'font-size:10px;color:rgba(255,255,255,0.25);margin-bottom:12px;font-family:monospace;';
    }
  }

  showState('result');
}

// ── Show error ────────────────────────────────────────────────────────────────
function showError(message) {
  const isOffline = !message || message.includes('fetch') || message.includes('Failed to fetch') || message.includes('ERR_CONNECTION');
  const errMsg  = document.getElementById('authrix-error-msg');
  const errHint = document.getElementById('authrix-error-hint');

  if (errMsg) {
    errMsg.textContent = isOffline
      ? 'Authrix server is not running'
      : (message || 'Unknown error');
  }
  if (errHint) {
    errHint.textContent = isOffline
      ? 'Visit https://aarav13-authrix.hf.space to check server status'
      : 'Make sure a video is playing before capturing.';
  }
  showState('error');
}

// ── Utility ───────────────────────────────────────────────────────────────────
function escHtml(s) {
  return String(s)
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;');
}