Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor favicon fetch to separate method (#649)

Leendert de Borst7da5557b980b
diff --git a/browser-extensions/chrome/src/contentScript/Popup.ts b/browser-extensions/chrome/src/contentScript/Popup.ts
index 793f1456f..06a32afd4 100644
--- a/browser-extensions/chrome/src/contentScript/Popup.ts
+++ b/browser-extensions/chrome/src/contentScript/Popup.ts
@@ -1131,45 +1131,51 @@ async function getFaviconBytes(document: Document): Promise<Uint8Array | null> {
const uniqueLinks = Array.from(new Map(faviconLinks.map(link => [link.href, link])).values());
for (const link of uniqueLinks) {
- try {
- const response = await fetch(link.href);
- if (!response.ok) {
- // Could not fetch favicon, skip.
- continue;
- }
+ const imageData = await fetchAndProcessFavicon(link.href, MAX_SIZE_BYTES, TARGET_WIDTH);
+ if (imageData) {
+ return imageData;
+ }
+ }
- const contentType = response.headers.get('content-type');
- if (!contentType || !contentType.startsWith('image/')) {
- // Not a valid favicon, skip.
- continue;
- }
+ return null;
+}
- const arrayBuffer = await response.arrayBuffer();
- if (arrayBuffer.byteLength === 0) {
- // Empty favicon, skip.
- continue;
- }
+/**
+ * Attempt to fetch and process a favicon from a given URL
+ */
+async function fetchAndProcessFavicon(url: string, maxSize: number, targetWidth: number): Promise<Uint8Array | null> {
+ try {
+ const response = await fetch(url);
+ if (!response.ok) {
+ return null;
+ }
- let imageData = new Uint8Array(arrayBuffer);
+ const contentType = response.headers.get('content-type');
+ if (!contentType?.startsWith('image/')) {
+ return null;
+ }
- // If image is too large, attempt to resize.
- if (imageData.byteLength > MAX_SIZE_BYTES) {
- const resizedBlob = await resizeImage(imageData, contentType, TARGET_WIDTH);
- if (resizedBlob) {
- imageData = new Uint8Array(await resizedBlob.arrayBuffer());
- }
- }
+ const arrayBuffer = await response.arrayBuffer();
+ if (arrayBuffer.byteLength === 0) {
+ return null;
+ }
+
+ let imageData = new Uint8Array(arrayBuffer);
- // Ensure final size is within limits.
- if (imageData.byteLength <= MAX_SIZE_BYTES) {
- return imageData;
+ // If image is too large, attempt to resize
+ if (imageData.byteLength > maxSize) {
+ const resizedBlob = await resizeImage(imageData, contentType, targetWidth);
+ if (resizedBlob) {
+ imageData = new Uint8Array(await resizedBlob.arrayBuffer());
}
- } catch (error) {
- console.error('Error fetching favicon:', link.href, error);
}
- }
- return null;
+ // Return only if within size limits
+ return imageData.byteLength <= maxSize ? imageData : null;
+ } catch (error) {
+ console.error('Error fetching favicon:', url, error);
+ return null;
+ }
}
/**