Merge remote-tracking branch 'origin/master'

# Conflicts:
#	popup/popup.html
#	popup/popup.js
This commit is contained in:
zadam
2021-03-31 22:25:16 +02:00
4 changed files with 94 additions and 15 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,14 @@ async function getActiveTab() {
return tabs[0];
}
async function getWindowTabs() {
const tabs = await browser.tabs.query({
currentWindow: true
});
return tabs;
}
async function sendMessageToActiveTab(message) {
const activeTab = await getActiveTab();
@@ -115,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
});
}
@@ -260,6 +271,50 @@ async function saveLinkWithNote(title, content) {
return true;
}
async function getTabsPayload(tabs) {
let content = '<ul>';
tabs.forEach(tab => {
content += `<li><a href="${tab.url}">${tab.title}</a></li>`
});
content += '</ul>';
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;
}
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) {
if (info.menuItemId === 'trilium-save-selection') {
await saveSelection();
@@ -326,6 +381,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});
}
@@ -340,6 +398,9 @@ browser.runtime.onMessage.addListener(async request => {
else if (request.name === 'save-link-with-note') {
return await saveLinkWithNote(request.title, request.content);
}
else if (request.name === 'save-tabs') {
return await saveTabs();
}
else if (request.name === 'trigger-trilium-search') {
triliumServerFacade.triggerSearchForTrilium();
}