aboutsummaryrefslogtreecommitdiff
path: root/extension/public/iconGrabber.js
blob: 5ccdccf9276baeef4fb185bd713c06857ef18799 (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
function getBrowser() {
    return typeof browser === "undefined" ? chrome : browser;
}

const tagTypes = ["apple-touch-icon", "shortcut icon", "icon"]

let x = Array.from(document.getElementsByTagName("link"))
    .filter(elem => tagTypes.includes(elem.rel))
    .sort((a, b) => {
        function compareTags() {
            // ascending
            return tagTypes.indexOf(a.rel) - tagTypes.indexOf(b.rel);
        }
        function compareSizes() {
            function getSize(elem) {
                try { return Number(elem.sizes[0].split('x')[0]); }
                catch { return 0; }
            }
            // descending
            return getSize(b) - getSize(a);
        }

        return compareSizes() || compareTags()
    });

console.log("found icons", x.map(elem => elem.outerHTML));
x = x.map(elem => elem.href);

getBrowser().runtime.sendMessage([window.location.href, x]).catch(() => {
    console.log("failed to send message")
})