-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
63 lines (54 loc) · 2.02 KB
/
popup.js
File metadata and controls
63 lines (54 loc) · 2.02 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
/**
* Popup Logic
* Handles 3 modes: Visible, Full Page, Timer
*/
const btnVisible = document.getElementById('btnVisible');
const btnFull = document.getElementById('btnFull');
const btnTimer = document.getElementById('btnTimer');
const statusEl = document.getElementById('status');
function setStatus(msg, type = '') {
statusEl.textContent = msg;
statusEl.className = type;
}
async function inject(tabId) {
setStatus('Injecting scripts...');
try {
await chrome.scripting.insertCSS({ target: { tabId }, files: ['styles.css'] });
await chrome.scripting.executeScript({ target: { tabId }, files: ['content.js'] });
} catch (e) {
console.error(e); // Keep for dev debugging in popup console
}
}
async function handleCapture(mode) {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab || tab.url.startsWith('chrome')) {
setStatus('Cannot capture this page', 'error');
return;
}
// 1. Inject
await inject(tab.id);
await new Promise(r => setTimeout(r, 100));
// 2. Dispatch Mode
if (mode === 'visible') {
const dataUrl = await chrome.tabs.captureVisibleTab(null, { format: 'png' });
await chrome.tabs.sendMessage(tab.id, { action: 'startCrop', screenshot: dataUrl });
window.close();
}
else if (mode === 'timer') {
// Tell content script to show countdown (fire and forget)
chrome.tabs.sendMessage(tab.id, { action: 'startTimer' });
window.close();
}
else if (mode === 'full') {
setStatus('Scrolling & Capturing...', '');
chrome.runtime.sendMessage({ action: 'initFullPage', tabId: tab.id });
window.close();
}
} catch (e) {
setStatus('Error: ' + e.message, 'error');
}
}
btnVisible.onclick = () => handleCapture('visible');
btnTimer.onclick = () => handleCapture('timer');
btnFull.onclick = () => handleCapture('full');