-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
37 lines (35 loc) · 1.52 KB
/
background.js
File metadata and controls
37 lines (35 loc) · 1.52 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
// Background script for BlackOCR extension
chrome.runtime.onInstalled.addListener(() => {
console.log('BlackOCR extension installed');
});
// Handle messages from content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'capture-request') {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
const tab = tabs[0];
if (!tab) {
sendResponse({ error: 'No active tab found' });
return;
}
try {
// Use browser.tabs.captureTab for Firefox
if (typeof browser !== 'undefined') {
const dataUrl = await browser.tabs.captureTab(tab.id, { format: 'png' });
sendResponse({ success: true, dataUrl: dataUrl });
} else {
// Fallback for Chrome
chrome.tabs.captureVisibleTab(null, { format: 'png' }, (dataUrl) => {
if (chrome.runtime.lastError) {
sendResponse({ error: chrome.runtime.lastError.message });
return;
}
sendResponse({ success: true, dataUrl: dataUrl });
});
}
} catch (error) {
sendResponse({ error: error.message });
}
});
return true; // Keep the message channel open for async response
}
});