2020-03-24 00:52:08 +07:00
|
|
|
import { Injectable } from '@angular/core';
|
2020-11-20 00:59:10 +09:00
|
|
|
import { Title, Meta } from '@angular/platform-browser';
|
2023-03-09 02:34:21 -06:00
|
|
|
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
|
|
|
|
import { filter, map, switchMap } from 'rxjs';
|
2020-05-09 20:37:50 +07:00
|
|
|
import { StateService } from './state.service';
|
2020-03-24 00:52:08 +07:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class SeoService {
|
2020-05-09 20:37:50 +07:00
|
|
|
network = '';
|
2023-02-28 18:40:59 -06:00
|
|
|
baseTitle = 'mempool';
|
2024-01-21 11:19:01 +00:00
|
|
|
baseDescription = 'Explore the full Bitcoin ecosystem® with The Mempool Open Source Project®.';
|
2024-05-15 20:57:29 +00:00
|
|
|
baseDomain = 'mempool.space';
|
2020-03-24 00:52:08 +07:00
|
|
|
|
2024-05-15 20:57:29 +00:00
|
|
|
canonicalLink: HTMLLinkElement = document.getElementById('canonical') as HTMLLinkElement;
|
2023-08-22 02:54:23 +09:00
|
|
|
|
2020-03-24 00:52:08 +07:00
|
|
|
constructor(
|
|
|
|
private titleService: Title,
|
2020-11-20 00:59:10 +09:00
|
|
|
private metaService: Meta,
|
2020-05-09 20:37:50 +07:00
|
|
|
private stateService: StateService,
|
2023-03-09 02:34:21 -06:00
|
|
|
private router: Router,
|
|
|
|
private activatedRoute: ActivatedRoute,
|
2020-05-09 20:37:50 +07:00
|
|
|
) {
|
2024-05-15 20:57:29 +00:00
|
|
|
// save original meta tags
|
|
|
|
this.baseDescription = metaService.getTag('name=\'description\'')?.content || this.baseDescription;
|
|
|
|
this.baseTitle = titleService.getTitle()?.split(' - ')?.[0] || this.baseTitle;
|
|
|
|
try {
|
|
|
|
const canonicalUrl = new URL(this.canonicalLink?.href || '');
|
|
|
|
this.baseDomain = canonicalUrl?.host;
|
|
|
|
} catch (e) {
|
|
|
|
// leave as default
|
|
|
|
}
|
|
|
|
|
2020-05-09 20:37:50 +07:00
|
|
|
this.stateService.networkChanged$.subscribe((network) => this.network = network);
|
2023-03-09 02:34:21 -06:00
|
|
|
this.router.events.pipe(
|
|
|
|
filter(event => event instanceof NavigationEnd),
|
|
|
|
map(() => this.activatedRoute),
|
|
|
|
map(route => {
|
|
|
|
while (route.firstChild) route = route.firstChild;
|
|
|
|
return route;
|
|
|
|
}),
|
|
|
|
filter(route => route.outlet === 'primary'),
|
|
|
|
switchMap(route => route.data),
|
|
|
|
).subscribe((data) => {
|
|
|
|
this.clearSoft404();
|
|
|
|
});
|
2020-05-09 20:37:50 +07:00
|
|
|
}
|
2020-03-24 00:52:08 +07:00
|
|
|
|
2020-08-12 14:04:04 +07:00
|
|
|
setTitle(newTitle: string): void {
|
|
|
|
this.titleService.setTitle(newTitle + ' - ' + this.getTitle());
|
2020-11-20 00:59:10 +09:00
|
|
|
this.metaService.updateTag({ property: 'og:title', content: newTitle});
|
2024-03-10 09:17:32 +09:00
|
|
|
this.metaService.updateTag({ name: 'twitter:title', content: newTitle});
|
2022-08-02 00:37:54 +00:00
|
|
|
this.metaService.updateTag({ property: 'og:meta:ready', content: 'ready'});
|
2020-03-24 00:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-08-12 14:04:04 +07:00
|
|
|
resetTitle(): void {
|
|
|
|
this.titleService.setTitle(this.getTitle());
|
2020-11-20 00:59:10 +09:00
|
|
|
this.metaService.updateTag({ property: 'og:title', content: this.getTitle()});
|
2024-03-10 09:17:32 +09:00
|
|
|
this.metaService.updateTag({ name: 'twitter:title', content: this.getTitle()});
|
2022-08-02 00:37:54 +00:00
|
|
|
this.metaService.updateTag({ property: 'og:meta:ready', content: 'ready'});
|
2020-08-12 14:04:04 +07:00
|
|
|
}
|
|
|
|
|
2024-04-27 20:39:53 +00:00
|
|
|
setEnterpriseTitle(title: string, override: boolean = false) {
|
|
|
|
if (override) {
|
|
|
|
this.baseTitle = title;
|
|
|
|
} else {
|
|
|
|
this.baseTitle = title + ' - ' + this.baseTitle;
|
|
|
|
}
|
2022-07-21 19:58:12 +02:00
|
|
|
this.resetTitle();
|
|
|
|
}
|
|
|
|
|
2023-08-26 14:18:55 +09:00
|
|
|
setDescription(newDescription: string): void {
|
|
|
|
this.metaService.updateTag({ name: 'description', content: newDescription});
|
|
|
|
this.metaService.updateTag({ name: 'twitter:description', content: newDescription});
|
|
|
|
this.metaService.updateTag({ property: 'og:description', content: newDescription});
|
|
|
|
}
|
|
|
|
|
2023-08-28 13:10:08 +09:00
|
|
|
resetDescription(): void {
|
|
|
|
this.metaService.updateTag({ name: 'description', content: this.getDescription()});
|
|
|
|
this.metaService.updateTag({ name: 'twitter:description', content: this.getDescription()});
|
|
|
|
this.metaService.updateTag({ property: 'og:description', content: this.getDescription()});
|
|
|
|
}
|
|
|
|
|
2023-08-22 02:54:23 +09:00
|
|
|
updateCanonical(path) {
|
2024-05-15 20:57:29 +00:00
|
|
|
this.canonicalLink.setAttribute('href', 'https://' + this.baseDomain + path);
|
2023-08-22 02:54:23 +09:00
|
|
|
}
|
|
|
|
|
2020-08-12 14:04:04 +07:00
|
|
|
getTitle(): string {
|
2022-01-05 15:58:15 +09:00
|
|
|
if (this.network === 'testnet')
|
2024-05-06 15:40:32 +00:00
|
|
|
return this.baseTitle + ' - Bitcoin Testnet3';
|
|
|
|
if (this.network === 'testnet4')
|
|
|
|
return this.baseTitle + ' - Bitcoin Testnet4';
|
2022-01-05 15:58:15 +09:00
|
|
|
if (this.network === 'signet')
|
2022-07-21 19:58:12 +02:00
|
|
|
return this.baseTitle + ' - Bitcoin Signet';
|
2021-08-05 22:45:03 +09:00
|
|
|
if (this.network === 'liquid')
|
2022-07-21 19:58:12 +02:00
|
|
|
return this.baseTitle + ' - Liquid Network';
|
2021-12-27 22:54:45 +04:00
|
|
|
if (this.network === 'liquidtestnet')
|
2022-07-21 19:58:12 +02:00
|
|
|
return this.baseTitle + ' - Liquid Testnet';
|
|
|
|
return this.baseTitle + ' - ' + (this.network ? this.ucfirst(this.network) : 'Bitcoin') + ' Explorer';
|
2020-08-12 14:04:04 +07:00
|
|
|
}
|
|
|
|
|
2023-08-28 13:10:08 +09:00
|
|
|
getDescription(): string {
|
2024-05-15 20:57:29 +00:00
|
|
|
return this.baseDescription;
|
2023-08-28 13:10:08 +09:00
|
|
|
}
|
|
|
|
|
2020-08-12 14:04:04 +07:00
|
|
|
ucfirst(str: string) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
2020-03-24 00:52:08 +07:00
|
|
|
}
|
2023-03-09 02:34:21 -06:00
|
|
|
|
|
|
|
clearSoft404() {
|
|
|
|
window['soft404'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
logSoft404() {
|
|
|
|
window['soft404'] = true;
|
|
|
|
}
|
2020-03-24 00:52:08 +07:00
|
|
|
}
|