File size: 5,378 Bytes
0533780
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// content.js β€” Injected into every page
// Manages the screen selection overlay and sidebar panel

let overlayActive = false;
let sidebarFrame  = null;

// ── Listen for messages from background / popup ───────────────────────────────

chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {

  if (msg.type === "START_SELECTION") {
    if (!overlayActive) startSelection();
    sendResponse({ ok: true });
    return false;
  }

  if (msg.type === "SHOW_SIDEBAR") {
    showSidebar({});
    return false;
  }

  if (msg.type === "SHOW_RESULT") {
    showSidebar(msg.data);
    return false;
  }

});

// ── Selection overlay ─────────────────────────────────────────────────────────

function startSelection() {
  overlayActive = true;

  // Dim the page
  const overlay = document.createElement("div");
  overlay.id = "glmocr-overlay";

  // Crosshair hint
  const hint = document.createElement("div");
  hint.id = "glmocr-hint";
  hint.textContent = "Drag to select text region β€” Press Esc to cancel";
  overlay.appendChild(hint);

  // Selection box
  const selBox = document.createElement("div");
  selBox.id = "glmocr-selbox";
  overlay.appendChild(selBox);

  document.body.appendChild(overlay);

  let startX = 0, startY = 0, isDragging = false;

  function onMouseDown(e) {
    if (e.button !== 0) return;
    isDragging = true;
    startX = e.clientX;
    startY = e.clientY;
    selBox.style.cssText = `left:${startX}px; top:${startY}px; width:0; height:0; display:block`;
    hint.style.opacity = "0";
    e.preventDefault();
  }

  function onMouseMove(e) {
    if (!isDragging) return;
    const x = Math.min(e.clientX, startX);
    const y = Math.min(e.clientY, startY);
    const w = Math.abs(e.clientX - startX);
    const h = Math.abs(e.clientY - startY);
    selBox.style.cssText = `left:${x}px; top:${y}px; width:${w}px; height:${h}px; display:block`;
  }

  function onMouseUp(e) {
    if (!isDragging) return;
    isDragging = false;

    const x = Math.min(e.clientX, startX);
    const y = Math.min(e.clientY, startY);
    const w = Math.abs(e.clientX - startX);
    const h = Math.abs(e.clientY - startY);

    cleanup();

    if (w < 10 || h < 10) {
      showToast("Selection too small β€” try again.");
      return;
    }

    const dpr = window.devicePixelRatio || 1;
    const rect = {
      x:      x + window.scrollX,
      y:      y + window.scrollY,
      width:  w,
      height: h,
      dpr,
    };

    runOcr(rect);
  }

  function onKeyDown(e) {
    if (e.key === "Escape") {
      cleanup();
      showToast("Cancelled.");
    }
  }

  function cleanup() {
    overlayActive = false;
    overlay.removeEventListener("mousedown", onMouseDown);
    overlay.removeEventListener("mousemove", onMouseMove);
    overlay.removeEventListener("mouseup",   onMouseUp);
    document.removeEventListener("keydown",  onKeyDown);
    overlay.remove();
  }

  overlay.addEventListener("mousedown", onMouseDown);
  overlay.addEventListener("mousemove", onMouseMove);
  overlay.addEventListener("mouseup",   onMouseUp);
  document.addEventListener("keydown",  onKeyDown);
}

// ── Send region to background for capture + OCR ───────────────────────────────

function runOcr(rect) {
  // Show a loading sidebar immediately
  showSidebar({ loading: true });

  chrome.runtime.sendMessage({ type: "CAPTURE_REGION", rect }, (response) => {
    if (chrome.runtime.lastError) {
      showSidebar({ error: chrome.runtime.lastError.message });
      return;
    }
    if (response.success) {
      showSidebar(response);
    } else {
      showSidebar({ error: response.error });
    }
  });
}

// ── Sidebar panel ─────────────────────────────────────────────────────────────

function showSidebar(data) {
  // Remove existing sidebar if any
  if (sidebarFrame) sidebarFrame.remove();

  const frame = document.createElement("iframe");
  frame.id  = "glmocr-sidebar";
  frame.src = chrome.runtime.getURL("sidebar.html");

  document.body.appendChild(frame);
  sidebarFrame = frame;

  // Wait for iframe to load, then send data
  frame.onload = () => {
    frame.contentWindow.postMessage({ type: "SIDEBAR_DATA", data }, "*");
  };

  // Close button via message from sidebar
  window.addEventListener("message", (e) => {
    if (e.data?.type === "CLOSE_SIDEBAR") {
      frame.remove();
      sidebarFrame = null;
    }
    if (e.data?.type === "START_NEW_SELECTION") {
      frame.remove();
      sidebarFrame = null;
      startSelection();
    }
  });
}

// ── Toast notification ────────────────────────────────────────────────────────

function showToast(msg) {
  const existing = document.getElementById("glmocr-toast");
  if (existing) existing.remove();

  const toast = document.createElement("div");
  toast.id = "glmocr-toast";
  toast.textContent = msg;
  document.body.appendChild(toast);
  setTimeout(() => toast?.remove(), 3000);
}