From a0277b18db1cbd456da1c77883edd7ac04637f02 Mon Sep 17 00:00:00 2001 From: Jeremy Yen Date: Wed, 6 Nov 2024 11:34:45 -0500 Subject: [PATCH] Fix getFavicon --- internal/sha-inverse-challenge.html | 38 +++++++++++++++-------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/internal/sha-inverse-challenge.html b/internal/sha-inverse-challenge.html index b689952..78e31c6 100644 --- a/internal/sha-inverse-challenge.html +++ b/internal/sha-inverse-challenge.html @@ -169,22 +169,25 @@ return selectedMessage; } - function getFaviconUrl() { - // Search for elements in the document - var links = document.getElementsByTagName('link'); - - // Loop through the links to find one with "icon" in the rel attribute - for (var i = 0; i < links.length; i++) { - var rel = links[i].getAttribute('rel'); - if (rel && rel.toLowerCase().indexOf('icon') !== -1) { - return links[i].href; // Return the favicon URL - } - } + function getFavicon(imgElement) { + // Build the base URL in a backward-compatible way + var origin = window.location.protocol + "//" + window.location.host; + var faviconUrl = origin + '/favicon.ico'; + var fallbackSvg = imgElement.src; + var testImage = new Image(); + + testImage.src = faviconUrl + + // Set favicon if it loads successfully + testImage.addEventListener('load', function() { + imgElement.src = faviconUrl; + }); - // If no found, return the default path /favicon.ico - var faviconUrl = window.location.origin + '/favicon.ico'; - console.log('Favicon URL:', faviconUrl); - return faviconUrl; + // Fallback to SVG if favicon fails to load + testImage.addEventListener('error', function() { + console.log('Favicon not found, falling back to SVG'); + imgElement.src = fallbackSvg; + }); } function setDomainName() { @@ -200,8 +203,7 @@ } function setFavicon() { - document.getElementsByClassName("website-favicon")[0].src = - getFaviconUrl(); + getFavicon(document.getElementsByClassName("website-favicon")[0]) }