-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_worker.js
More file actions
27 lines (23 loc) · 873 Bytes
/
service_worker.js
File metadata and controls
27 lines (23 loc) · 873 Bytes
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
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
(async () => {
if (msg?.type !== "CAPTURE_VISIBLE") {
sendResponse({ ok: false, error: "Unknown message" });
return;
}
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab?.windowId || !tab?.url) {
sendResponse({ ok: false, error: "No active tab" });
return;
}
if (tab.url.startsWith("chrome://") || tab.url.startsWith("chrome-extension://")) {
sendResponse({ ok: false, error: "Cannot capture chrome:// pages" });
return;
}
const dataUrl = await chrome.tabs.captureVisibleTab(tab.windowId, { format: "png" });
sendResponse({ ok: true, dataUrl });
})().catch((e) => {
console.error("SW error:", e);
sendResponse({ ok: false, error: String(e) });
});
return true; // async
});