diff --git a/background.js b/background.js index ade9e9476..4074987ab 100644 --- a/background.js +++ b/background.js @@ -444,4 +444,8 @@ browser.runtime.onMessage.addListener(async request => { else if (request.name === 'send-trilium-search-status') { triliumServerFacade.sendTriliumSearchStatusToPopup(); } + else if (request.name === 'trigger-trilium-search-note-url') { + const activeTab = await getActiveTab(); + triliumServerFacade.triggerSearchNoteByUrl(activeTab.url); + } }); diff --git a/popup/popup.html b/popup/popup.html index ceca59820..4e2109f1e 100644 --- a/popup/popup.html +++ b/popup/popup.html @@ -10,6 +10,7 @@

Trilium Web Clipper

+
@@ -17,6 +18,7 @@
+
@@ -44,6 +46,7 @@ + diff --git a/popup/popup.js b/popup/popup.js index 13bf179d2..f360323dd 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -108,6 +108,7 @@ function escapeHtml(string) { const $connectionStatus = $("#connection-status"); const $needsConnection = $(".needs-connection"); +const $alreadyVisited = $("#already-visited"); browser.runtime.onMessage.addListener(request => { if (request.name === 'trilium-search-status') { @@ -140,12 +141,27 @@ browser.runtime.onMessage.addListener(request => { if (isConnected) { $needsConnection.removeAttr("disabled"); $needsConnection.removeAttr("title"); + browser.runtime.sendMessage({name: "trigger-trilium-search-note-url"}); } else { $needsConnection.attr("disabled", "disabled"); $needsConnection.attr("title", "This action can't be performed without active connection to Trilium."); } } + else if (request.name == "trilium-previously-visited"){ + const {searchNote} = request; + if (searchNote.status === 'found'){ + const a = createLink({name: 'openNoteInTrilium', noteId: searchNote.noteId}, + "Open in Trilium.") + noteFound = `Already visited website!`; + $alreadyVisited.html(noteFound); + $alreadyVisited[0].appendChild(a); + }else{ + $alreadyVisited.html(''); + } + + + } }); const $checkConnectionButton = $("#check-connection-button"); diff --git a/trilium_server_facade.js b/trilium_server_facade.js index 8b82f1d53..6f46893e5 100644 --- a/trilium_server_facade.js +++ b/trilium_server_facade.js @@ -23,6 +23,21 @@ class TriliumServerFacade { } catch (e) {} // nothing might be listening } + async sendTriliumSearchNoteToPopup(){ + try{ + await browser.runtime.sendMessage({ + name: "trilium-previously-visited", + searchNote: this.triliumSearchNote + }) + + } + catch (e) {} // nothing might be listening + } + + setTriliumSearchNote(st){ + this.triliumSearchNote = st; + this.sendTriliumSearchNoteToPopup(); + } setTriliumSearch(ts) { this.triliumSearch = ts; @@ -116,6 +131,18 @@ class TriliumServerFacade { this.setTriliumSearch({ status: 'not-found' }); } + async triggerSearchNoteByUrl(noteUrl) { + const resp = await triliumServerFacade.callService('GET', 'notes-by-url/' + encodeURIComponent(noteUrl)) + let newStatus = { + status: 'not-found', + noteId: null + } + if (resp && resp.noteId) { + newStatus.noteId = resp.noteId; + newStatus.status = 'found'; + } + this.setTriliumSearchNote(newStatus); + } async waitForTriliumSearch() { return new Promise((res, rej) => { const checkStatus = () => {