-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
110 lines (93 loc) · 3.53 KB
/
script.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
document.querySelector('[name="show-import"]').addEventListener('click', showImport);
document.querySelector('[name="import-button"]').addEventListener('click', importPrevious);
document.querySelector('form').addEventListener('submit', generate);
document.querySelector('.copy-button').addEventListener('click', copyDataURI);
generate();
function showImport() {
document.querySelector('.import-container--collapsed').classList.remove('import-container--collapsed');
}
function importPrevious() {
const importField = document.querySelector('[name="import"]');
const dataURI = importField.value;
const base64Html = dataURI.replace(/^data:text\/html;[^,]*base64,/, '');
document.querySelector('[name="site-content"]').value = removeWatermark(atob(base64Html));
generate();
importField.value = '';
}
function removeWatermark(content) {
return content.replace(/<a href="https:\/\/mattiasw\.github\.io\/self-contained\/"[^>]+>[^<]+<\/a><\/body>/, '</body>');
}
async function generate(event) {
event?.preventDefault();
const content = addWatermark(document.querySelector('[name="site-content"]').value);
const shouldMinify = document.querySelector('[name="minify"]').checked;
const html = shouldMinify ? await minify(content) : content;
const dataURI = 'data:text/html;charset=utf-8;base64,' + btoa(html);
const encodedUri = encodeURI(dataURI);
const output = document.querySelector('[name="data-uri"]');
output.value = encodedUri;
if (event) {
output.focus();
selectOutput();
}
document.querySelector('.copy-button').disabled = false;
const preview = document.querySelector('.preview');
preview.src = encodedUri;
updateSize(output.value.length);
}
function addWatermark(content) {
const htmlPattern = /<\/body>[\n\r\s]*<\/html>[\n\r\s]*$/;
if (htmlPattern.test(content)) {
const wrapperStyles = [
'background: rgba(20, 90, 180, .35);',
'bottom: 0;',
'display: block;',
'font-size: .8rem;',
'left: 0;',
'opacity: .5;',
'padding: 4px 8px;',
'position: fixed;',
].join('');
return content.replace(
htmlPattern,
`<a href="https://mattiasw.github.io/self-contained/" target="_blank" style="${wrapperStyles}">Created with Web page data URI generator.</a></body>\n</html>`,
);
}
return content;
}
async function minify(content) {
const HtmlMinifier = await import('html-minifier-terser/dist/htmlminifier.cjs');
return HtmlMinifier.minify(content, {
minifyCSS: true,
minifyJS: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
});
}
function selectOutput() {
const output = document.querySelector('[name="data-uri"]');
output.select();
setTimeout(function () {
output.scrollTop = 0;
}, 10);
}
function updateSize(length) {
const size = document.querySelector('.size');
if (length < 1024) {
size.textContent = `${length} byte${length === 1 ? '' : 's'}`;
} else if (length < 1024 * 1024) {
size.textContent = `${(length / 1024).toFixed(1)} KiB`;
} else {
size.textContent = `${(length / 1024 / 1024).toFixed(1)} MiB`;
}
}
function copyDataURI(event) {
event.preventDefault();
selectOutput();
document.execCommand('copy');
this.focus();
}