Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
367 lines (311 loc) · 10.7 KB
/
Copy pathbackground.js
File metadata and controls
367 lines (311 loc) · 10.7 KB
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
/**
* TIFF Viewer - Background Service Worker
*
* This service worker handles:
* 1. TIFF file interception - Redirects TIFF URLs to the viewer
* 2. Download interception - Catches TIFF downloads and opens in viewer
* 3. OCR message routing - Forwards OCR requests between viewer and offscreen document
*
* Interception Methods:
* - declarativeNetRequest: Creates dynamic redirect rules for TIFF URLs
* - downloads.onDeterminingFilename: Catches TIFF downloads
* - tabs.onUpdated/onCreated: Detects navigation to TIFF URLs
* - webNavigation.onBeforeNavigate: Early detection of TIFF navigation
*/
// Production mode - set to false to enable debug logging
const DEBUG = false;
const log = DEBUG ? console.log.bind(console) : () => {};
const logError = DEBUG ? console.error.bind(console) : () => {};
// ==================== Configuration ====================
/** Regex to match TIFF file extensions in URLs */
const TIFF_EXTENSIONS = /\.(tiff?|tif)(\?.*)?$/i;
/** URL of the viewer page */
const VIEWER_URL = chrome.runtime.getURL('viewer/viewer.html');
// ==================== Dynamic Rule Management ====================
//
// Chrome's declarativeNetRequest requires rules to be created before
// navigation occurs. We create temporary rules when TIFF URLs are detected
// and clean them up after 30 seconds.
//
/** Counter for generating unique rule IDs */
let ruleIdCounter = 1;
/** Map of URL -> rule ID for active redirect rules */
const pendingRules = new Map();
/**
* Create a dynamic redirect rule for a specific URL
*/
async function createRedirectRule(url) {
// Check if we already have a rule for this URL
if (pendingRules.has(url)) {
return;
}
const ruleId = ruleIdCounter++;
pendingRules.set(url, ruleId);
const viewerUrl = `${VIEWER_URL}?url=${encodeURIComponent(url)}`;
try {
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
id: ruleId,
priority: 1,
action: {
type: 'redirect',
redirect: { url: viewerUrl }
},
condition: {
urlFilter: url,
resourceTypes: ['main_frame']
}
}],
removeRuleIds: []
});
log(`[TIFF Viewer] Created redirect rule ${ruleId} for: ${url}`);
// Clean up rule after 30 seconds (it should have been used by then)
setTimeout(() => removeRedirectRule(url), 30000);
} catch (err) {
logError('[TIFF Viewer] Failed to create redirect rule:', err);
pendingRules.delete(url);
}
}
/**
* Remove a dynamic redirect rule
*/
async function removeRedirectRule(url) {
const ruleId = pendingRules.get(url);
if (!ruleId) return;
try {
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [],
removeRuleIds: [ruleId]
});
pendingRules.delete(url);
log(`[TIFF Viewer] Removed redirect rule ${ruleId}`);
} catch (err) {
logError('[TIFF Viewer] Failed to remove redirect rule:', err);
}
}
/**
* Check if URL is a TIFF file by extension
*/
function isTiffUrl(url) {
try {
const urlObj = new URL(url);
return TIFF_EXTENSIONS.test(urlObj.pathname);
} catch {
return TIFF_EXTENSIONS.test(url);
}
}
/**
* Open TIFF in viewer
*/
function openInViewer(url, tabId = null) {
const viewerUrl = `${VIEWER_URL}?url=${encodeURIComponent(url)}`;
if (tabId) {
chrome.tabs.update(tabId, { url: viewerUrl });
} else {
chrome.tabs.create({ url: viewerUrl });
}
}
// ==================== Method 1: Download Interception ====================
//
// When Chrome decides to download a TIFF (instead of displaying it),
// we cancel the download and open the URL in our viewer instead.
// Note: blob: URLs are excluded since they're intentional saves from our viewer.
//
chrome.downloads.onDeterminingFilename.addListener((downloadItem, suggest) => {
const url = downloadItem.url;
const filename = downloadItem.filename || '';
const mime = downloadItem.mime || '';
// Don't intercept blob URLs (these are intentional saves from our viewer)
if (url.startsWith('blob:')) {
suggest({ filename: downloadItem.filename });
return;
}
const isTiff =
TIFF_EXTENSIONS.test(filename) ||
TIFF_EXTENSIONS.test(url) ||
mime === 'image/tiff' ||
mime === 'image/x-tiff';
if (isTiff) {
log(`[TIFF Viewer] Intercepted download: ${url}`);
// Cancel the download
chrome.downloads.cancel(downloadItem.id);
// Remove the cancelled download from history
setTimeout(() => {
chrome.downloads.erase({ id: downloadItem.id });
}, 100);
// Open in viewer
openInViewer(url);
// Don't suggest a filename (download is cancelled)
return;
}
// Let other downloads proceed normally
suggest({ filename: downloadItem.filename });
});
// ==================== Method 2: Tab Navigation Interception ====================
//
// Listen for tab URL changes that involve TIFF files.
// When detected, redirect the tab to our viewer.
//
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url && isTiffUrl(changeInfo.url)) {
// Skip if it's already our viewer
if (changeInfo.url.includes(VIEWER_URL)) return;
log(`[TIFF Viewer] Tab navigating to TIFF: ${changeInfo.url}`);
openInViewer(changeInfo.url, tabId);
}
});
// Also check when a tab is created with a TIFF URL
chrome.tabs.onCreated.addListener((tab) => {
if (tab.pendingUrl && isTiffUrl(tab.pendingUrl)) {
if (tab.pendingUrl.includes(VIEWER_URL)) return;
log(`[TIFF Viewer] New tab with TIFF: ${tab.pendingUrl}`);
createRedirectRule(tab.pendingUrl);
}
});
// ==================== Method 3: Early Navigation Interception ====================
//
// Use webNavigation API for early detection of TIFF URL navigation.
// Creates redirect rules before the request is fully processed.
//
chrome.webNavigation?.onBeforeNavigate?.addListener((details) => {
// Only main frame
if (details.frameId !== 0) return;
const url = details.url;
// Skip our viewer
if (url.includes('viewer/viewer.html')) return;
if (isTiffUrl(url)) {
log(`[TIFF Viewer] Navigation to TIFF: ${url}`);
// Create a redirect rule immediately
createRedirectRule(url);
}
});
// ==================== Rule Cleanup ====================
//
// Clean up any leftover redirect rules from previous sessions.
// Rules are created dynamically and should be removed, but this
// handles cases where cleanup didn't happen (e.g., browser crash).
//
chrome.runtime.onStartup.addListener(async () => {
try {
const rules = await chrome.declarativeNetRequest.getDynamicRules();
if (rules.length > 0) {
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [],
removeRuleIds: rules.map(r => r.id)
});
log(`[TIFF Viewer] Cleaned up ${rules.length} old rules`);
}
} catch (err) {
logError('[TIFF Viewer] Cleanup error:', err);
}
});
// Also clean up on install
chrome.runtime.onInstalled.addListener(async () => {
try {
const rules = await chrome.declarativeNetRequest.getDynamicRules();
if (rules.length > 0) {
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [],
removeRuleIds: rules.map(r => r.id)
});
}
log('[TIFF Viewer] Extension installed/updated');
} catch (err) {
logError('[TIFF Viewer] Install cleanup error:', err);
}
});
// ==================== OCR via Offscreen Document ====================
//
// Chrome MV3 extensions cannot run Web Workers in the service worker.
// We use an offscreen document to host the Tesseract.js worker.
//
// Message Flow:
// 1. Viewer sends ocr-init/ocr-recognize/ocr-terminate to background
// 2. Background ensures offscreen document exists
// 3. Background forwards message to offscreen with a unique requestId
// 4. Offscreen processes and sends ocr-response back with the requestId
// 5. Background matches response to pending callback and resolves
//
// Note: We use request IDs because Chrome's sendResponse times out
// before long OCR operations complete.
//
/** Promise tracking offscreen document creation to prevent duplicates */
let creatingOffscreen = null;
/** Counter for generating unique OCR request IDs */
let ocrRequestId = 0;
/** Map of requestId -> sendResponse callback for pending OCR requests */
const pendingOcrRequests = new Map();
/**
* Ensure the offscreen document exists for OCR processing
* Creates it if needed, waits if creation is already in progress
*/
async function setupOffscreen() {
const existingContexts = await chrome.runtime.getContexts({
contextTypes: ['OFFSCREEN_DOCUMENT'],
documentUrls: [chrome.runtime.getURL('offscreen/offscreen.html')]
});
if (existingContexts.length > 0) {
return;
}
if (creatingOffscreen) {
await creatingOffscreen;
} else {
creatingOffscreen = chrome.offscreen.createDocument({
url: 'offscreen/offscreen.html',
reasons: ['WORKERS'],
justification: 'Run Tesseract.js OCR worker for text recognition'
});
await creatingOffscreen;
creatingOffscreen = null;
}
}
// Handle messages
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Handle OCR requests from viewer
if (message.type === 'ocr-init' || message.type === 'ocr-recognize' || message.type === 'ocr-terminate') {
const requestId = ++ocrRequestId;
// Store the response callback
pendingOcrRequests.set(requestId, sendResponse);
// Forward to offscreen with request ID
(async () => {
try {
await setupOffscreen();
// Send message with request ID - offscreen will respond with ocr-response
chrome.runtime.sendMessage({
...message,
requestId
});
} catch (err) {
logError('[TIFF Viewer] OCR setup error:', err);
const callback = pendingOcrRequests.get(requestId);
if (callback) {
callback({ success: false, error: err.message || String(err) });
pendingOcrRequests.delete(requestId);
}
}
})();
return true; // Keep channel open
}
// Handle OCR responses from offscreen
if (message.type === 'ocr-response' && sender.url?.includes('offscreen')) {
const { requestId, ...response } = message;
const callback = pendingOcrRequests.get(requestId);
if (callback) {
log('[TIFF Viewer] OCR response received for request:', requestId);
callback(response);
pendingOcrRequests.delete(requestId);
}
return false;
}
// Forward progress messages to viewer tabs
if (message.type === 'ocr-progress' && sender.url?.includes('offscreen')) {
chrome.tabs.query({}, (tabs) => {
for (const tab of tabs) {
if (tab.url?.includes('viewer/viewer.html')) {
chrome.tabs.sendMessage(tab.id, message).catch(() => {});
}
}
});
}
});
log('[TIFF Viewer] Service worker loaded');