Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(favicons): cache shows wrong Google Docs icons #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions src/modules/favIcons/favIcons.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { doc, globals } from '../globals';
// import { stopLinksObserver } from '../linksObserver/linksObserver';
import { getPropsByPageName } from '../pageIcons/queries';
import { getBase64FromUrl, isNeedLowContrastFix } from '../utils';
import { getBase64FromUrlWithCache, isNeedLowContrastFix } from '../utils';

import './favIcons.css';

Expand Down Expand Up @@ -34,20 +34,9 @@ const setIconToExtItem = async (extLinkItem: HTMLAnchorElement) => {
format: 'img',
src: ''
};
const { hostname } = new URL(url);
if (!hostname) {
// skip cache for strange URIs
faviconData = await getFaviconData(url);
} else {
if (globals.favIconsCache.has(hostname)) {
// try from cache
faviconData = globals.favIconsCache.get(hostname);
} else {
// no? get fresh + save to cache
faviconData = await getFaviconData(url);
globals.favIconsCache.set(hostname, faviconData);
}
}

faviconData = await getFaviconData(url);

if (faviconData.format === 'img') {
// use IMG
const fav = doc.createElement('img');
Expand Down Expand Up @@ -107,31 +96,32 @@ const getFaviconData = async (url: string): Promise<favRecord> => {
if (hostname === 'youtu.be') {
return favIcon = {
format: 'img',
src: await getBase64FromUrl(`https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://youtube.com&size=32`)
src: await getBase64FromUrlWithCache(`https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://youtube.com&size=32`)
};

}
if (hostname === 'gmail.com' || hostname === 'mail.google.com') {
return favIcon = {
format: 'img',
src: await getBase64FromUrl(`https://ssl.gstatic.com/ui/v1/icons/mail/rfr/gmail.ico`)
src: await getBase64FromUrlWithCache(`https://ssl.gstatic.com/ui/v1/icons/mail/rfr/gmail.ico`)
}
}
if (url.includes('docs.google.com/document')) {
return favIcon = {
format: 'img',
src: await getBase64FromUrl(`https://ssl.gstatic.com/docs/documents/images/kix-favicon7.ico`)
src: await getBase64FromUrlWithCache(`https://ssl.gstatic.com/docs/documents/images/kix-favicon7.ico`)
}
}
if (url.includes('docs.google.com/spreadsheets')) {
return favIcon = {
format: 'img',
src: await getBase64FromUrl(`https://ssl.gstatic.com/docs/spreadsheets/favicon3.ico`)
src: await getBase64FromUrlWithCache(`https://ssl.gstatic.com/docs/spreadsheets/favicon3.ico`)
}
}
if (url.includes('docs.google.com/presentation')) {
return favIcon = {
format: 'img',
src: await getBase64FromUrl(`https://ssl.gstatic.com/docs/presentations/images/favicon5.ico`)
src: await getBase64FromUrlWithCache(`https://ssl.gstatic.com/docs/presentations/images/favicon5.ico`)
}
}
if (url.includes('.atlassian.net/jira/') || url.includes('.atlassian.net/browse/')) {
Expand All @@ -144,13 +134,13 @@ const getFaviconData = async (url: string): Promise<favRecord> => {
if (protocol === 'http:' || protocol === 'https:') {
favIcon = {
format: 'img',
src: await getBase64FromUrl(`https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://${hostname}&size=32`)
src: await getBase64FromUrlWithCache(`https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://${hostname}&size=32`)
}
if (favIcon.src === 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsSAAALEgHS3X78AAACiElEQVQ4EaVTzU8TURCf2tJuS7tQtlRb6UKBIkQwkRRSEzkQgyEc6lkOKgcOph78Y+CgjXjDs2i44FXY9AMTlQRUELZapVlouy3d7kKtb0Zr0MSLTvL2zb75eL838xtTvV6H/xELBptMJojeXLCXyobnyog4YhzXYvmCFi6qVSfaeRdXdrfaU1areV5KykmX06rcvzumjY/1ggkR3Jh+bNf1mr8v1D5bLuvR3qDgFbvbBJYIrE1mCIoCrKxsHuzK+Rzvsi29+6DEbTZz9unijEYI8ObBgXOzlcrx9OAlXyDYKUCzwwrDQx1wVDGg089Dt+gR3mxmhcUnaWeoxwMbm/vzDFzmDEKMMNhquRqduT1KwXiGt0vre6iSeAUHNDE0d26NBtAXY9BACQyjFusKuL2Ry+IPb/Y9ZglwuVscdHaknUChqLF/O4jn3V5dP4mhgRJgwSYm+gV0Oi3XrvYB30yvhGa7BS70eGFHPoTJyQHhMK+F0ZesRVVznvXw5Ixv7/C10moEo6OZXbWvlFAF9FVZDOqEABUMRIkMd8GnLwVWg9/RkJF9sA4oDfYQAuzzjqzwvnaRUFxn/X2ZlmGLXAE7AL52B4xHgqAUqrC1nSNuoJkQtLkdqReszz/9aRvq90NOKdOS1nch8TpL555WDp49f3uAMXhACRjD5j4ykuCtf5PP7Fm1b0DIsl/VHGezzP1KwOiZQobFF9YyjSRYQETRENSlVzI8iK9mWlzckpSSCQHVALmN9Az1euDho9Xo8vKGd2rqooA8yBcrwHgCqYR0kMkWci08t/R+W4ljDCanWTg9TJGwGNaNk3vYZ7VUdeKsYJGFNkfSzjXNrSX20s4/h6kB81/271ghG17l+rPTAAAAAElFTkSuQmCC') {
// is common ugly icon?
favIcon = {
format: 'img',
src: await getBase64FromUrl(`https://icons.duckduckgo.com/ip3/${hostname}`)
src: await getBase64FromUrlWithCache(`https://icons.duckduckgo.com/ip3/${hostname}`)
}
if (favIcon.src === 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyhpVFh0WE1MOmNvbS5hZG9iZS54bX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/PjGq5lQAAAI0SURBVHja1Jo9jsIwEIW9Fh17gBxgD0B63EPPBXIBDkAP/Ub0XIAeeuiTA3CAHID061mNkWXlPzN28qQRQojkffZkPEn8dT6fBYFiHSv8jDCqVGBkOnL8HKXFSNNbHUrHsuN/DNwKv5c6HjpuQ2EWA40nlokxAvANBszIpS9IHwAYub2OteARDMivjqeOFFOtVbLjwRWODpd5W2s8l6ICgFE/9shzQZRaRzz3KICDjp0Ipx16GARwwIsrtDZNELIhbaZg3obYdwVQgdOmKZ1UG0DUlnOBdXBXeVmROssJAyzdVJLOCktV51867ozrRFy1EieE5mGU3tYFSK3EtBzS6SapzZ+YZsJ0vh+ALYN5wQyxtQEUEcC75jcOCGUAYqLKs2kpwdQQ4DmWRLkfCmIl7ZI0Q4j/GYh8N2CEEBEXgC+ISApesUNwA7BD+ABglQ+AO44yy52fnLN5A1DM1Tx45wLwYf4DkM3UPCgDgHym5kG5mYFyhuZLMwOgB8EBf3R8ezL/8WwAbkQAaQUE1xO+mw2QEV0LLgSX+dy9qQddiA5uIDifrV6qVmIgehJCcJl/2qXfbSVSoorEpRI91vZCRUspDK2T2znImvJ0naD5a1W5r+tGU8H3bHPoIpn2badPE4FoXOFlh5y7Bk6bxmuyy3viFMsW1HVf7w5KNN7a4nS9I4MDJYTrRFudT7r2Z33e1Bc4C5RbDdz2gHWrgb1iZ2LYZo+qVPG+2cMFESLgdps/AQYA9D2Sc4DqpGYAAAAASUVORK5CYII=') {
favIcon = {
Expand Down
9 changes: 9 additions & 0 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export const checkUpdate = async () => {
}
}

export const getBase64FromUrlWithCache = async (url: string): Promise<string> => {
if (globals.favIconsCache[url]) {
return globals.favIconsCache[url];
}
const favicon = await getBase64FromUrl(url);
globals.favIconsCache[url] = favicon;
return favicon;
}

// Generate Base64 from image URL
export const getBase64FromUrl = async (url: string): Promise<string> => {
let data;
Expand Down