diff --git a/main b/main index ba08cd2..b54d7d5 100644 --- a/main +++ b/main @@ -214,6 +214,33 @@ def apilistnotes(): datatemplate.append(notetemplate) return datatemplate, 200 + +@app.route("/api/exportnotes", methods=("GET", "POST")) +def apiexportnotes(): + if request.method == "POST": + data = request.get_json() + secretKey = data["secretKey"] + + userCookie = get_session(secretKey) + user = get_user(userCookie["id"]) + + conn = get_db_connection() + notes = conn.execute("SELECT * FROM notes WHERE creator = ? ORDER BY id DESC;", (user["id"],)).fetchall() + conn.close() + + datatemplate = [] + + for note in notes: + notetemplate = { + "id": note["id"], + "created": note["created"], + "edited": note["edited"], + "title": note["title"], + "content": note["content"] + } + datatemplate.append(notetemplate) + + return datatemplate, 200 @app.route("/api/newnote", methods=("GET", "POST")) def apinewnote(): diff --git a/static/css/style.css b/static/css/style.css index 2f1a795..e0e0b5a 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -188,6 +188,7 @@ body { padding: 10px; color: black; border-radius: 8px; + min-width: 338.5px } .optionsDiv button { diff --git a/static/js/main.js b/static/js/main.js index f6cb585..7585ed1 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -29,6 +29,7 @@ let noteBox = document.getElementById("noteBox") let loadingStuff = document.getElementById("loadingStuff") let burgerDropdown = document.getElementById("burgerDropdown") let burgerButton = document.getElementById("burgerButton") +let exportNotesButton = document.getElementById("exportNotesButton") for (let i = 0; i < 40; i++) { notesDiv.appendChild(loadingStuff.cloneNode()) @@ -305,4 +306,54 @@ burgerButton.addEventListener("click", (event) => { burgerDropdown.style.left = String(event.clientX) + "px" burgerDropdown.style.top = String(event.clientY) + "px" +}); +function downloadObjectAsJson(exportObj, exportName) { + var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj)); + var downloadAnchorNode = document.createElement("a"); + downloadAnchorNode.setAttribute("href", dataStr); + downloadAnchorNode.setAttribute("download", exportName + ".json"); + document.body.appendChild(downloadAnchorNode); + downloadAnchorNode.click(); + downloadAnchorNode.remove(); +} + +function exportNotes() { + let noteExport = [] + fetch("/api/exportnotes", { + method: "POST", + body: JSON.stringify({ + secretKey: secretkey + }), + headers: { + "Content-type": "application/json; charset=UTF-8" + } + }) + .then((response) => response) + .then((response) => { + async function doStuff() { + let responseData = await response.json() + for (let i in responseData) { + let bytes = CryptoJS.AES.decrypt(responseData[i]["title"], password); + let originalTitle = bytes.toString(CryptoJS.enc.Utf8); + + responseData[i]["title"] = originalTitle + + let bytesd = CryptoJS.AES.decrypt(responseData[i]["content"], password); + let originalContent = bytesd.toString(CryptoJS.enc.Utf8); + + responseData[i]["content"] = originalContent + } + let jsonString = JSON.parse(JSON.stringify(responseData)) + console.log(jsonString) + + downloadObjectAsJson(jsonString, "data") + } + doStuff() + }) +} + +exportNotesButton.addEventListener("click", (event) => { + exportNotesButton.innerText = "exporting.." + exportNotes() + exportNotesButton.innerText = "export notes" }); \ No newline at end of file diff --git a/templates/app.html b/templates/app.html index ad165cb..8345460 100644 --- a/templates/app.html +++ b/templates/app.html @@ -39,6 +39,7 @@
+