-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.js
166 lines (140 loc) · 4.24 KB
/
screenshot.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
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
function generateFilename(url, extension) {
const urlObj = new URL(url);
const urlParams = new URLSearchParams(urlObj.search);
let text = urlParams.get("q") || "default";
text = text.replace(/\s+/g, "-").replace(/\+/g, "-").toLowerCase();
const date = new Date();
const timestamp = `${date.getFullYear()}${String(
date.getMonth() + 1
).padStart(2, "0")}${String(date.getDate()).padStart(2, "0")}${String(
date.getHours()
).padStart(2, "0")}${String(date.getMinutes()).padStart(2, "0")}${String(
date.getSeconds()
).padStart(2, "0")}`;
return `${text}-${timestamp}.${extension}`;
}
async function startFullPageScreenshot() {
const screenshots = [];
const viewportHeight = window.innerHeight;
const viewportWidth = window.innerWidth;
const totalHeight = document.documentElement.scrollHeight;
const totalWidth = document.documentElement.scrollWidth;
function hideStickyElements() {
const stickyElements = document.querySelectorAll(
"nav, header, .sticky, .minidiv, [style*='position: sticky'], [style*='position: fixed'], .CvDJxb"
);
stickyElements.forEach((el) => {
el.dataset.originalDisplay = el.style.display;
el.style.display = "none";
});
}
function showStickyElements() {
const stickyElements = document.querySelectorAll(
"nav, header, .sticky, .minidiv, [style*='position: sticky'], [style*='position: fixed'], .CvDJxb"
);
stickyElements.forEach((el) => {
el.style.display = el.dataset.originalDisplay || "";
});
}
function hideScrollbar() {
document.body.style.overflowY = "hidden";
}
function showScrollbar() {
document.body.style.overflowY = "";
}
async function captureVisibleTab() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ action: "capture" }, (dataUrl) => {
if (dataUrl) {
resolve(dataUrl);
} else {
console.error("Failed to capture screenshot.");
resolve(null);
}
});
});
}
window.scrollTo(0, 0);
await new Promise((resolve) => setTimeout(resolve, 500));
hideScrollbar();
const firstScreenshot = await captureVisibleTab();
if (firstScreenshot) {
screenshots.push({
image: firstScreenshot,
y: 0,
height: viewportHeight
});
}
hideStickyElements();
let scrollY = viewportHeight;
while (scrollY < totalHeight) {
window.scrollTo(0, scrollY);
await new Promise((resolve) => setTimeout(resolve, 500));
let captureHeight = viewportHeight;
if (scrollY + viewportHeight > totalHeight) {
captureHeight = totalHeight - scrollY;
}
const screenshot = await captureVisibleTab();
if (screenshot) {
screenshots.push({
image: screenshot,
y: scrollY,
height: captureHeight
});
}
scrollY += viewportHeight;
}
showStickyElements();
showScrollbar();
await stitchScreenshots(
screenshots,
totalWidth,
totalHeight,
viewportWidth,
viewportHeight
);
}
async function stitchScreenshots(
screenshots,
fullWidth,
fullHeight,
viewportWidth,
viewportHeight
) {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const devicePixelRatio = window.devicePixelRatio;
canvas.width = fullWidth * devicePixelRatio;
canvas.height = fullHeight * devicePixelRatio;
let accumulatedHeight = 0;
for (let i = 0; i < screenshots.length; i++) {
const img = new Image();
img.src = screenshots[i].image;
const height = screenshots[i].height * devicePixelRatio;
await new Promise((resolve) => {
img.onload = () => {
ctx.drawImage(
img,
0,
(viewportHeight - screenshots[i].height) * devicePixelRatio,
viewportWidth * devicePixelRatio,
height,
0,
accumulatedHeight,
viewportWidth * devicePixelRatio,
height
);
accumulatedHeight += height;
resolve();
};
});
}
const finalImage = canvas.toDataURL("image/png");
const link = document.createElement("a");
link.href = finalImage;
link.download = generateFilename(document.location.href, "png");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
startFullPageScreenshot();