diff options
Diffstat (limited to 'extension/src/components/Bookmark.tsx')
-rw-r--r-- | extension/src/components/Bookmark.tsx | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/extension/src/components/Bookmark.tsx b/extension/src/components/Bookmark.tsx index 75badd7..c12da35 100644 --- a/extension/src/components/Bookmark.tsx +++ b/extension/src/components/Bookmark.tsx @@ -1,4 +1,7 @@ import BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode; +import GlobeIcon from "../assets/globe.svg" +import React, {useEffect} from "react"; +import {getBrowser} from "../main.tsx"; /** * A component for a single bookmark @@ -6,9 +9,19 @@ import BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode; * @param props.data The BookmarkTreeNode with the data for the bookmark */ function Bookmark(props: {data: BookmarkTreeNode}) { + let [favicon, setFavicon] = React.useState(GlobeIcon); + useEffect(() => { + // faviconURL(props.data.url).then(o => o && setFavicon(o)) + faviconURL(props.data).then(r => { + if (r) { + setFavicon(r); + } + }) + }, []); + return( <a className="bookmark draggable" href={props.data.url}> - <img alt="Bookmark icon" src={faviconURL(props.data.url)}></img> + <img alt="Bookmark icon" src={favicon}></img> <span>{props.data.title}</span> </a> ); @@ -17,18 +30,19 @@ function Bookmark(props: {data: BookmarkTreeNode}) { /** * Gets the icon for a bookmark * - * @param u The URL of the link + * @param data The URL of the link * @return The URL of the icon */ -function faviconURL(u: string | undefined) { - if (!u) return ""; - u = new URL(u).hostname.toString(); +async function faviconURL(data: BookmarkTreeNode) { + + let i = (await getBrowser().storage.local.get("icon-"+data.id))["icon-"+data.id]; + if (i) return i.toString() const url = new URL('https://www.google.com/s2/favicons'); url.searchParams.set("sz", "256"); - // u = u.split(".")[u.split(".").length-2] +"."+ u.split(".")[u.split(".").length-1] - url.searchParams.set("domain_url", u); - return url.toString(); - + url.searchParams.set("domain_url", data.url!); + if ((await fetch(url)).ok) { + return url.toString(); + } } export default Bookmark;
\ No newline at end of file |