diff --git a/webresearcher/export.js b/webresearcher/export.js index bda9f77..89dae1a 100644 --- a/webresearcher/export.js +++ b/webresearcher/export.js @@ -12,13 +12,25 @@ document.getElementById('exportNotesJoplin').addEventListener('click', ()=>{ } console.log("User asked to export notes to Joplin."); - console.log( exportHTML.join('')); + // console.log( exportHTML.join('')); - fetch("http://localhost:41184/notes?token=" + joplinToken, + async function fetchJson(url) { + var results = await fetch(url); + var resultsJSON = await results.json(); + if(resultsJSON.items.length == 0){ + // pack results into a dictionary + let foo_final ={}; + foo_final['HTML'] = WBJS_HTML; + foo_final['JSON'] = WBJS_JSON; + foo_final['CSS'] = WBJS_CSS; + let metaDataBlock = ``; + + // create note in Joplin + fetch("http://localhost:41184/notes?token=" + joplinToken, { body: JSON.stringify({ "title": document.title , - "body_html": exportHTML.join(''), + "body": exportHTML.join('') + metaDataBlock, "tags" : "WBJS", "source_url": url_window, }), @@ -27,7 +39,42 @@ document.getElementById('exportNotesJoplin').addEventListener('click', ()=>{ "Content-Type": "application/json", }, } - ); - $.notify('Adding notes to Joplin', "success"); + ); + $.notify('Adding notes to Joplin', "success"); + } + + if(resultsJSON.items.length == 1){ + // pack results into a dictionary + let foo_final ={}; + foo_final['HTML'] = WBJS_HTML; + foo_final['JSON'] = WBJS_JSON; + foo_final['CSS'] = WBJS_CSS; + let metaDataBlock = ``; + + $.notify('Overwriting existing note', "info"); + fetch("http://localhost:41184/notes/"+resultsJSON.items[0].id+"?token=" + joplinToken, + { + body: JSON.stringify({ + "body": exportHTML.join('') + metaDataBlock, + }), + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + } + ); + $.notify('Adding notes to Joplin', "success"); + + } + if(resultsJSON.items.length>1){ + $.notify('Error: Multiple notes with same title found. Cannot save to Joplin. Delete and retry.', "error"); + } + + } + // Check if a note already exists + // If yes, then use the exisiting note ID + // If no, then create a new note + + fetchJson("http://localhost:41184/search?token=" + joplinToken + "&query=" + pageTitle); }); \ No newline at end of file diff --git a/webresearcher/init.js b/webresearcher/init.js index 3ed8b9e..a4e0aab 100644 --- a/webresearcher/init.js +++ b/webresearcher/init.js @@ -31,4 +31,5 @@ var htmlAppend = $("html").append(` `); -$("#userButtonPanelWBJS").draggable(); \ No newline at end of file +$("#userButtonPanelWBJS").draggable(); + diff --git a/webresearcher/loadLocalStorage.js b/webresearcher/loadLocalStorage.js index 9d3a7f5..3768458 100644 --- a/webresearcher/loadLocalStorage.js +++ b/webresearcher/loadLocalStorage.js @@ -1,8 +1,10 @@ // Check if there are any notes already in localstorage and if so load them up +$.notify("WBJS is initializing.", "info",{autoHideDelay: 30000}); if(localStorage.getItem(webPageUrl)!=null){ var foo_loaded = JSON.parse(localStorage.getItem(webPageUrl)); var foo_loaded_keys = Object.keys(foo_loaded['HTML']); + for(k=0;k