Merge pull request #54 from manto89/feature/already-visited

Check if the current page is already saved in trilium
This commit is contained in:
zadam 2023-07-09 23:02:09 +02:00 committed by GitHub
commit 526eaa2749
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 0 deletions

View File

@ -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);
}
});

View File

@ -10,6 +10,7 @@
<body>
<div style="display: flex; justify-content: space-between; vertical-align: middle;">
<h3>Trilium Web Clipper</h3>
<div style="position: relative; top: 6px;">
<button class="button" id="show-options-button">Options</button>
@ -17,6 +18,7 @@
<button class="button" id="show-help-button">Help</button>
</div>
</div>
<div id="already-visited"></div>
<button class="button full needs-connection" id="save-cropped-screenshot-button">Crop screen shot</button>
<button class="button full needs-connection" id="save-whole-screenshot-button">Save whole screen shot</button>
@ -48,6 +50,7 @@
<script src="../lib/cash.min.js"></script>
<script src="popup.js"></script>
<script src="../utils.js"></script>
<script src="../content.js"></script>
</body>
</html>

View File

@ -113,6 +113,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') {
@ -145,12 +146,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");

View File

@ -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 = () => {