* origin/develop: changelong Fix pinned statuses and perhaps some other stuff update changelog Remove title from index.html, title is now from server generated meta changelog mention fix emoji not working in profile field names change method of fix to rounding changelog prevent call to scroll if the value doesn't change because firefox is stupid Update CHANGELOG.md wrap react button icon to a span to fix popover overflow Update CHANGELOG.md to match master change favicon dimensions for high res, add handling when favicon isn't available remove the favicon changes make badge just a ball, make it use theming add favicon badge for unread notifsshigusegubu
commit
d8ac75f58a
@ -0,0 +1,61 @@ |
||||
import { find } from 'lodash' |
||||
|
||||
const createFaviconService = () => { |
||||
let favimg, favcanvas, favcontext, favicon |
||||
const faviconWidth = 128 |
||||
const faviconHeight = 128 |
||||
const badgeRadius = 32 |
||||
|
||||
const initFaviconService = () => { |
||||
const nodes = document.getElementsByTagName('link') |
||||
favicon = find(nodes, node => node.rel === 'icon') |
||||
if (favicon) { |
||||
favcanvas = document.createElement('canvas') |
||||
favcanvas.width = faviconWidth |
||||
favcanvas.height = faviconHeight |
||||
favimg = new Image() |
||||
favimg.src = favicon.href |
||||
favcontext = favcanvas.getContext('2d') |
||||
} |
||||
} |
||||
|
||||
const isImageLoaded = (img) => img.complete && img.naturalHeight !== 0 |
||||
|
||||
const clearFaviconBadge = () => { |
||||
if (!favimg || !favcontext || !favicon) return |
||||
|
||||
favcontext.clearRect(0, 0, faviconWidth, faviconHeight) |
||||
if (isImageLoaded(favimg)) { |
||||
favcontext.drawImage(favimg, 0, 0, favimg.width, favimg.height, 0, 0, faviconWidth, faviconHeight) |
||||
} |
||||
favicon.href = favcanvas.toDataURL('image/png') |
||||
} |
||||
|
||||
const drawFaviconBadge = () => { |
||||
if (!favimg || !favcontext || !favcontext) return |
||||
|
||||
clearFaviconBadge() |
||||
|
||||
const style = getComputedStyle(document.body) |
||||
const badgeColor = `${style.getPropertyValue('--badgeNotification') || 'rgb(240, 100, 100)'}` |
||||
|
||||
if (isImageLoaded(favimg)) { |
||||
favcontext.drawImage(favimg, 0, 0, favimg.width, favimg.height, 0, 0, faviconWidth, faviconHeight) |
||||
} |
||||
favcontext.fillStyle = badgeColor |
||||
favcontext.beginPath() |
||||
favcontext.arc(faviconWidth - badgeRadius, badgeRadius, badgeRadius, 0, 2 * Math.PI, false) |
||||
favcontext.fill() |
||||
favicon.href = favcanvas.toDataURL('image/png') |
||||
} |
||||
|
||||
return { |
||||
initFaviconService, |
||||
clearFaviconBadge, |
||||
drawFaviconBadge |
||||
} |
||||
} |
||||
|
||||
const FaviconService = createFaviconService() |
||||
|
||||
export default FaviconService |
Loading…
Reference in new issue