-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture_page_with_images.js
73 lines (64 loc) · 2.45 KB
/
capture_page_with_images.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
// ==UserScript==
// @name Capture Page with Images
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Capture page content and images every 10 seconds
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.onload = function() {
const getTimestamp = () => {
const now = new Date();
return now.toISOString().replace(/[:.]/g, '-');
};
const getImageDataURL = async (url) => {
try {
const response = await fetch(url);
const blob = await response.blob();
const reader = new FileReader();
return new Promise((resolve) => {
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
} catch (error) {
console.error('Error fetching image:', error);
return url;
}
};
const processImages = async (html) => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const images = Array.from(doc.querySelectorAll('img'));
const imagePromises = images.map(async (img) => {
const dataURL = await getImageDataURL(img.src);
img.src = dataURL;
});
await Promise.all(imagePromises);
return doc.documentElement.outerHTML;
};
const downloadHTML = (htmlContent) => {
const timestamp = getTimestamp();
const blob = new Blob([htmlContent], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `page-${timestamp}.html`;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 100);
};
const capturePageWithImages = async () => {
console.log('Starting capture...');
const htmlContent = document.documentElement.outerHTML;
const updatedHtmlContent = await processImages(htmlContent);
downloadHTML(updatedHtmlContent);
console.log('Capture complete.');
};
setInterval(capturePageWithImages, 10000);
};
})();