Skip to content

Commit

Permalink
Update get favicon method
Browse files Browse the repository at this point in the history
  • Loading branch information
tannn-younet committed Oct 12, 2023
1 parent b6ff23f commit 37525c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/Favorites/FavoriteItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class FavoriteItem extends Component {
this.setState({
data: {
...this.state.data,
icon,
icon: icon.toString(),
},
});
}
Expand Down
14 changes: 6 additions & 8 deletions src/util/favicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import isUrl from "is-url";
*/
const parseHtmlSource = (htmlSource) => {
if (htmlSource && htmlSource.length > 0) {
// use a return statement for the error handler to avoid the console warning
// as any error will result in fallback favicon
return new DOMParser().parseFromString(htmlSource, "text/html");
}
};
Expand All @@ -20,10 +18,10 @@ const parseHtmlSource = (htmlSource) => {
*
* @param {HTMLCollectionOf<Element> | undefined} links the HTML links collection
* @param {string} origin the origin URL to be used to build the favicon url
* @returns {string} the first found favicon URL or empty object if none found
* @returns {URL | undefined} the first found favicon URL or empty object if none found
*/
const getFaviconUrlFromLinks = (links, origin) => {
let faviconURL = "";
let faviconURL;

if (links && links.length > 0 && origin) {
for (let i in Array.from(links)) {
Expand All @@ -32,7 +30,7 @@ const getFaviconUrlFromLinks = (links, origin) => {
if (rel && rel.split(" ").includes("icon")) {
const href = link.getAttribute("href");
if (href) {
faviconURL = new URL(href, origin).toString();
faviconURL = new URL(href, origin);
break; //stop loop at first favicon found, same behaviour as browser extension
}
}
Expand All @@ -45,15 +43,15 @@ const getFaviconUrlFromLinks = (links, origin) => {
* Sanitize origin url
*
* @param {string} origin
* @returns {string | undefined}
* @returns {URL | undefined}
*/
const originToUrl = (origin) => {
if (origin) {
try {
const originWithProtocol = isUrl(origin)
? origin
: `https://${origin}`;
return new URL(originWithProtocol).toString();
return new URL(originWithProtocol);
} catch (e) {}
}
};
Expand All @@ -62,7 +60,7 @@ const originToUrl = (origin) => {
* Returns URL for the favicon of the given url
*
* @param {string} origin - String corresponding to website url or domain
* @returns {Promise<string> | undefined} - String corresponding to favicon url or empty string if none found
* @returns {Promise<URL | undefined>} - String corresponding to favicon url or empty string if none found
*/
export const getFaviconURLFromHtml = async (origin) => {
if (!origin || origin === 'null') {
Expand Down

0 comments on commit 37525c2

Please sign in to comment.