Cybrkyd's Git Repositories

IndexedDB-notes - commit: f834bcc

commit f834bcc653bb6aeaefbe88fbb0c60d395f49bf08d5677718ac52da62f9d5b196
author cybrkyd <git@cybrkyd.com> 2026-05-10 17:05:51 +0100
committer cybrkyd <git@cybrkyd.com> 2026-05-10 17:05:51 +0100

Commit Message

importDatabase function

📊 Diffstat

idb-notes.html 49
1 files changed, 49 insertions(+), 0 deletions(-)

Diff

diff --git a/idb-notes.html b/idb-notes.html
index 65e11ad..ee42626 100644
--- a/idb-notes.html
+++ b/idb-notes.html
@@ -80,6 +80,9 @@
<button id="newBtn">New Document</button>
<button id="exportBtn">Export as TXT</button>
<button id="exportDbBtn">Export NotesDB</button>
+ <button id="importDbBtn">Import NotesDB</button>
+
+ <input type="file" id="importFile" accept=".json" style="display:none;">
<div class="docs">
<h2>Saved Documents</h2>
@@ -274,6 +277,40 @@ function exportDatabase() {
};
}
+ function importDatabase(file) {
+ const reader = new FileReader();
+
+ reader.onload = function(event) {
+ try {
+ const data = JSON.parse(event.target.result);
+
+ if (!Array.isArray(data)) {
+ alert("Invalid backup file");
+ return;
+ }
+
+ const tx = db.transaction("documents", "readwrite");
+ const store = tx.objectStore("documents");
+
+ store.clear();
+
+ data.forEach(doc => {
+ store.put(doc);
+ });
+
+ tx.oncomplete = function() {
+ loadDocuments();
+ alert("Import complete");
+ };
+
+ } catch (err) {
+ alert("Failed to import JSON");
+ }
+ };
+
+ reader.readAsText(file);
+ }
+
function escapeHtml(text) {
const div = document.createElement("div");
div.textContent = text;
@@ -302,6 +339,18 @@ document.getElementById("exportBtn").onclick = function() {
document.getElementById("exportDbBtn").onclick = function() {
exportDatabase();
};
+
+ document.getElementById("importDbBtn").onclick = function() {
+ document.getElementById("importFile").click();
+ };
+
+ document.getElementById("importFile").onchange = function(event) {
+ const file = event.target.files[0];
+
+ if (file) {
+ importDatabase(file);
+ }
+ };
</script>
</body>