MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
mw.loader.using('mediawiki.util', function () { | mw.loader.using('mediawiki.util', function () { | ||
if (mw.config.get('wgNamespaceNumber') !== 0) return; | if (mw.config.get('wgNamespaceNumber') !== 0) return; | ||
| Line 17: | Line 16: | ||
).map(a => a.innerText.toLowerCase()); | ).map(a => a.innerText.toLowerCase()); | ||
let | let schema = null; | ||
// COMPANY | |||
if (categories.some(c => c.includes('companies') || c.includes('agency'))) { | if (categories.some(c => c.includes('companies') || c.includes('agency'))) { | ||
schema = { | |||
"@context": "https://schema.org", | |||
"@type": "Organization", | |||
"name": pageTitle, | |||
"url": pageUrl, | |||
"description": metaDesc | |||
}; | |||
const logo = infobox.querySelector('img')?.src; | |||
if (logo) schema.logo = logo; | |||
} | } | ||
const image = infobox.querySelector('img')?.src | // PERSON | ||
else if (categories.some(c => c.includes('people') || c.includes('birth'))) { | |||
schema = { | |||
"@context": "https://schema.org", | |||
"@type": "Person", | |||
"name": pageTitle, | |||
"url": pageUrl, | |||
"description": metaDesc | |||
}; | |||
const image = infobox.querySelector('img')?.src; | |||
if (image) schema.image = image; | |||
} | |||
// MOVIE | |||
else if (categories.some(c => c.includes('films') || c.includes('movies'))) { | |||
schema = { | |||
"@context": "https://schema.org", | |||
"@type": "Movie", | |||
"name": pageTitle, | "name": pageTitle, | ||
"url": pageUrl, | "url": pageUrl, | ||
"description": metaDesc | "description": metaDesc | ||
} | }; | ||
} | const poster = infobox.querySelector('img')?.src; | ||
if (poster) schema.image = poster; | |||
} | |||
if ( | if (!schema) return; | ||
const script = document.createElement('script'); | const script = document.createElement('script'); | ||
Revision as of 19:17, 17 December 2025
mw.loader.using('mediawiki.util', function () {
if (mw.config.get('wgNamespaceNumber') !== 0) return;
const infobox = document.querySelector('.infobox');
if (!infobox) return;
const metaDesc =
document.querySelector('meta[name="description"]')?.content || "";
const pageUrl = location.href;
const pageTitle = mw.config.get('wgTitle');
const categories = Array.from(
document.querySelectorAll('#mw-normal-catlinks a')
).map(a => a.innerText.toLowerCase());
let schema = null;
// COMPANY
if (categories.some(c => c.includes('companies') || c.includes('agency'))) {
schema = {
"@context": "https://schema.org",
"@type": "Organization",
"name": pageTitle,
"url": pageUrl,
"description": metaDesc
};
const logo = infobox.querySelector('img')?.src;
if (logo) schema.logo = logo;
}
// PERSON
else if (categories.some(c => c.includes('people') || c.includes('birth'))) {
schema = {
"@context": "https://schema.org",
"@type": "Person",
"name": pageTitle,
"url": pageUrl,
"description": metaDesc
};
const image = infobox.querySelector('img')?.src;
if (image) schema.image = image;
}
// MOVIE
else if (categories.some(c => c.includes('films') || c.includes('movies'))) {
schema = {
"@context": "https://schema.org",
"@type": "Movie",
"name": pageTitle,
"url": pageUrl,
"description": metaDesc
};
const poster = infobox.querySelector('img')?.src;
if (poster) schema.image = poster;
}
if (!schema) return;
const script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(schema);
document.head.appendChild(script);
});