Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(webclipper): address requested changes
Elian Doranb7b367b5a344
diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.tsindex 31ee608f53..8657ee5117 100644--- a/apps/web-clipper/entrypoints/background/index.ts+++ b/apps/web-clipper/entrypoints/background/index.ts@@ -2,6 +2,19 @@ import { randomString, Rect } from "@/utils";import TriliumServerFacade from "./trilium_server_facade";+type BackgroundMessage = {+ name: "toast";+ message: string;+ noteId: string | null;+ tabIds: number[] | null;+} | {+ name: "trilium-save-selection";+} | {+ name: "trilium-get-rectangle-for-screenshot";+} | {+ name: "trilium-save-page";+};+export default defineBackground(() => {const triliumServerFacade = new TriliumServerFacade();@@ -155,7 +168,7 @@ export default defineBackground(() => {return tabs;}- async function sendMessageToActiveTab(message) {+ async function sendMessageToActiveTab(message: BackgroundMessage) {const activeTab = await getActiveTab();if (!activeTab?.id) {@@ -229,7 +242,7 @@ export default defineBackground(() => {toast("Selection has been saved to Trilium.", resp.noteId);}- async function getImagePayloadFromSrc(src, pageUrl) {+ async function getImagePayloadFromSrc(src: string, pageUrl: string | null | undefined) {const image = {imageId: randomString(20),src@@ -247,7 +260,7 @@ export default defineBackground(() => {};}- async function saveCroppedScreenshot(pageUrl) {+ async function saveCroppedScreenshot(pageUrl: string | null | undefined) {const { rect, devicePixelRatio } = await sendMessageToActiveTab({name: 'trilium-get-rectangle-for-screenshot'});const src = await takeCroppedScreenshot(rect, devicePixelRatio);@@ -263,7 +276,7 @@ export default defineBackground(() => {toast("Screenshot has been saved to Trilium.", resp.noteId);}- async function saveWholeScreenshot(pageUrl) {+ async function saveWholeScreenshot(pageUrl: string | null | undefined) {const src = await takeWholeScreenshot();const payload = await getImagePayloadFromSrc(src, pageUrl);@@ -277,7 +290,7 @@ export default defineBackground(() => {toast("Screenshot has been saved to Trilium.", resp.noteId);}- async function saveImage(srcUrl, pageUrl) {+ async function saveImage(srcUrl: string, pageUrl: string | null | undefined) {const payload = await getImagePayloadFromSrc(srcUrl, pageUrl);const resp = await triliumServerFacade.callService("POST", "clippings", payload);@@ -303,11 +316,11 @@ export default defineBackground(() => {toast("Page has been saved to Trilium.", resp.noteId);}- async function saveLinkWithNote(title, content) {+ async function saveLinkWithNote(title: string, content: string) {const activeTab = await getActiveTab();if (!title.trim()) {- title = activeTab.title;+ title = activeTab.title ?? "";}const resp = await triliumServerFacade.callService('POST', 'notes', {@@ -326,7 +339,7 @@ export default defineBackground(() => {return true;}- async function getTabsPayload(tabs) {+ async function getTabsPayload(tabs: Browser.tabs.Tab[]) {let content = '<ul>';tabs.forEach(tab => {content += `<li><a href="${tab.url}">${tab.title}</a></li>`;@@ -335,7 +348,7 @@ export default defineBackground(() => {const domainsCount = tabs.map(tab => tab.url).reduce((acc, url) => {- const hostname = new URL(url).hostname;+ const hostname = new URL(url ?? "").hostname;return acc.set(hostname, (acc.get(hostname) || 0) + 1);}, new Map());@@ -362,7 +375,7 @@ export default defineBackground(() => {const resp = await triliumServerFacade.callService('POST', 'notes', payload);if (!resp) return;- const tabIds = tabs.map(tab => tab.id!).filter(id => id !== undefined) as number[];+ const tabIds = tabs.map(tab => tab.id).filter(id => id !== undefined) as number[];toast(`${tabs.length} links have been saved to Trilium.`, resp.noteId, tabIds);}@@ -377,6 +390,7 @@ export default defineBackground(() => {await saveWholeScreenshot(info.pageUrl);}else if (info.menuItemId === 'trilium-save-image') {+ if (!info.srcUrl) return;await saveImage(info.srcUrl, info.pageUrl);}else if (info.menuItemId === 'trilium-save-link') {@@ -407,7 +421,7 @@ export default defineBackground(() => {console.log("Received", request);if (request.name === 'openNoteInTrilium') {- const resp = await triliumServerFacade.callService('POST', `open/${ request.noteId}`);+ const resp = await triliumServerFacade.callService('POST', `open/${request.noteId}`);if (!resp) {return;diff --git a/apps/web-clipper/entrypoints/background/trilium_server_facade.ts b/apps/web-clipper/entrypoints/background/trilium_server_facade.tsindex 43247a9e79..da553cec29 100644--- a/apps/web-clipper/entrypoints/background/trilium_server_facade.ts+++ b/apps/web-clipper/entrypoints/background/trilium_server_facade.ts@@ -57,7 +57,7 @@ export default class TriliumServerFacade {catch (e) {} // nothing might be listening}- setTriliumSearchNote(st){+ setTriliumSearchNote(st: TriliumSearchNoteStatus){this.triliumSearchNote = st;this.sendTriliumSearchNoteToPopup();}@@ -69,7 +69,7 @@ export default class TriliumServerFacade {}setTriliumSearchWithVersionCheck(json: { protocolVersion: string }, resp: TriliumSearchStatus) {- const [major, minor] = json.protocolVersion+ const [ major ] = json.protocolVersion.split(".").map(chunk => parseInt(chunk, 10));@@ -94,7 +94,7 @@ export default class TriliumServerFacade {try {const port = await this.getPort();- console.debug(`Trying port ${ port}`);+ console.debug(`Trying port ${port}`);const resp = await fetch(`http://127.0.0.1:${port}/api/clipper/handshake`);diff --git a/apps/web-clipper/entrypoints/content/index.ts b/apps/web-clipper/entrypoints/content/index.tsindex b708bd6393..d35f3d8970 100644--- a/apps/web-clipper/entrypoints/content/index.ts+++ b/apps/web-clipper/entrypoints/content/index.ts@@ -6,7 +6,7 @@ export default defineContentScript({"<all_urls>"],main: () => {- function absoluteUrl(url) {+ function absoluteUrl(url: string | undefined) {if (!url) {return url;}@@ -89,8 +89,8 @@ export default defineContentScript({messageComp.style.position = 'fixed';messageComp.style.opacity = '0.95';messageComp.style.fontSize = '14px';- messageComp.style.width = `${messageCompWidth }px`;- messageComp.style.maxWidth = `${messageCompWidth }px`;+ messageComp.style.width = `${messageCompWidth}px`;+ messageComp.style.maxWidth = `${messageCompWidth}px`;messageComp.style.border = '1px solid black';messageComp.style.background = 'white';messageComp.style.color = 'black';@@ -199,10 +199,12 @@ export default defineContentScript({});}- function makeLinksAbsolute(container) {+ function makeLinksAbsolute(container: HTMLElement) {for (const link of container.getElementsByTagName('a')) {if (link.href) {- link.href = absoluteUrl(link.href);+ const newUrl = absoluteUrl(link.href);+ if (!newUrl) continue;+ link.href = newUrl;}}}diff --git a/apps/web-clipper/entrypoints/options/index.ts b/apps/web-clipper/entrypoints/options/index.tsindex da8fa1850a..c7cb3d4bdf 100644--- a/apps/web-clipper/entrypoints/options/index.ts+++ b/apps/web-clipper/entrypoints/options/index.ts@@ -27,7 +27,7 @@ async function saveTriliumServerSetup(e) {let resp;try {- resp = await fetch(`${$triliumServerUrl.val() }/api/login/token`, {+ resp = await fetch(`${$triliumServerUrl.val()}/api/login/token`, {method: "POST",headers: {'Accept': 'application/json',diff --git a/apps/web-clipper/entrypoints/popup/popup.ts b/apps/web-clipper/entrypoints/popup/popup.tsindex 273fe5c5b5..0e4613b794 100644--- a/apps/web-clipper/entrypoints/popup/popup.ts+++ b/apps/web-clipper/entrypoints/popup/popup.ts@@ -111,7 +111,7 @@ function escapeHtml(string) {const htmlWithPars = pre.innerHTML.replace(/\n/g, "</p><p>");- return `<p>${ htmlWithPars }</p>`;+ return `<p>${htmlWithPars}</p>`;}const $connectionStatus = $("#connection-status");