aboutsummaryrefslogtreecommitdiff
path: root/extension/src/Icons.ts
blob: b386713a9cc0013b95b578c9f01ac371fd755bb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {getBrowser} from "./main.tsx";
import BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode;
import {RGBColor} from "colorthief";

interface IconCacheEntry {
    url?: string,
    data: string,
    setByUser: boolean,
    source: "google" | "site" | "custom"
}

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];

    if (cache) {
        // if there is an icon in the cache, set that
        setIcon(cache.data);
        if (cache.setByUser) {
            // if the user was the one who set the icon, then quit
            return;
        }

        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})
                }
            })
        }
    } 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})
                    }
                })
            }
        })
    }
}

async function iconFromGoogle(bmUrl: string): Promise<string | undefined> {
    const url = new URL('https://www.google.com/s2/favicons');
    url.searchParams.set("sz", "256");
    url.searchParams.set("domain_url", bmUrl);
    let resp = await fetch(url)
    if (resp.ok) {
        return url.toString()
    }
}

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 toDataURL(url: string) {
    let response = await fetch(url);
    let blob: Blob = await response.blob();
    return await new Promise<string>((resolve, reject) => {
        const reader = new FileReader()
        // @ts-ignore
        reader.onloadend = () => resolve(reader.result)
        reader.onerror = reject
        reader.readAsDataURL(blob)
    });
}

export {type IconCacheEntry, getIcon}