106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
import {Inject, Injectable} from '@angular/core';
|
|
import { Title, Meta } from '@angular/platform-browser';
|
|
import { Router } from '@angular/router';
|
|
import { DOCUMENT } from '@angular/common';
|
|
|
|
export interface DiscordButton {
|
|
label: string;
|
|
url: string;
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
|
|
export class SeoService {
|
|
private readonly rootDomain = 'https://itzminey.dev';
|
|
private readonly defaultImage = 'https://itzminey.dev/assets/ogimg.png';
|
|
private readonly defaultThumb = 'https://itzminey.dev/assets/thumb.png';
|
|
private readonly siteColor = '#0095a4';
|
|
|
|
constructor(
|
|
private titleService: Title,
|
|
private metaService: Meta,
|
|
private router: Router,
|
|
@Inject(DOCUMENT) private document: Document
|
|
) {}
|
|
|
|
update(config: { title: string; desc: string; img?: string; suffix?: boolean; buttons?: DiscordButton[];}) {
|
|
const siteName = 'itzMiney';
|
|
const appendSuffix = config.suffix ?? true;
|
|
|
|
const title = appendSuffix && !config.title.includes(siteName)
|
|
? `${config.title} | ${siteName}`
|
|
: config.title;
|
|
|
|
const description = config.desc;
|
|
const image = config.img || this.defaultImage;
|
|
const currentUrl = `${this.rootDomain}${this.router.url}`;
|
|
|
|
this.titleService.setTitle(title );
|
|
|
|
this.metaService.updateTag({ name: 'description', content: description });
|
|
this.metaService.updateTag({ property: 'og:title', content: title });
|
|
this.metaService.updateTag({ property: 'og:description', content: description });
|
|
this.metaService.updateTag({ property: 'og:image', content: image });
|
|
this.metaService.updateTag({ property: 'og:url', content: currentUrl });
|
|
this.metaService.updateTag({ name: 'theme-color', content: this.siteColor });
|
|
|
|
this.injectDiscordComponentEmbed(config.buttons, { title, description, image, currentUrl });
|
|
}
|
|
|
|
private injectDiscordComponentEmbed(buttons?: DiscordButton[], meta?: { title: string; description: string; image: string; currentUrl: string }) {
|
|
const existingScript = this.document.getElementById('discord:component-embed');
|
|
if (existingScript) {
|
|
existingScript.remove();
|
|
}
|
|
|
|
const thumb = meta?.image || this.defaultThumb;
|
|
|
|
const sectionComponent = {
|
|
type: 9,
|
|
components: [
|
|
{
|
|
type: 10,
|
|
content: `### [${meta?.title ?? ''}](${meta?.currentUrl ?? ''})\n${meta?.description ?? ''}`
|
|
}
|
|
],
|
|
accessory: {
|
|
type: 11,
|
|
media: {
|
|
url: thumb
|
|
}
|
|
}
|
|
};
|
|
|
|
const containerComponents: any[] = [sectionComponent];
|
|
|
|
if (buttons && buttons.length > 0) {
|
|
containerComponents.push({
|
|
type: 1,
|
|
components: buttons.map(btn => ({
|
|
type: 2,
|
|
label: btn.label,
|
|
style: 5,
|
|
url: btn.url
|
|
}))
|
|
});
|
|
}
|
|
|
|
const componentPayload = {
|
|
flags: 32768,
|
|
component: {
|
|
type: 17,
|
|
accent_color: parseInt(this.siteColor.replace('#', ''), 16),
|
|
components: containerComponents
|
|
}
|
|
};
|
|
|
|
const script = this.document.createElement('script');
|
|
script.id = 'discord:component-embed';
|
|
script.type = 'application/json';
|
|
script.text = JSON.stringify(componentPayload, null, 2);
|
|
this.document.head.appendChild(script);
|
|
}
|
|
}
|