2024-04-02 23:30:14 +02:00
|
|
|
class FavIconDot {
|
|
|
|
canvas : HTMLCanvasElement;
|
|
|
|
src : string | null = null;
|
|
|
|
ctx : CanvasRenderingContext2D | null = null;
|
2024-05-02 17:22:30 +02:00
|
|
|
faviconImage : HTMLImageElement | null = null;
|
|
|
|
faviconEL : HTMLLinkElement | undefined;
|
|
|
|
hasLoaded : Promise<void> | undefined;
|
2024-04-02 23:30:14 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.canvas = document.createElement('canvas');
|
2024-05-02 17:22:30 +02:00
|
|
|
}
|
2024-04-02 23:30:14 +02:00
|
|
|
|
2024-05-02 17:22:30 +02:00
|
|
|
//MUST BE CALLED BEFORE CALLING ANY OTHER FUNCTIONS
|
|
|
|
public async setup() {
|
|
|
|
const element : HTMLLinkElement = await this.getOrMakeFaviconElement();
|
|
|
|
|
|
|
|
this.faviconEL = element;
|
2024-04-02 23:30:14 +02:00
|
|
|
this.src = this.faviconEL.getAttribute('href');
|
|
|
|
this.ctx = this.canvas.getContext('2d');
|
2024-05-02 17:22:30 +02:00
|
|
|
|
|
|
|
this.faviconImage = document.createElement('img');
|
|
|
|
|
|
|
|
this.hasLoaded = new Promise((resolve, reject) => {
|
2024-05-03 11:27:41 +02:00
|
|
|
(this.faviconImage as HTMLImageElement).addEventListener('load', () => {
|
2024-05-02 17:22:30 +02:00
|
|
|
this.canvas.width = (this.faviconImage as HTMLImageElement).width;
|
|
|
|
this.canvas.height = (this.faviconImage as HTMLImageElement).height;
|
|
|
|
resolve();
|
2024-05-03 11:27:41 +02:00
|
|
|
});
|
|
|
|
(this.faviconImage as HTMLImageElement).addEventListener('error', () => {
|
2024-05-02 17:22:30 +02:00
|
|
|
reject('Failed to create favicon img element');
|
2024-05-03 11:27:41 +02:00
|
|
|
});
|
2024-05-02 17:22:30 +02:00
|
|
|
});
|
2024-05-02 17:38:16 +02:00
|
|
|
|
|
|
|
this.faviconImage.src = this.faviconEL.href;
|
2024-05-02 17:22:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getOrMakeFaviconElement() : Promise<HTMLLinkElement> {
|
|
|
|
return new Promise((resolve, reject) => {
|
2024-05-03 12:15:47 +02:00
|
|
|
const favicon = (document.querySelector('link[rel=icon]') ?? this._createFaviconElem()) as HTMLLinkElement;
|
2024-05-03 11:27:41 +02:00
|
|
|
favicon.addEventListener('load', () => {
|
2024-05-02 17:38:16 +02:00
|
|
|
resolve(favicon);
|
2024-05-03 11:27:41 +02:00
|
|
|
});
|
2024-05-02 17:22:30 +02:00
|
|
|
|
2024-05-02 17:38:16 +02:00
|
|
|
favicon.onerror = () => {
|
|
|
|
reject('Failed to load favicon');
|
|
|
|
};
|
2024-05-02 17:22:30 +02:00
|
|
|
resolve(favicon);
|
2024-04-02 23:30:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _createFaviconElem() {
|
|
|
|
const newLink = document.createElement('link');
|
2024-05-03 12:20:58 +02:00
|
|
|
newLink.setAttribute('rel', 'icon');
|
|
|
|
newLink.setAttribute('href', '/favicon.ico');
|
|
|
|
newLink.setAttribute('type', 'image/x-icon');
|
|
|
|
|
2024-04-02 23:30:14 +02:00
|
|
|
document.head.appendChild(newLink);
|
|
|
|
return newLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _drawIcon() {
|
2024-05-02 17:22:30 +02:00
|
|
|
if (!this.ctx || !this.faviconImage) return;
|
2024-04-02 23:30:14 +02:00
|
|
|
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
2024-05-02 17:22:30 +02:00
|
|
|
this.ctx.drawImage(this.faviconImage, 0, 0, this.faviconImage.width, this.faviconImage.height);
|
2024-04-02 23:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private _drawDot() {
|
2024-05-02 17:22:30 +02:00
|
|
|
if (!this.ctx || !this.faviconImage) return;
|
2024-04-02 23:30:14 +02:00
|
|
|
this.ctx.beginPath();
|
2024-05-02 17:22:30 +02:00
|
|
|
this.ctx.arc(this.faviconImage.width - 10, 10, 10, 0, 2 * Math.PI);
|
2024-04-02 23:30:14 +02:00
|
|
|
this.ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--navIndicator');
|
|
|
|
this.ctx.strokeStyle = 'white';
|
|
|
|
this.ctx.fill();
|
|
|
|
this.ctx.stroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _setFavicon() {
|
2024-05-02 17:22:30 +02:00
|
|
|
if (this.faviconEL) this.faviconEL.href = this.canvas.toDataURL('image/png');
|
2024-04-02 23:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async setVisible(isVisible : boolean) {
|
|
|
|
//Wait for it to have loaded the icon
|
|
|
|
await this.hasLoaded;
|
|
|
|
this._drawIcon();
|
|
|
|
if (isVisible) this._drawDot();
|
|
|
|
this._setFavicon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 17:22:30 +02:00
|
|
|
let icon: FavIconDot | undefined = undefined;
|
2024-04-02 23:30:14 +02:00
|
|
|
|
|
|
|
export function setFavIconDot(visible: boolean) {
|
2024-05-02 17:22:30 +02:00
|
|
|
const setIconVisibility = async () => {
|
|
|
|
if (!icon) {
|
|
|
|
icon = new FavIconDot();
|
|
|
|
await icon.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
(icon as FavIconDot).setVisible(visible);
|
|
|
|
};
|
|
|
|
|
|
|
|
// If document is already loaded, set visibility immediately
|
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
setIconVisibility();
|
|
|
|
} else {
|
|
|
|
// Otherwise, set visibility when window loads
|
2024-05-03 11:27:41 +02:00
|
|
|
window.addEventListener('load', setIconVisibility);
|
2024-04-02 23:30:14 +02:00
|
|
|
}
|
|
|
|
}
|