1jQuery(document).ready(function () { 2 console.log('linkfavicon'); 3 const faviconCache = {}; 4 const ICON_NOT_FOUND = -1; 5 const ICON_LOADING = 0; 6 const ICON_LOADED = 1; 7 8 function load(src) { 9 return new Promise(function (resolve, reject) { 10 const image = new Image(); 11 image.addEventListener('load', resolve); 12 image.addEventListener('error', reject); 13 image.src = src; 14 }); 15 } 16 17 function setIcon(faviconUrl) { 18 if (faviconCache[faviconUrl] === ICON_LOADED) { 19 jQuery('[data-linkfavicon="' + faviconUrl + '"]').each(function (idx, el) { 20 el.style.backgroundImage = 'url(' + faviconUrl + ')'; 21 }) 22 } 23 } 24 25 jQuery('[data-linkfavicon]').each(function (idx, el) { 26 const faviconUrl = el.getAttribute('data-linkfavicon'); 27 if (faviconCache[faviconUrl] === undefined) { 28 faviconCache[faviconUrl] = ICON_LOADING; 29 load(faviconUrl).then(function () { 30 faviconCache[faviconUrl] = ICON_LOADED; 31 setIcon(faviconUrl); 32 }).catch(function () { 33 faviconCache[faviconUrl] = ICON_NOT_FOUND; 34 }); 35 } 36 }) 37 38}); 39