xref: /plugin/pagesicon/script/favicon-runtime.js (revision b603bbe1e83f2122ab63b4df278133cf619e6be8)
1*b603bbe1SLORTET(function () {
2*b603bbe1SLORTET    function replaceFavicon(href) {
3*b603bbe1SLORTET        if (!href || !document.head) return;
4*b603bbe1SLORTET
5*b603bbe1SLORTET        var links = document.head.querySelectorAll('link[rel*="icon"]');
6*b603bbe1SLORTET        for (var i = 0; i < links.length; i++) {
7*b603bbe1SLORTET            if (links[i].parentNode) {
8*b603bbe1SLORTET                links[i].parentNode.removeChild(links[i]);
9*b603bbe1SLORTET            }
10*b603bbe1SLORTET        }
11*b603bbe1SLORTET
12*b603bbe1SLORTET        var icon = document.createElement('link');
13*b603bbe1SLORTET        icon.rel = 'icon';
14*b603bbe1SLORTET        icon.href = href;
15*b603bbe1SLORTET        document.head.appendChild(icon);
16*b603bbe1SLORTET
17*b603bbe1SLORTET        var shortcut = document.createElement('link');
18*b603bbe1SLORTET        shortcut.rel = 'shortcut icon';
19*b603bbe1SLORTET        shortcut.href = href;
20*b603bbe1SLORTET        document.head.appendChild(shortcut);
21*b603bbe1SLORTET    }
22*b603bbe1SLORTET
23*b603bbe1SLORTET    document.addEventListener('DOMContentLoaded', function () {
24*b603bbe1SLORTET        var markers = document.querySelectorAll('.pagesicon-favicon-runtime[data-href]');
25*b603bbe1SLORTET        if (!markers.length) return;
26*b603bbe1SLORTET
27*b603bbe1SLORTET        var lastMarker = markers[markers.length - 1];
28*b603bbe1SLORTET        replaceFavicon(lastMarker.getAttribute('data-href') || '');
29*b603bbe1SLORTET    });
30*b603bbe1SLORTET})();
31