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