feat: SEO-Injektor für Meta-Tags, Open Graph und JSON-LD
This commit is contained in:
1 parent
16b8b4a4d0
commit
5c295621a2
1 file changed
+162
@@ -0,0 +1,162 @@
|
||||
// SEO-Injektor: setzt Title, Meta-Tags, Open Graph und JSON-LD.
|
||||
// Läuft auf DOMContentLoaded, idempotent via data-seo-injected="true".
|
||||
(function () {
|
||||
'use strict';
|
||||
try {
|
||||
if (document.documentElement.dataset.seoInjected === 'true') return;
|
||||
var cfg = window.SEO_CONFIG;
|
||||
if (!cfg) return;
|
||||
|
||||
function setMeta(name, content, attr) {
|
||||
attr = attr || 'name';
|
||||
if (!content) return;
|
||||
var sel = 'meta[' + attr + '="' + name + '"]';
|
||||
var el = document.head.querySelector(sel);
|
||||
if (!el) {
|
||||
el = document.createElement('meta');
|
||||
el.setAttribute(attr, name);
|
||||
document.head.appendChild(el);
|
||||
}
|
||||
el.setAttribute('content', content);
|
||||
el.dataset.seoManaged = 'true';
|
||||
}
|
||||
|
||||
function setLink(rel, href) {
|
||||
if (!href) return;
|
||||
var el = document.head.querySelector('link[rel="' + rel + '"]');
|
||||
if (!el) {
|
||||
el = document.createElement('link');
|
||||
el.setAttribute('rel', rel);
|
||||
document.head.appendChild(el);
|
||||
}
|
||||
el.setAttribute('href', href);
|
||||
el.dataset.seoManaged = 'true';
|
||||
}
|
||||
|
||||
function injectJsonLd(id, data) {
|
||||
if (document.getElementById(id)) return;
|
||||
var s = document.createElement('script');
|
||||
s.type = 'application/ld+json';
|
||||
s.id = id;
|
||||
s.textContent = JSON.stringify(data);
|
||||
document.head.appendChild(s);
|
||||
}
|
||||
|
||||
// Pfad normalisieren: trailing slash, .html, /index entfernen
|
||||
var path = location.pathname.replace(/\/index\.html$/, '').replace(/\.html$/, '').replace(/\/$/, '') || '/';
|
||||
var page = cfg.PAGES[path] || {};
|
||||
var title = page.title || cfg.SITE_NAME;
|
||||
var desc = page.desc || cfg.DEFAULT_DESCRIPTION;
|
||||
var ogImage = cfg.DEFAULT_OG_IMAGE ? cfg.SITE_URL + cfg.DEFAULT_OG_IMAGE : '';
|
||||
var canonical = cfg.SITE_URL + (path === '/' ? '/' : path);
|
||||
|
||||
document.title = title;
|
||||
setMeta('description', desc);
|
||||
setLink('canonical', canonical);
|
||||
|
||||
setMeta('og:title', title, 'property');
|
||||
setMeta('og:description', desc, 'property');
|
||||
setMeta('og:url', canonical, 'property');
|
||||
setMeta('og:type', path === '/' ? 'website' : 'article', 'property');
|
||||
setMeta('og:site_name', cfg.SITE_NAME, 'property');
|
||||
if (ogImage) setMeta('og:image', ogImage, 'property');
|
||||
|
||||
setMeta('twitter:card', 'summary_large_image');
|
||||
setMeta('twitter:title', title);
|
||||
setMeta('twitter:description', desc);
|
||||
if (ogImage) setMeta('twitter:image', ogImage);
|
||||
|
||||
// JSON-LD je nach Seite
|
||||
var biz = cfg.BUSINESS;
|
||||
var b = biz.addressLocality;
|
||||
var r = biz.addressRegion;
|
||||
var co = biz.addressCountry;
|
||||
var phone = biz.telephone;
|
||||
var mail = biz.email;
|
||||
var siteUrl = cfg.SITE_URL;
|
||||
|
||||
function personBusinessGraph() {
|
||||
return {
|
||||
'@context': 'https://schema.org',
|
||||
'@graph': [
|
||||
{
|
||||
'@type': 'Person',
|
||||
name: 'Leon Tonhäuser',
|
||||
jobTitle: 'Sportfotograf',
|
||||
url: siteUrl,
|
||||
email: mail,
|
||||
telephone: phone
|
||||
},
|
||||
{
|
||||
'@type': 'ProfessionalService',
|
||||
name: biz.name,
|
||||
url: siteUrl,
|
||||
telephone: phone,
|
||||
email: mail,
|
||||
address: { '@type': 'PostalAddress', addressLocality: b, addressRegion: r, addressCountry: co },
|
||||
areaServed: biz.areaServed.map(function (n) { return { '@type': 'City', name: n }; }),
|
||||
priceRange: biz.priceRange
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
function breadcrumbListGraph() {
|
||||
var segs = path.split('/').filter(Boolean);
|
||||
var items = [{ '@type': 'ListItem', position: 1, name: 'Startseite', item: siteUrl + '/' }];
|
||||
var acc = '';
|
||||
var labels = { portfolio: 'Portfolio', leistungen: 'Leistungen', 'über-mich': 'Über mich', kontakt: 'Kontakt', blog: 'Blog', archiv: 'Archiv' };
|
||||
for (var i = 0; i < segs.length; i++) {
|
||||
acc += '/' + segs[i];
|
||||
var label = labels[segs[i]] || segs[i].replace(/^portfolio-/, '').replace(/-/g, ' ').replace(/\b\w/g, function (c) { return c.toUpperCase(); });
|
||||
items.push({ '@type': 'ListItem', position: i + 2, name: label, item: siteUrl + acc });
|
||||
}
|
||||
return { '@context': 'https://schema.org', '@type': 'BreadcrumbList', itemListElement: items };
|
||||
}
|
||||
|
||||
function imageObjectGraph() {
|
||||
var img = document.querySelector('img[src*="framerusercontent.com"]');
|
||||
if (!img) return null;
|
||||
var contentUrl = img.src;
|
||||
var loc = path.replace(/^.*?(?:portfolio|archiv)-/, '').replace(/-/g, ' ');
|
||||
return {
|
||||
'@context': 'https://schema.org',
|
||||
'@type': 'ImageObject',
|
||||
contentUrl: contentUrl,
|
||||
creator: { '@type': 'Person', name: 'Leon Tonhäuser' },
|
||||
copyrightHolder: { '@type': 'Person', name: 'Leon Tonhäuser' },
|
||||
contentLocation: { '@type': 'Place', name: loc ? loc.charAt(0).toUpperCase() + loc.slice(1) : 'Niedersachsen' }
|
||||
};
|
||||
}
|
||||
|
||||
function serviceGraph() {
|
||||
return {
|
||||
'@context': 'https://schema.org',
|
||||
'@type': 'Service',
|
||||
serviceType: 'Fotografie',
|
||||
provider: { '@type': 'ProfessionalService', name: biz.name, url: siteUrl },
|
||||
areaServed: biz.areaServed.map(function (n) { return { '@type': 'City', name: n }; }),
|
||||
hasOfferCatalog: {
|
||||
'@type': 'OfferCatalog',
|
||||
name: 'Fotografie-Leistungen',
|
||||
itemListElement: [
|
||||
{ '@type': 'Offer', itemOffered: { '@type': 'Service', name: 'Eventfotografie' } },
|
||||
{ '@type': 'Offer', itemOffered: { '@type': 'Service', name: 'Sportfotografie' } },
|
||||
{ '@type': 'Offer', itemOffered: { '@type': 'Service', name: 'Reportagefotografie' } }
|
||||
]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var schemata = page.jsonld || [];
|
||||
if (schemata.indexOf('person-business') !== -1) injectJsonLd('seo-person-business', personBusinessGraph());
|
||||
if (schemata.indexOf('breadcrumb') !== -1) injectJsonLd('seo-breadcrumb', breadcrumbListGraph());
|
||||
var img = imageObjectGraph();
|
||||
if (schemata.indexOf('imageobject') !== -1 && img) injectJsonLd('seo-image', img);
|
||||
if (schemata.indexOf('service') !== -1) injectJsonLd('seo-service', serviceGraph());
|
||||
|
||||
document.documentElement.dataset.seoInjected = 'true';
|
||||
} catch (e) {
|
||||
/* ponytail: still-inject schlägt fehl → kein DOM-Crash, andere Skripte laufen weiter */
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user