Add save, send and close current window's tabs feature

This commit is contained in:
alteist 2021-02-22 01:40:07 +06:00
parent 1c75ea89ad
commit 6952c467cd
3 changed files with 67 additions and 2 deletions

View File

@ -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 = '<ul>';
tabs.forEach(tab => {
content += `<li><a href="${tab.url}">${tab.title}</a></li>`
});
content += '</ul>';
// 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);
}

View File

@ -21,6 +21,7 @@
<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="create-text-note-button">Create text note</button>
<button class="button full needs-connection" id="save-tabs-button">Save window's tabs as a list</button>
<div id="create-text-note-wrapper">
<textarea id="create-text-note-textarea" rows="5"></textarea>
@ -43,4 +44,4 @@
<script src="popup.js"></script>
</body>
</html>
</html>

View File

@ -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"}));
$(() => browser.runtime.sendMessage({name: "send-trilium-search-status"}));