Skip to content

Instantly share code, notes, and snippets.

@lucasburlingham
Last active November 27, 2022 00:40
Show Gist options
  • Save lucasburlingham/323f6343bc21050f8e73914bb7574bc8 to your computer and use it in GitHub Desktop.
Save lucasburlingham/323f6343bc21050f8e73914bb7574bc8 to your computer and use it in GitHub Desktop.
In-tab notepad with storage
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"
integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V"
crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body contenteditable="true">
</body>
<script>
if (localStorage.getItem("TabNotes")) {
var data = localStorage.getItem("TabNotes");
console.info("Loaded data from localStorage");
document.querySelector("body").innerHTML = data;
} else {
var data = "<br>Start typing!";
document.querySelector("body").innerHTML = data;
}
document.querySelector("body").addEventListener("input", function () {
data = document.querySelector("body").innerHTML;
localStorage.setItem("TabNotes", data);
if (data.includes("/reset") || data.includes("/clear")) {
resetNotepad();
}
if (data.includes("/save")) {
save();
}
if (data.includes("/print")) {
data.replace("/print", "");
window.print();
}
//if (data.includes("/help")) {
// showHelp();
//}
});
window.addEventListener('beforeunload', function (e) {
localStorage.clear();
localStorage.setItem("TabNotes", data);
});
// Save
var isCtrl = false;
window.addEventListener('load', function () {
document.body.addEventListener('keydown', function (e) {
if (e.keyCode == 17) {
e.preventDefault();
isCtrl = true;
}
if (e.keyCode == 83 && isCtrl) {
e.preventDefault();
data.replace("/save", "");
data.replace("/reset", "");
data.replace("/print", "");
data.replace("/clear", "");
// data.replace("/help", "");
save(data, type, name);
}
});
document.body.addEventListener('keyup', function (e) {
e.preventDefault();
isCtrl = false;
});
});
var type = "text/html";
var name = "index.html";
function save(data, type, name) {
var blob = new Blob([data], { type });
var url = window.URL.createObjectURL(blob);
downloadURI(url, name);
window.URL.revokeObjectURL(url);
}
function downloadURI(uri, name) {
var link = document.createElement("a");
console.info("Downloading HTML content");
link.download = name;
link.href = uri;
link.click();
}
function resetNotepad() {
console.info("Resetting notepad");
localStorage.clear();
document.body.innerHTML = "";
}
</script>
<style id="style">
body {
padding-top: 10px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
body:first-line {
font-weight: bolder;
height: 2rem;
}
body::before {
content: "Use / reset to reset the notepad";
padding: 1px;
}
</style>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment