Skip to content

Instantly share code, notes, and snippets.

@aont
Last active October 17, 2024 01:04
Show Gist options
  • Save aont/6ed494fe0a502cbce44f2f1ba5061c99 to your computer and use it in GitHub Desktop.
Save aont/6ed494fe0a502cbce44f2f1ba5061c99 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS Minify</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundle.min.js"></script>
</head>
<body>
<script>
window.addEventListener('load', (event) => {
const textarea = document.createElement("textarea");
textarea.style.width = "100%";
function rctAutoExpand(shrink=false) {
if(shrink) {
textarea.style.height = "auto";
rctHeightSave = textarea.scrollHeight;
} else {
let rctHeightSave = textarea.computedStyleMap().get("height").value;
if (rctHeightSave == "auto") {
rctHeightSave = textarea.scrollHeight;
}
}
console.log({rctHeightSave});
textarea.style.height = "auto";
const rctScrollHeight = textarea.scrollHeight
console.log({rctScrollHeight});
textarea.style.height = rctHeightSave + "px";
if(rctScrollHeight>rctHeightSave) {
textarea.style.height = rctScrollHeight.toString() + "px";
}
}
textarea.addEventListener("input", (event)=>{
console.log("input")
rctAutoExpand(true);
})
document.body.appendChild(textarea);
document.body.appendChild(document.createElement("br"));
const buttonMinify = document.createElement("button");
buttonMinify.innerText = "minify";
buttonMinify.addEventListener("click", function(ev) {
(async function(ev) {
try {
statusArea.innerText = "";
let inputText;
if (textarea.value) {
console.log(textarea.value);
inputText = textarea.value;
} else {
inputText = await navigator.clipboard.readText();
console.log(inputText);
}
const result = await Terser.minify(inputText, {mangle: false, compress: true});
console.log(result.code);
textarea.value = result.code;
rctAutoExpand(true);
} catch(e) {
statusArea.innerText = e.toString();
}
})(ev);
});
document.body.appendChild(buttonMinify);
const statusArea = document.createElement("div")
document.body.appendChild(statusArea);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment