Skip to content

Build your personal knowledge base with Trilium Notes

chore(web-clipper): address requested changes

Elian Doranc2a758dd4af3
diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.ts
index 7ca94f21e7..5ac45ba92f 100644
--- a/apps/web-clipper/entrypoints/background/index.ts
+++ b/apps/web-clipper/entrypoints/background/index.ts
@@ -7,18 +7,23 @@ export default defineBackground(() => {
// Keyboard shortcuts
browser.commands.onCommand.addListener(async (command) => {
- if (command == "saveSelection") {
- await saveSelection();
- } else if (command == "saveWholePage") {
- await saveWholePage();
- } else if (command == "saveTabs") {
- await saveTabs();
- } else if (command == "saveCroppedScreenshot") {
- const activeTab = await getActiveTab();
-
- await saveCroppedScreenshot(activeTab.url);
- } else {
- console.log("Unrecognized command", command);
+ switch (command) {
+ case "saveSelection":
+ await saveSelection();
+ break;
+ case "saveWholePage":
+ await saveWholePage();
+ break;
+ case "saveTabs":
+ await saveTabs();
+ break;
+ case "saveCroppedScreenshot": {
+ const activeTab = await getActiveTab();
+ await saveCroppedScreenshot(activeTab.url);
+ break;
+ }
+ default:
+ console.log("Unrecognized command", command);
}
});
@@ -39,6 +44,7 @@ export default defineBackground(() => {
ctx.drawImage(img, newArea.x, newArea.y, newArea.width, newArea.height, 0, 0, newArea.width, newArea.height);
resolve(canvas.toDataURL());
};
+ img.onerror = reject;
img.src = dataUrl;
});
@@ -188,14 +194,15 @@ export default defineBackground(() => {
async function postProcessImage(image: { src: string, dataUrl?: string | null }) {
if (image.src.startsWith("data:image/")) {
image.dataUrl = image.src;
- image.src = `inline.${ image.src.substr(11, 3)}`; // this should extract file type - png/jpg
+ const mimeSubtype = image.src.match(/data:image\/(.*?);/)?.[1];
+ if (!mimeSubtype) return;
+ image.src = `inline.${mimeSubtype}`; // this should extract file type - png/jpg
}
else {
try {
image.dataUrl = await fetchImage(image.src);
- }
- catch (e) {
- console.log(`Cannot fetch image from ${image.src}`);
+ } catch (e) {
+ console.error(`Cannot fetch image from ${image.src}`, e);
}
}
}
diff --git a/apps/web-clipper/entrypoints/background/trilium_server_facade.ts b/apps/web-clipper/entrypoints/background/trilium_server_facade.ts
index 3e65b849df..43247a9e79 100644
--- a/apps/web-clipper/entrypoints/background/trilium_server_facade.ts
+++ b/apps/web-clipper/entrypoints/background/trilium_server_facade.ts
@@ -154,7 +154,7 @@ export default class TriliumServerFacade {
this.setTriliumSearch({ status: 'not-found' });
}
- async triggerSearchNoteByUrl(noteUrl) {
+ async triggerSearchNoteByUrl(noteUrl: string) {
const resp = await this.callService('GET', `notes-by-url/${encodeURIComponent(noteUrl)}`);
let newStatus: TriliumSearchNoteStatus;
if (resp && resp.noteId) {
@@ -229,8 +229,6 @@ export default class TriliumServerFacade {
catch (e) {
console.log("Sending request to trilium failed", e);
- window.showToast('Your request failed because we could not contact Trilium instance. Please make sure Trilium is running and is accessible.');
-
return null;
}
}
diff --git a/apps/web-clipper/entrypoints/content/index.ts b/apps/web-clipper/entrypoints/content/index.ts
index 618ad11e16..b708bd6393 100644
--- a/apps/web-clipper/entrypoints/content/index.ts
+++ b/apps/web-clipper/entrypoints/content/index.ts
@@ -55,12 +55,12 @@ export default defineContentScript({
let modifiedDate: Date | null = null;
const articlePublishedTime = document.querySelector("meta[property='article:published_time']")?.getAttribute('content');
- if (articlePublishedTime && articlePublishedTime) {
+ if (articlePublishedTime) {
publishedDate = new Date(articlePublishedTime);
}
const articleModifiedTime = document.querySelector("meta[property='article:modified_time']")?.getAttribute('content');
- if (articleModifiedTime && articleModifiedTime) {
+ if (articleModifiedTime) {
modifiedDate = new Date(articleModifiedTime);
}
@@ -129,7 +129,7 @@ export default defineContentScript({
selection.style.height = `${selectionArea.height}px`;
}
- function setSelectionSizeFromMouse(event) {
+ function setSelectionSizeFromMouse(event: MouseEvent) {
if (!draggingStartPos) return;
if (event.clientX < draggingStartPos.x) {
@@ -145,14 +145,14 @@ export default defineContentScript({
updateSelection();
}
- function selection_mouseDown(event) {
+ function selection_mouseDown(event: MouseEvent) {
selectionArea = {x: event.clientX, y: event.clientY, width: 0, height: 0};
draggingStartPos = {x: event.clientX, y: event.clientY};
isDragging = true;
updateSelection();
}
- function selection_mouseMove(event) {
+ function selection_mouseMove(event: MouseEvent) {
if (!isDragging) return;
setSelectionSizeFromMouse(event);
}
@@ -169,7 +169,7 @@ export default defineContentScript({
document.body.removeChild(messageComp);
}
- function selection_mouseUp(event) {
+ function selection_mouseUp(event: MouseEvent) {
setSelectionSizeFromMouse(event);
removeOverlay();
@@ -186,7 +186,7 @@ export default defineContentScript({
setTimeout(() => resolve(selectionArea), 100);
}
- function cancel(event) {
+ function cancel(event: KeyboardEvent) {
if (event.key === "Escape") {
removeOverlay();
}
@@ -195,7 +195,6 @@ export default defineContentScript({
overlay.addEventListener('mousedown', selection_mouseDown);
overlay.addEventListener('mousemove', selection_mouseMove);
overlay.addEventListener('mouseup', selection_mouseUp);
- overlay.addEventListener('mouseup', selection_mouseUp);
messageComp.addEventListener('keydown', cancel);
});
}