"save note" is now "save link with note" - i.e. we're saving a current URL together with the text note

This commit is contained in:
zadam 2021-03-31 22:23:43 +02:00
parent 1c75ea89ad
commit 0811276fbb
6 changed files with 64 additions and 55 deletions

View File

@ -237,18 +237,25 @@ async function saveWholePage() {
toast("Page has been saved to Trilium.", resp.noteId); toast("Page has been saved to Trilium.", resp.noteId);
} }
async function saveNote(title, content) { async function saveLinkWithNote(title, content) {
const activeTab = await getActiveTab();
if (!title.trim()) {
title = activeTab.title;
}
const resp = await triliumServerFacade.callService('POST', 'notes', { const resp = await triliumServerFacade.callService('POST', 'notes', {
title: title, title: title,
content: content, content: content,
clipType: 'note' clipType: 'note',
pageUrl: activeTab.url
}); });
if (!resp) { if (!resp) {
return false; return false;
} }
toast("Note has been saved to Trilium.", resp.noteId); toast("Link with note has been saved to Trilium.", resp.noteId);
return true; return true;
} }
@ -330,8 +337,8 @@ browser.runtime.onMessage.addListener(async request => {
else if (request.name === 'save-whole-page') { else if (request.name === 'save-whole-page') {
return await saveWholePage(); return await saveWholePage();
} }
else if (request.name === 'save-note') { else if (request.name === 'save-link-with-note') {
return await saveNote(request.title, request.content); return await saveLinkWithNote(request.title, request.content);
} }
else if (request.name === 'trigger-trilium-search') { else if (request.name === 'trigger-trilium-search') {
triliumServerFacade.triggerSearchForTrilium(); triliumServerFacade.triggerSearchForTrilium();

View File

@ -23,24 +23,6 @@ function pageTitle() {
return titleElements.length ? titleElements[0].text.trim() : document.title.trim(); return titleElements.length ? titleElements[0].text.trim() : document.title.trim();
} }
function getPageLocationOrigin() {
// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)
// but for file:// protocol this is browser dependant and in particular Firefox returns "null" in this case.
return location.protocol === 'file:' ? 'file://' : location.origin;
}
function getBaseUrl() {
let output = getPageLocationOrigin() + location.pathname;
if (output[output.length - 1] !== '/') {
output = output.split('/');
output.pop();
output = output.join('/');
}
return output;
}
function getReadableDocument() { function getReadableDocument() {
// Readability directly change the passed document so clone it so as // Readability directly change the passed document so clone it so as
// to preserve the original web page. // to preserve the original web page.

View File

@ -24,11 +24,11 @@ body {
width: 100%; width: 100%;
} }
#create-text-note-wrapper { #save-link-with-note-wrapper {
display: none; display: none;
} }
#create-text-note-textarea { #save-link-with-note-textarea {
width: 100%; width: 100%;
} }

View File

@ -20,10 +20,10 @@
<button class="button full needs-connection" id="clip-screenshot-button">Clip screenshot</button> <button class="button full needs-connection" id="clip-screenshot-button">Clip screenshot</button>
<button class="button full needs-connection" id="save-whole-page-button">Save whole page</button> <button class="button full needs-connection" id="save-whole-page-button">Save whole page</button>
<button class="button full needs-connection" id="create-text-note-button">Create text note</button> <button class="button full needs-connection" id="save-link-with-note-button">Save link with a note</button>
<div id="create-text-note-wrapper"> <div id="save-link-with-note-wrapper">
<textarea id="create-text-note-textarea" rows="5"></textarea> <textarea id="save-link-with-note-textarea" rows="5"></textarea>
<div style="display: flex;"> <div style="display: flex;">
<button type="submit" class="button wide" id="save-button">Save</button> <button type="submit" class="button wide" id="save-button">Save</button>
@ -41,6 +41,7 @@
<script src="../lib/browser-polyfill.js"></script> <script src="../lib/browser-polyfill.js"></script>
<script src="../lib/cash.min.js"></script> <script src="../lib/cash.min.js"></script>
<script src="popup.js"></script> <script src="popup.js"></script>
<script src="../utils.js"></script>
</body> </body>
</html> </html>

View File

@ -19,42 +19,42 @@ $clipScreenShotButton.on("click", () => sendMessage({name: 'save-screenshot'}));
$saveWholePageButton.on("click", () => sendMessage({name: 'save-whole-page'})); $saveWholePageButton.on("click", () => sendMessage({name: 'save-whole-page'}));
const $createTextNoteWrapper = $("#create-text-note-wrapper"); const $saveLinkWithNoteWrapper = $("#save-link-with-note-wrapper");
const $textNote = $("#create-text-note-textarea"); const $textNote = $("#save-link-with-note-textarea");
$textNote.on('keypress', function (event) { $textNote.on('keypress', function (event) {
if (event.which === 10 || event.which === 13 && event.ctrlKey) { if ((event.which === 10 || event.which === 13) && event.ctrlKey) {
saveNote(); saveLinkWithNote();
return false; return false;
} }
return true; return true;
}); });
$("#create-text-note-button").on("click", () => { $("#save-link-with-note-button").on("click", () => {
$createTextNoteWrapper.show(); $saveLinkWithNoteWrapper.show();
$textNote[0].focus(); $textNote[0].focus();
}); });
$("#cancel-button").on("click", () => { $("#cancel-button").on("click", () => {
$createTextNoteWrapper.hide(); $saveLinkWithNoteWrapper.hide();
$textNote.val(""); $textNote.val("");
window.close(); window.close();
}); });
async function saveNote() { async function saveLinkWithNote() {
const textNoteVal = $textNote.val().trim(); const textNoteVal = $textNote.val().trim();
if (textNoteVal.length === 0) {
alert("Note is empty. Please enter some text");
return;
}
const match = /^(.*?)([.?!]\s|\n)/.exec(textNoteVal);
let title, content; let title, content;
if (!textNoteVal) {
title = '';
content = '';
}
else {
const match = /^(.*?)([.?!]\s|\n)/.exec(textNoteVal);
if (match) { if (match) {
title = match[0].trim(); title = match[0].trim();
content = textNoteVal.substr(title.length).trim(); content = textNoteVal.substr(title.length).trim();
@ -63,10 +63,11 @@ async function saveNote() {
title = textNoteVal; title = textNoteVal;
content = ''; content = '';
} }
}
content = escapeHtml(content); content = escapeHtml(content);
const result = await sendMessage({name: 'save-note', title, content}); const result = await sendMessage({name: 'save-link-with-note', title, content});
if (result) { if (result) {
$textNote.val(''); $textNote.val('');
@ -75,7 +76,7 @@ async function saveNote() {
} }
} }
$("#save-button").on("click", saveNote); $("#save-button").on("click", saveLinkWithNote);
$("#show-help-button").on("click", () => { $("#show-help-button").on("click", () => {
window.open("https://github.com/zadam/trilium/wiki/Web-clipper", '_blank'); window.open("https://github.com/zadam/trilium/wiki/Web-clipper", '_blank');

View File

@ -8,3 +8,21 @@ function randomString(len) {
return text; return text;
} }
function getBaseUrl() {
let output = getPageLocationOrigin() + location.pathname;
if (output[output.length - 1] !== '/') {
output = output.split('/');
output.pop();
output = output.join('/');
}
return output;
}
function getPageLocationOrigin() {
// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)
// but for file:// protocol this is browser dependant and in particular Firefox returns "null" in this case.
return location.protocol === 'file:' ? 'file://' : location.origin;
}