From 8394ce800270d5b843fcc4cea22319420c9c8dff Mon Sep 17 00:00:00 2001 From: manto89 Date: Wed, 21 Jun 2023 13:09:49 +0200 Subject: [PATCH 1/5] Change method when adding a new note. Add reference to new backend method --- src/routes/api/clipper.js | 30 +++++++++++++++++++++++++----- src/routes/routes.js | 1 + 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index d792ff38a..6cf03a519 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -1,6 +1,7 @@ "use strict"; const attributeService = require("../../services/attributes"); +const cloneService = require("../../services/cloning"); const noteService = require('../../services/notes'); const dateNoteService = require('../../services/date_notes'); const dateUtils = require('../../services/date_utils'); @@ -24,7 +25,7 @@ function findClippingNote(clipperInboxNote, pageUrl) { ); for (const note of notes) { - if (note.getOwnedLabelValue('clipType') === 'clippings') { + if (note.getOwnedLabelValue('clipType') === 'note') { return note; } } @@ -43,16 +44,20 @@ function getClipperInboxNote() { } function addClipping(req) { + //if a note under the clipperInbox as the same 'pageUrl' attribute, add the content to that note + //and clone it under today's inbox + //otherwise just create a new note under today's inbox let {title, content, pageUrl, images} = req.body; const clipperInbox = getClipperInboxNote(); pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); let clippingNote = findClippingNote(clipperInbox, pageUrl); - + let dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate()); + if (!clippingNote) { clippingNote = noteService.createNewNote({ - parentNoteId: clipperInbox.noteId, + parentNoteId: dailyNote.noteId, title: title, content: '', type: 'text' @@ -63,12 +68,16 @@ function addClipping(req) { clippingNote.setLabel('iconClass', 'bx bx-globe'); } + const rewrittenContent = processContent(images, clippingNote, content); const existingContent = clippingNote.getContent(); clippingNote.setContent(`${existingContent}${existingContent.trim() ? "
" : ""}${rewrittenContent}`); - + + if (clippingNote.parentNoteId != dailyNote.noteId){ + cloneService.cloneNoteToParentNote(clippingNote.noteId, dailyNote.noteId); + } return { noteId: clippingNote.noteId }; @@ -187,9 +196,20 @@ function handshake() { } } +function findNotesByUrl(req){ + let pageUrl = req.params.noteUrl; + const clipperInbox = getClipperInboxNote(); + let foundPage = findClippingNote(clipperInbox, pageUrl); + return { + noteId: foundPage ? foundPage.noteId : null + } + +} + module.exports = { createNote, addClipping, openNote, - handshake + handshake, + findNotesByUrl }; diff --git a/src/routes/routes.js b/src/routes/routes.js index 2989208b5..764b2746c 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -286,6 +286,7 @@ function register(app) { route(POST, '/api/clipper/clippings', clipperMiddleware, clipperRoute.addClipping, apiResultHandler); route(POST, '/api/clipper/notes', clipperMiddleware, clipperRoute.createNote, apiResultHandler); route(POST, '/api/clipper/open/:noteId', clipperMiddleware, clipperRoute.openNote, apiResultHandler); + route(GET, '/api/clipper/notes-by-url/:noteUrl', clipperMiddleware, clipperRoute.findNotesByUrl, apiResultHandler); apiRoute(GET, '/api/similar-notes/:noteId', similarNotesRoute.getSimilarNotes); From 54065672aac470e99bd464e3b24107a3ba175003 Mon Sep 17 00:00:00 2001 From: manto89 Date: Wed, 21 Jun 2023 16:10:06 +0200 Subject: [PATCH 2/5] Normalize behaviour with the other create method for web-clipper --- src/routes/api/clipper.js | 42 ++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index 6cf03a519..310735985 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -37,7 +37,7 @@ function getClipperInboxNote() { let clipperInbox = attributeService.getNoteWithLabel('clipperInbox'); if (!clipperInbox) { - clipperInbox = dateNoteService.getDayNote(dateUtils.localNowDate()); + clipperInbox = dateNoteService.getRootCalendarNote(); } return clipperInbox; @@ -49,11 +49,12 @@ function addClipping(req) { //otherwise just create a new note under today's inbox let {title, content, pageUrl, images} = req.body; + //this is only for reference const clipperInbox = getClipperInboxNote(); + const dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate()); pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); let clippingNote = findClippingNote(clipperInbox, pageUrl); - let dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate()); if (!clippingNote) { clippingNote = noteService.createNewNote({ @@ -91,24 +92,31 @@ function createNote(req) { } const clipperInbox = getClipperInboxNote(); + const dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate()); + pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); + let note = findClippingNote(clipperInbox, pageUrl); - const {note} = noteService.createNewNote({ - parentNoteId: clipperInbox.noteId, - title, - content, - type: 'text' - }); + if (!note){ + note = noteService.createNewNote({ + parentNoteId: dailyNote.noteId, + title, + content, + type: 'text' + }).note; + clipType = htmlSanitizer.sanitize(clipType); - clipType = htmlSanitizer.sanitize(clipType); + note.setLabel('clipType', clipType); + if (pageUrl) { + pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); - note.setLabel('clipType', clipType); + note.setLabel('pageUrl', pageUrl); + note.setLabel('iconClass', 'bx bx-globe'); + } - if (pageUrl) { - pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); - - note.setLabel('pageUrl', pageUrl); - note.setLabel('iconClass', 'bx bx-globe'); } + + + if (labels) { for (const labelName in labels) { @@ -117,9 +125,11 @@ function createNote(req) { } } + const existingContent = note.getContent(); const rewrittenContent = processContent(images, note, content); + note.setContent(`${existingContent}${existingContent.trim() ? "
" : ""}${rewrittenContent}`); - note.setContent(rewrittenContent); + // note.setContent(rewrittenContent); return { noteId: note.noteId From ebccd48013b20a7d72486b46a479d49c5c5f2d32 Mon Sep 17 00:00:00 2001 From: manto89 Date: Wed, 21 Jun 2023 18:05:41 +0200 Subject: [PATCH 3/5] Don't search note by url if url begins with 'about:' --- src/routes/api/clipper.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index 310735985..636d94e21 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -16,17 +16,20 @@ const htmlSanitizer = require('../../services/html_sanitizer'); const {formatAttrForSearch} = require("../../services/attribute_formatter"); function findClippingNote(clipperInboxNote, pageUrl) { - const notes = clipperInboxNote.searchNotesInSubtree( - formatAttrForSearch({ - type: 'label', - name: "pageUrl", - value: pageUrl - }, true) - ); + //Avoid searching for empty of browser pages like about:blank + if (pageUrl.trim().length > 1 && pageUrl.trim().indexOf('about:') != 0 ){ + const notes = clipperInboxNote.searchNotesInSubtree( + formatAttrForSearch({ + type: 'label', + name: "pageUrl", + value: pageUrl + }, true) + ); - for (const note of notes) { - if (note.getOwnedLabelValue('clipType') === 'note') { - return note; + for (const note of notes) { + if (note.getOwnedLabelValue('clipType') === 'note') { + return note; + } } } From 0c86dece5fcdd449601b513c6dd2d0f47bacfbbf Mon Sep 17 00:00:00 2001 From: manto89 Date: Sun, 2 Jul 2023 12:51:23 +0200 Subject: [PATCH 4/5] Fix clipType searching for web-clipper --- src/routes/api/clipper.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index 636d94e21..1735d0eba 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -15,7 +15,7 @@ const BAttribute = require('../../becca/entities/battribute'); const htmlSanitizer = require('../../services/html_sanitizer'); const {formatAttrForSearch} = require("../../services/attribute_formatter"); -function findClippingNote(clipperInboxNote, pageUrl) { +function findClippingNote(clipperInboxNote, pageUrl, clipType) { //Avoid searching for empty of browser pages like about:blank if (pageUrl.trim().length > 1 && pageUrl.trim().indexOf('about:') != 0 ){ const notes = clipperInboxNote.searchNotesInSubtree( @@ -27,7 +27,12 @@ function findClippingNote(clipperInboxNote, pageUrl) { ); for (const note of notes) { - if (note.getOwnedLabelValue('clipType') === 'note') { + if (clipType){ + if (note.getOwnedLabelValue('clipType') === clipType) { + return note; + } + } + else{ return note; } } @@ -51,13 +56,14 @@ function addClipping(req) { //and clone it under today's inbox //otherwise just create a new note under today's inbox let {title, content, pageUrl, images} = req.body; + const clipType = 'clippings'; //this is only for reference const clipperInbox = getClipperInboxNote(); const dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate()); pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); - let clippingNote = findClippingNote(clipperInbox, pageUrl); + let clippingNote = findClippingNote(clipperInbox, pageUrl, clipType); if (!clippingNote) { clippingNote = noteService.createNewNote({ @@ -94,19 +100,21 @@ function createNote(req) { title = `Clipped note from ${pageUrl}`; } + + clipType = htmlSanitizer.sanitize(clipType); + const clipperInbox = getClipperInboxNote(); const dailyNote = dateNoteService.getDayNote(dateUtils.localNowDate()); pageUrl = htmlSanitizer.sanitizeUrl(pageUrl); - let note = findClippingNote(clipperInbox, pageUrl); + let note = findClippingNote(clipperInbox, pageUrl, clipType); if (!note){ note = noteService.createNewNote({ parentNoteId: dailyNote.noteId, title, - content, + content: '', type: 'text' }).note; - clipType = htmlSanitizer.sanitize(clipType); note.setLabel('clipType', clipType); if (pageUrl) { @@ -212,7 +220,7 @@ function handshake() { function findNotesByUrl(req){ let pageUrl = req.params.noteUrl; const clipperInbox = getClipperInboxNote(); - let foundPage = findClippingNote(clipperInbox, pageUrl); + let foundPage = findClippingNote(clipperInbox, pageUrl, null); return { noteId: foundPage ? foundPage.noteId : null } From bb6ab0fe45413bf4d64e01a809176b3684847102 Mon Sep 17 00:00:00 2001 From: manto89 Date: Sun, 2 Jul 2023 12:52:16 +0200 Subject: [PATCH 5/5] Fix clipping html with parser when using web-clipper --- src/routes/api/clipper.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index 1735d0eba..bcab106ba 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -14,6 +14,8 @@ const path = require('path'); const BAttribute = require('../../becca/entities/battribute'); const htmlSanitizer = require('../../services/html_sanitizer'); const {formatAttrForSearch} = require("../../services/attribute_formatter"); +const jsdom = require("jsdom"); +const { JSDOM } = jsdom; function findClippingNote(clipperInboxNote, pageUrl, clipType) { //Avoid searching for empty of browser pages like about:blank @@ -83,7 +85,7 @@ function addClipping(req) { const existingContent = clippingNote.getContent(); - clippingNote.setContent(`${existingContent}${existingContent.trim() ? "
" : ""}${rewrittenContent}`); + clippingNote.setContent(`${existingContent}${existingContent.trim() ? "
" : ""}${rewrittenContent}`); if (clippingNote.parentNoteId != dailyNote.noteId){ cloneService.cloneNoteToParentNote(clippingNote.noteId, dailyNote.noteId); @@ -188,6 +190,15 @@ function processContent(images, note, content) { // fallback if parsing/downloading images fails for some reason on the extension side ( rewrittenContent = noteService.downloadImages(note.noteId, rewrittenContent); + // Check if rewrittenContent contains at least one HTML tag + if (!/<.+?>/.test(rewrittenContent)) { + rewrittenContent = '

'+rewrittenContent + '

'; + } + // Create a JSDOM object from the existing HTML content + let dom = new JSDOM(rewrittenContent); + + // Get the content inside the body tag and serialize it + rewrittenContent = dom.window.document.body.innerHTML; return rewrittenContent; }