-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download Page as HTML-1.3.user.js
110 lines (96 loc) · 3.7 KB
/
Download Page as HTML-1.3.user.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
// ==UserScript==
// @name Download Page as HTML
// @namespace https://github.com/noarche/
// @version 1.3
// @description Adds a button to download the current page as a complete HTML file with all images and resources embedded.
// @author noarche
// @match *://*/*
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @connect *
// ==/UserScript==
(function() {
'use strict';
// Apply custom styles to the button
GM_addStyle(`
.download-button {
padding: 2px 2px;
font-size: 10px;
cursor: pointer;
background-color: #343b65;
color: #FFF;
border: none;
border-radius: 2px;
transition: background-color 2.5s;
position: fixed;
bottom: 2px;
right: 2px;
z-index: 1000;
}
.download-button:hover {
background-color: #6bb28e;
}
`);
// Create a button element
const button = document.createElement('button');
button.innerHTML = 'Save Page';
button.className = 'download-button';
// Add button to the document
document.body.appendChild(button);
// Function to convert a URL to a data URL
const urlToDataURL = (url) => {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: url,
responseType: 'blob',
onload: (response) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(response.response);
},
onerror: reject
});
});
};
// Function to replace URLs with data URLs for images, stylesheets, and other resources
const replaceResourceURLs = async () => {
const resources = document.querySelectorAll('img, link[rel="stylesheet"], script[src]');
for (let resource of resources) {
try {
const originalURL = resource.src || resource.href;
const dataURL = await urlToDataURL(originalURL);
if (resource.tagName.toLowerCase() === 'img') {
resource.src = dataURL;
} else if (resource.tagName.toLowerCase() === 'link') {
resource.href = dataURL;
} else if (resource.tagName.toLowerCase() === 'script') {
resource.src = dataURL;
}
} catch (error) {
console.error('Failed to convert resource:', resource, error);
}
}
};
// Function to download the page as an HTML file
const downloadPage = async () => {
const title = document.title.replace(/[^a-zA-Z0-9]/g, '_'); // Sanitize the title
const timestamp = new Date().toISOString().split('T')[0]; // Human-readable date (YYYY-MM-DD)
const filename = `${title}_${timestamp}.html`;
await replaceResourceURLs();
const doctype = new XMLSerializer().serializeToString(document.doctype);
const htmlContent = document.documentElement.outerHTML;
const blob = new Blob([doctype + htmlContent], { type: 'text/html' });
// Create a link and trigger the download
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
// Clean up
document.body.removeChild(link);
};
// Add the event listener to the button
button.addEventListener('click', downloadPage);
})();