From 6952c467cdb4eaa5c226b24983421d8b581de9f0 Mon Sep 17 00:00:00 2001 From: alteist Date: Mon, 22 Feb 2021 01:40:07 +0600 Subject: [PATCH 1/5] Add save, send and close current window's tabs feature --- background.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ popup/popup.html | 3 ++- popup/popup.js | 5 +++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/background.js b/background.js index 060d8db75..406d9f3b6 100644 --- a/background.js +++ b/background.js @@ -4,6 +4,8 @@ chrome.commands.onCommand.addListener(async function (command) { await saveSelection(); } else if (command == "saveWholePage") { await saveWholePage(); + } else if (command == "saveTabs") { + await saveTabs(); } else if (command == "saveScreenshot") { const activeTab = await getActiveTab(); @@ -100,6 +102,18 @@ async function getActiveTab() { return tabs[0]; } +async function getWindowTabs() { + const tabs = await browser.tabs.query({ + currentWindow: true + }); + + return tabs; +} + +async function closeWindowTabs(tabs) { + await browser.tabs.remove(tabs.map(tab=>{return tab.id})); +} + async function sendMessageToActiveTab(message) { const activeTab = await getActiveTab(); @@ -237,6 +251,50 @@ async function saveWholePage() { toast("Page has been saved to Trilium.", resp.noteId); } +async function getTabsPayload(tabs) { + let content = ''; + + // topDomains string with 1-3 most used domains will be used as the note title + // to help the user differentiate between multiple notes of this type + const domainsCount = tabs.map(tab => tab.url) + .reduce((acc, url) => { + const hostname = new URL(url).hostname + return acc.set(hostname, (acc.get(hostname) || 0) + 1) + }, new Map()); + + let topDomains = [...domainsCount] + .sort((a, b) => {return b[1]-a[1]}) + .slice(0,3) + .map(domain=>domain[0]) + .join(', ') + + if (tabs.length > 3) { topDomains += '...' }; + + return { + title: `${tabs.length} browser tabs: ${topDomains}`, + content: content, + clipType: 'tabs' + }; +} + +async function saveTabs() { + const tabs = await getWindowTabs(); + + const payload = await getTabsPayload(tabs); + + const resp = await triliumServerFacade.callService('POST', 'notes', payload); + + if (!resp) { + return; + } + + await closeWindowTabs(tabs); +} + async function saveNote(title, content) { const resp = await triliumServerFacade.callService('POST', 'notes', { title: title, @@ -330,6 +388,9 @@ browser.runtime.onMessage.addListener(async request => { else if (request.name === 'save-whole-page') { return await saveWholePage(); } + else if (request.name === 'save-tabs') { + return await saveTabs(); + } else if (request.name === 'save-note') { return await saveNote(request.title, request.content); } diff --git a/popup/popup.html b/popup/popup.html index fe94eed29..3a85fd7a1 100644 --- a/popup/popup.html +++ b/popup/popup.html @@ -21,6 +21,7 @@ +
@@ -43,4 +44,4 @@ - \ No newline at end of file + diff --git a/popup/popup.js b/popup/popup.js index ff61ca423..0b5a008db 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -12,6 +12,7 @@ async function sendMessage(message) { const $showOptionsButton = $("#show-options-button"); const $clipScreenShotButton = $("#clip-screenshot-button"); const $saveWholePageButton = $("#save-whole-page-button"); +const $saveTabsButton = $("#save-tabs-button"); $showOptionsButton.on("click", () => browser.runtime.openOptionsPage()); @@ -19,6 +20,8 @@ $clipScreenShotButton.on("click", () => sendMessage({name: 'save-screenshot'})); $saveWholePageButton.on("click", () => sendMessage({name: 'save-whole-page'})); +$saveTabsButton.on("click", () => sendMessage({name: 'save-tabs'})); + const $createTextNoteWrapper = $("#create-text-note-wrapper"); const $textNote = $("#create-text-note-textarea"); @@ -141,4 +144,4 @@ $checkConnectionButton.on("click", () => { }) }); -$(() => browser.runtime.sendMessage({name: "send-trilium-search-status"})); \ No newline at end of file +$(() => browser.runtime.sendMessage({name: "send-trilium-search-status"})); From 9abee14beb98df1446b944d0cd9ce963c1c70cb3 Mon Sep 17 00:00:00 2001 From: alteist Date: Mon, 22 Feb 2021 13:24:46 +0600 Subject: [PATCH 2/5] reformat code --- background.js | 92 +++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/background.js b/background.js index 406d9f3b6..f3fc51ab7 100644 --- a/background.js +++ b/background.js @@ -251,50 +251,6 @@ async function saveWholePage() { toast("Page has been saved to Trilium.", resp.noteId); } -async function getTabsPayload(tabs) { - let content = '
    '; - tabs.forEach(tab => { - content += `
  • ${tab.title}
  • ` - }); - content += '
'; - - // topDomains string with 1-3 most used domains will be used as the note title - // to help the user differentiate between multiple notes of this type - const domainsCount = tabs.map(tab => tab.url) - .reduce((acc, url) => { - const hostname = new URL(url).hostname - return acc.set(hostname, (acc.get(hostname) || 0) + 1) - }, new Map()); - - let topDomains = [...domainsCount] - .sort((a, b) => {return b[1]-a[1]}) - .slice(0,3) - .map(domain=>domain[0]) - .join(', ') - - if (tabs.length > 3) { topDomains += '...' }; - - return { - title: `${tabs.length} browser tabs: ${topDomains}`, - content: content, - clipType: 'tabs' - }; -} - -async function saveTabs() { - const tabs = await getWindowTabs(); - - const payload = await getTabsPayload(tabs); - - const resp = await triliumServerFacade.callService('POST', 'notes', payload); - - if (!resp) { - return; - } - - await closeWindowTabs(tabs); -} - async function saveNote(title, content) { const resp = await triliumServerFacade.callService('POST', 'notes', { title: title, @@ -311,6 +267,48 @@ async function saveNote(title, content) { return true; } +async function getTabsPayload(tabs) { + let content = '
    '; + tabs.forEach(tab => { + content += `
  • ${tab.title}
  • ` + }); + content += '
'; + + const domainsCount = tabs.map(tab => tab.url) + .reduce((acc, url) => { + const hostname = new URL(url).hostname + return acc.set(hostname, (acc.get(hostname) || 0) + 1) + }, new Map()); + + let topDomains = [...domainsCount] + .sort((a, b) => {return b[1]-a[1]}) + .slice(0,3) + .map(domain=>domain[0]) + .join(', ') + + if (tabs.length > 3) { topDomains += '...' } + + return { + title: `${tabs.length} browser tabs: ${topDomains}`, + content: content, + clipType: 'tabs' + }; +} + +async function saveTabs() { + const tabs = await getWindowTabs(); + + const payload = await getTabsPayload(tabs); + + const resp = await triliumServerFacade.callService('POST', 'notes', payload); + + if (!resp) { + return; + } + + await closeWindowTabs(tabs); +} + browser.contextMenus.onClicked.addListener(async function(info, tab) { if (info.menuItemId === 'trilium-save-selection') { await saveSelection(); @@ -388,12 +386,12 @@ browser.runtime.onMessage.addListener(async request => { else if (request.name === 'save-whole-page') { return await saveWholePage(); } - else if (request.name === 'save-tabs') { - return await saveTabs(); - } else if (request.name === 'save-note') { return await saveNote(request.title, request.content); } + else if (request.name === 'save-tabs') { + return await saveTabs(); + } else if (request.name === 'trigger-trilium-search') { triliumServerFacade.triggerSearchForTrilium(); } From 6824eb21d41cbce44ae63266b8e9db2cbf164ed8 Mon Sep 17 00:00:00 2001 From: alteist Date: Mon, 22 Feb 2021 13:34:34 +0600 Subject: [PATCH 3/5] removed close tabs function, added toast message after saving --- background.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/background.js b/background.js index f3fc51ab7..edd3fda23 100644 --- a/background.js +++ b/background.js @@ -110,10 +110,6 @@ async function getWindowTabs() { return tabs; } -async function closeWindowTabs(tabs) { - await browser.tabs.remove(tabs.map(tab=>{return tab.id})); -} - async function sendMessageToActiveTab(message) { const activeTab = await getActiveTab(); @@ -306,7 +302,7 @@ async function saveTabs() { return; } - await closeWindowTabs(tabs); + toast(`${tabs.length} links have been saved to Trilium.`, resp.noteId); } browser.contextMenus.onClicked.addListener(async function(info, tab) { From 73d48d8dd5635f38a82a002ff989348e37b143d1 Mon Sep 17 00:00:00 2001 From: alteist Date: Mon, 22 Feb 2021 14:35:55 +0600 Subject: [PATCH 4/5] Add close tabs link in toast message after saving window's tabs --- background.js | 14 +++++++++++--- content.js | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/background.js b/background.js index edd3fda23..49f94eaf4 100644 --- a/background.js +++ b/background.js @@ -125,11 +125,12 @@ async function sendMessageToActiveTab(message) { } } -function toast(message, noteId = null) { +function toast(message, noteId = null, tabIds = null) { sendMessageToActiveTab({ name: 'toast', message: message, - noteId: noteId + noteId: noteId, + tabIds: tabIds }); } @@ -302,7 +303,11 @@ async function saveTabs() { return; } - toast(`${tabs.length} links have been saved to Trilium.`, resp.noteId); + toast( + `${tabs.length} links have been saved to Trilium.`, + resp.noteId, + tabs.map(tab=>{return tab.id}) + ); } browser.contextMenus.onClicked.addListener(async function(info, tab) { @@ -371,6 +376,9 @@ browser.runtime.onMessage.addListener(async request => { } } } + else if (request.name === 'closeTabs') { + return await browser.tabs.remove(request.tabIds) + } else if (request.name === 'load-script') { return await browser.tabs.executeScript({file: request.file}); } diff --git a/content.js b/content.js index 1fb006636..326323b00 100644 --- a/content.js +++ b/content.js @@ -209,6 +209,17 @@ function getImages(container) { return images; } +function createLink(clickMessage, text, color = "lightskyblue") { + const link = document.createElement('a'); + link.href = "javascript:"; + link.style.color = color; + link.appendChild(document.createTextNode(text)); + link.addEventListener("click", () => { + browser.runtime.sendMessage(null, clickMessage) + }); + return link +} + async function prepareMessageResponse(message) { console.info('Message: ' + message.name); @@ -216,21 +227,24 @@ async function prepareMessageResponse(message) { let messageText; if (message.noteId) { - messageText = document.createElement('span'); + messageText = document.createElement('p'); + messageText.setAttribute("style", "padding: 0; margin: 0") messageText.appendChild(document.createTextNode(message.message + " ")); + openNoteLink = createLink( + {name: 'openNoteInTrilium',noteId: message.noteId}, + "Open in Trilium." + ) + messageText.appendChild(openNoteLink); - const link = document.createElement('a'); - link.href = "javascript:"; - link.style.color = "lightskyblue"; - link.appendChild(document.createTextNode("Open in Trilium.")); - link.addEventListener("click", () => { - browser.runtime.sendMessage(null, { - name: 'openNoteInTrilium', - noteId: message.noteId - }) - }); - - messageText.appendChild(link); + if (message.tabIds) { + messageText.appendChild(document.createElement("br")); + closeTabsLink = createLink( + {name: 'closeTabs', tabIds: message.tabIds}, + "Close saved tabs.", + "tomato" + ) + messageText.appendChild(closeTabsLink); + } } else { messageText = message.message; @@ -304,4 +318,4 @@ async function requireLib(libPath) { await browser.runtime.sendMessage({name: 'load-script', file: libPath}); } -} \ No newline at end of file +} From b42fd1a6c52e7892ad06b54a1fbfb017af81b6a4 Mon Sep 17 00:00:00 2001 From: alteist Date: Mon, 22 Feb 2021 15:30:12 +0600 Subject: [PATCH 5/5] code cleanup --- background.js | 8 +++----- content.js | 18 +++++++++--------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/background.js b/background.js index 49f94eaf4..b4ac88bdf 100644 --- a/background.js +++ b/background.js @@ -303,11 +303,9 @@ async function saveTabs() { return; } - toast( - `${tabs.length} links have been saved to Trilium.`, - resp.noteId, - tabs.map(tab=>{return tab.id}) - ); + const tabIds = tabs.map(tab=>{return tab.id}); + + toast(`${tabs.length} links have been saved to Trilium.`, resp.noteId, tabIds); } browser.contextMenus.onClicked.addListener(async function(info, tab) { diff --git a/content.js b/content.js index 326323b00..25048685d 100644 --- a/content.js +++ b/content.js @@ -209,14 +209,15 @@ function getImages(container) { return images; } -function createLink(clickMessage, text, color = "lightskyblue") { +function createLink(clickAction, text, color = "lightskyblue") { const link = document.createElement('a'); link.href = "javascript:"; link.style.color = color; link.appendChild(document.createTextNode(text)); link.addEventListener("click", () => { - browser.runtime.sendMessage(null, clickMessage) + browser.runtime.sendMessage(null, clickAction) }); + return link } @@ -230,20 +231,19 @@ async function prepareMessageResponse(message) { messageText = document.createElement('p'); messageText.setAttribute("style", "padding: 0; margin: 0") messageText.appendChild(document.createTextNode(message.message + " ")); - openNoteLink = createLink( - {name: 'openNoteInTrilium',noteId: message.noteId}, + messageText.appendChild(createLink( + {name: 'openNoteInTrilium', noteId: message.noteId}, "Open in Trilium." - ) - messageText.appendChild(openNoteLink); + )); + // only after saving tabs if (message.tabIds) { messageText.appendChild(document.createElement("br")); - closeTabsLink = createLink( + messageText.appendChild(createLink( {name: 'closeTabs', tabIds: message.tabIds}, "Close saved tabs.", "tomato" - ) - messageText.appendChild(closeTabsLink); + )); } } else {