-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
57 lines (55 loc) · 2.23 KB
/
background.js
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
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "capturePage") {
capturePage();
}
});
function capturePage() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
let activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, { action: "getViewportHeight" }, (response) => {
if (chrome.runtime.lastError || !response) {
console.error("Error getting viewport height:", JSON.stringify(chrome.runtime.lastError, null, 2));
return;
}
const viewportHeight = response.viewportHeight;
chrome.scripting.executeScript({
target: { tabId: activeTab.id },
func: () => document.body.scrollHeight,
}, (results) => {
if (chrome.runtime.lastError || !results) {
console.error("Error executing script:", chrome.runtime.lastError);
return;
}
let maxScrollHeight = results[0].result;
scrollAndCapture(activeTab, 0, maxScrollHeight, viewportHeight);
});
});
});
}
function scrollAndCapture(tab, scrollHeight, maxScrollHeight, scrollStep) {
if (scrollHeight < maxScrollHeight) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (height) => window.scrollTo(0, height),
args: [scrollHeight],
}, () => {
if (chrome.runtime.lastError) {
console.error("Error scrolling:", chrome.runtime.lastError);
return;
}
setTimeout(() => {
chrome.tabs.captureVisibleTab(null, { format: 'png' }, (dataUrl) => {
if (chrome.runtime.lastError) {
console.error("Error capturing:", chrome.runtime.lastError);
return;
}
// Handle the captured image, e.g., save it to storage.
console.log(dataUrl);
scrollAndCapture(tab, scrollHeight + scrollStep, maxScrollHeight, scrollStep); // Recursive call
});
}, 1000); // Adjust timing as necessary
});
} else {
console.log('Capture complete.');
}
}