diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-01-14 18:00:03 -0500 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-01-14 18:00:03 -0500 |
commit | ef013458cc29c6948603e32a001019fe6c52b743 (patch) | |
tree | c8e0e49e91fb76c9d969d2383d74082825099750 /extension/src | |
parent | a37e3935e755f9a7f1a81e51d9fee696cac681c2 (diff) | |
download | bookmarks-home-ef013458cc29c6948603e32a001019fe6c52b743.tar.gz bookmarks-home-ef013458cc29c6948603e32a001019fe6c52b743.tar.bz2 bookmarks-home-ef013458cc29c6948603e32a001019fe6c52b743.zip |
Refactor Icons.ts
Diffstat (limited to 'extension/src')
-rw-r--r-- | extension/src/Icons.ts | 87 |
1 files changed, 31 insertions, 56 deletions
diff --git a/extension/src/Icons.ts b/extension/src/Icons.ts index b386713..39fee23 100644 --- a/extension/src/Icons.ts +++ b/extension/src/Icons.ts @@ -1,6 +1,5 @@ import {getBrowser} from "./main.tsx"; import BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode; -import {RGBColor} from "colorthief"; interface IconCacheEntry { url?: string, @@ -10,9 +9,7 @@ interface IconCacheEntry { } async function getIcon(bmData: BookmarkTreeNode, setIcon: (icon: string) => void) { - let id = bmData.id; - let bmUrl = bmData.url!; - let cache: IconCacheEntry = (await getBrowser().storage.local.get("icon-cache-" + id))["icon-cache-" + id]; + let cache: IconCacheEntry = (await getBrowser().storage.local.get("icon-cache-" + bmData.id))["icon-cache-" + bmData.id]; if (cache) { // if there is an icon in the cache, set that @@ -23,71 +20,49 @@ async function getIcon(bmData: BookmarkTreeNode, setIcon: (icon: string) => void } if (cache.source === "google") { - // if the cached icon came from google check to see if iconGrabber found an icon - iconFromSite(id).then(async r => { - if (r) { - // set the icon and update the cache - let iconData = toDataURL(r); - setIcon(r); - let newCache: IconCacheEntry = { - url: r, - data: await iconData, - setByUser: false, - source: "site" - } - await getBrowser().storage.local.set({["icon-cache-" +id]: newCache}) - } - }) + await iconFromSite(bmData, setIcon); } } else { - - iconFromSite(id).then(async r => { - if (r) { - // set the icon and update the cache - let iconData = toDataURL(r); - setIcon(r); - let newCache: IconCacheEntry = { - url: r, - data: await iconData, - setByUser: false, - source: "site" - } - await getBrowser().storage.local.set({["icon-cache-" +id]: newCache}) - } else { - iconFromGoogle(bmUrl).then(async r => { - if (r) { - // set the icon and update the cache - let iconData = toDataURL(r); - setIcon(r); - let newCache: IconCacheEntry = { - url: r, - data: await iconData, - setByUser: false, - source: "google" - } - await getBrowser().storage.local.set({["icon-cache-" +id]: newCache}) - } - }) - } + iconFromSite(bmData, setIcon).catch(() => { + iconFromGoogle(bmData, setIcon); }) } } -async function iconFromGoogle(bmUrl: string): Promise<string | undefined> { +async function iconFromGoogle(bmData: BookmarkTreeNode, setIcon: (icon: string) => void) { const url = new URL('https://www.google.com/s2/favicons'); url.searchParams.set("sz", "256"); - url.searchParams.set("domain_url", bmUrl); + url.searchParams.set("domain_url", bmData.url!); let resp = await fetch(url) - if (resp.ok) { - return url.toString() + if (!resp.ok) { + return Promise.reject(); + } + let r = url.toString() + setIcon(r); + let newCache: IconCacheEntry = { + url: r, + data: await toDataURL(r), + setByUser: false, + source: "google" } + await getBrowser().storage.local.set({["icon-cache-" + bmData.id]: newCache}) } -async function iconFromSite(id: string): Promise<string | undefined> { - let icons_aval: string[] = (await getBrowser().storage.local.get("icon-aval-" + id))["icon-aval-" + id]; - if (icons_aval && icons_aval.length > 0) { - return icons_aval[0]; +async function iconFromSite(bmData: BookmarkTreeNode, setIcon: (icon: string) => void) { + let icons_aval: string[] = (await getBrowser().storage.local.get("icon-aval-" + bmData.id))["icon-aval-" + bmData.id]; + if (!(icons_aval && icons_aval.length > 0)) { + return Promise.reject() + } + let r = icons_aval[0]; + // set the icon and update the cache + setIcon(r); + let newCache: IconCacheEntry = { + url: r, + data: await toDataURL(r), + setByUser: false, + source: "site" } + await getBrowser().storage.local.set({["icon-cache-" +bmData.id]: newCache}) } async function toDataURL(url: string) { |