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; | ||
const metaDesc = | |||
const | document.querySelector('meta[name="description"]')?.content || ""; | ||
const pageUrl = location.href; | |||
const | let schema = null; | ||
const | function inject(schemaObj) { | ||
const script = document.createElement('script'); | |||
script.type = 'application/ld+json'; | |||
script.text = JSON.stringify(schemaObj); | |||
document.head.appendChild(script); | |||
} | |||
/* COMPANY / BRAND */ | |||
if (document.querySelector('.infobox.company')) { | |||
const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle'); | |||
const logo = document.querySelector('.infobox img')?.src || null; | |||
schema = { | |||
"@context": "https://schema.org", | |||
"@type": "ProfilePage", | |||
"mainEntity": { | |||
"@type": "Organization", | |||
"name": name, | |||
"url": pageUrl, | |||
"description": metaDesc, | |||
"address": { "@type": "PostalAddress", "addressCountry": "IN" } | |||
} | |||
}; | |||
if (logo) schema.mainEntity.logo = logo; | |||
} | |||
/* PERSON / BIRTHDAY */ | |||
else if (document.querySelector('.infobox.person')) { | |||
const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle'); | |||
const image = document.querySelector('.infobox img')?.src || null; | |||
schema = { | |||
"@context": "https://schema.org", | |||
"@type": "ProfilePage", | |||
"mainEntity": { | |||
"@type": "Person", | |||
"name": name, | |||
"url": pageUrl, | |||
"description": metaDesc | |||
} | } | ||
} | }; | ||
} | |||
if (image) schema.mainEntity.image = image; | |||
} | |||
if ( | /* MOVIE / FILM */ | ||
schema.mainEntity. | else if (document.querySelector('.infobox.film')) { | ||
const title = document.querySelector('.infobox .summary')?.innerText || mw.config.get('wgTitle'); | |||
const poster = document.querySelector('.infobox img')?.src || null; | |||
schema = { | |||
"@context": "https://schema.org", | |||
"@type": "ProfilePage", | |||
"mainEntity": { | |||
"@type": "Movie", | |||
"name": title, | |||
"url": pageUrl, | |||
"description": metaDesc | |||
} | |||
}; | |||
if (poster) schema.mainEntity.image = poster; | |||
} | |||
/* PLACE */ | |||
else if (document.querySelector('.infobox.place')) { | |||
const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle'); | |||
schema = { | |||
"@context": "https://schema.org", | |||
"@type": "ProfilePage", | |||
"mainEntity": { | |||
"@type": "Place", | |||
"name": name, | |||
"url": pageUrl, | |||
"description": metaDesc | |||
} | |||
}; | |||
} | } | ||
if (schema) inject(schema); | |||
}); | }); | ||
Revision as of 19:03, 17 December 2025
mw.loader.using('mediawiki.util', function () {
if (mw.config.get('wgNamespaceNumber') !== 0) return;
const metaDesc =
document.querySelector('meta[name="description"]')?.content || "";
const pageUrl = location.href;
let schema = null;
function inject(schemaObj) {
const script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(schemaObj);
document.head.appendChild(script);
}
/* COMPANY / BRAND */
if (document.querySelector('.infobox.company')) {
const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
const logo = document.querySelector('.infobox img')?.src || null;
schema = {
"@context": "https://schema.org",
"@type": "ProfilePage",
"mainEntity": {
"@type": "Organization",
"name": name,
"url": pageUrl,
"description": metaDesc,
"address": { "@type": "PostalAddress", "addressCountry": "IN" }
}
};
if (logo) schema.mainEntity.logo = logo;
}
/* PERSON / BIRTHDAY */
else if (document.querySelector('.infobox.person')) {
const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
const image = document.querySelector('.infobox img')?.src || null;
schema = {
"@context": "https://schema.org",
"@type": "ProfilePage",
"mainEntity": {
"@type": "Person",
"name": name,
"url": pageUrl,
"description": metaDesc
}
};
if (image) schema.mainEntity.image = image;
}
/* MOVIE / FILM */
else if (document.querySelector('.infobox.film')) {
const title = document.querySelector('.infobox .summary')?.innerText || mw.config.get('wgTitle');
const poster = document.querySelector('.infobox img')?.src || null;
schema = {
"@context": "https://schema.org",
"@type": "ProfilePage",
"mainEntity": {
"@type": "Movie",
"name": title,
"url": pageUrl,
"description": metaDesc
}
};
if (poster) schema.mainEntity.image = poster;
}
/* PLACE */
else if (document.querySelector('.infobox.place')) {
const name = document.querySelector('.infobox .fn')?.innerText || mw.config.get('wgTitle');
schema = {
"@context": "https://schema.org",
"@type": "ProfilePage",
"mainEntity": {
"@type": "Place",
"name": name,
"url": pageUrl,
"description": metaDesc
}
};
}
if (schema) inject(schema);
});