2024-06-15 12:36:55 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: leah and other Sharkey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2024-05-06 13:59:42 +02:00
|
|
|
import tinycolor from 'tinycolor2';
|
|
|
|
|
2024-04-02 23:30:14 +02:00
|
|
|
class FavIconDot {
|
2024-06-17 17:44:00 +02:00
|
|
|
private readonly canvas: HTMLCanvasElement;
|
|
|
|
private src: string | null = null;
|
|
|
|
private ctx: CanvasRenderingContext2D | null = null;
|
|
|
|
private faviconImage: HTMLImageElement | null = null;
|
|
|
|
private faviconEL: HTMLLinkElement | undefined;
|
|
|
|
private 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-06 13:54:43 +02:00
|
|
|
/**
|
|
|
|
* Must be called before calling any other functions
|
|
|
|
*/
|
2024-05-02 17:22:30 +02:00
|
|
|
public async setup() {
|
2024-05-06 13:54:43 +02:00
|
|
|
const element: HTMLLinkElement = await this.getOrMakeFaviconElement();
|
2024-06-15 12:36:55 +02:00
|
|
|
|
2024-05-02 17:22:30 +02:00
|
|
|
this.faviconEL = element;
|
2024-04-02 23:30:14 +02:00
|
|
|
this.src = this.faviconEL.getAttribute('href');
|
|
|
|
this.ctx = this.canvas.getContext('2d');
|
2024-06-15 12:36:55 +02:00
|
|
|
|
2024-05-02 17:22:30 +02:00
|
|
|
this.faviconImage = document.createElement('img');
|
2024-06-15 12:36:55 +02:00
|
|
|
|
2024-05-02 17:22:30 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-06 13:54:43 +02:00
|
|
|
private async getOrMakeFaviconElement(): Promise<HTMLLinkElement> {
|
2024-05-02 17:22:30 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2024-05-06 13:54:43 +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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:54:43 +02:00
|
|
|
private createFaviconElem() {
|
2024-04-02 23:30:14 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:54:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-06 13:54:43 +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-05-06 13:59:42 +02:00
|
|
|
const computedStyle = getComputedStyle(document.documentElement);
|
|
|
|
this.ctx.fillStyle = tinycolor(computedStyle.getPropertyValue('--navIndicator')).toHexString();
|
2024-04-02 23:30:14 +02:00
|
|
|
this.ctx.strokeStyle = 'white';
|
|
|
|
this.ctx.fill();
|
|
|
|
this.ctx.stroke();
|
|
|
|
}
|
|
|
|
|
2024-05-06 13:54:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-17 17:44:00 +02:00
|
|
|
public async setVisible(isVisible: boolean) {
|
2024-05-06 13:54:43 +02:00
|
|
|
// Wait for it to have loaded the icon
|
2024-04-02 23:30:14 +02:00
|
|
|
await this.hasLoaded;
|
2024-05-06 13:54:43 +02:00
|
|
|
this.drawIcon();
|
|
|
|
if (isVisible) this.drawDot();
|
|
|
|
this.setFavicon();
|
2024-04-02 23:30:14 +02:00
|
|
|
}
|
2024-06-17 17:44:00 +02:00
|
|
|
|
|
|
|
public async worksOnInstance() {
|
|
|
|
try {
|
|
|
|
// Wait for it to have loaded the icon
|
|
|
|
await this.hasLoaded;
|
|
|
|
this.drawIcon();
|
|
|
|
this.drawDot();
|
|
|
|
this.canvas.toDataURL('image/png');
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2024-04-02 23:30:14 +02:00
|
|
|
}
|
|
|
|
|
2024-05-02 17:22:30 +02:00
|
|
|
let icon: FavIconDot | undefined = undefined;
|
2024-04-02 23:30:14 +02:00
|
|
|
|
2024-06-17 17:44:00 +02:00
|
|
|
export async function setFavIconDot(visible: boolean) {
|
2024-05-02 17:22:30 +02:00
|
|
|
const setIconVisibility = async () => {
|
|
|
|
if (!icon) {
|
|
|
|
icon = new FavIconDot();
|
|
|
|
await icon.setup();
|
|
|
|
}
|
2024-06-15 12:36:55 +02:00
|
|
|
|
2024-06-17 17:44:00 +02:00
|
|
|
try {
|
|
|
|
(icon as FavIconDot).setVisible(visible);
|
|
|
|
} catch (error) {
|
|
|
|
//Probably failed due to CORS and a dirty canvas
|
|
|
|
}
|
2024-05-02 17:22:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// If document is already loaded, set visibility immediately
|
|
|
|
if (document.readyState === 'complete') {
|
2024-06-17 17:44:00 +02:00
|
|
|
await setIconVisibility();
|
2024-05-02 17:22:30 +02:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
2024-06-17 17:44:00 +02:00
|
|
|
|
|
|
|
export async function worksOnInstance() {
|
|
|
|
if (!icon) {
|
|
|
|
icon = new FavIconDot();
|
|
|
|
await icon.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
return await icon.worksOnInstance();
|
|
|
|
}
|