MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
console.log("BharatWiki Common.js loaded"); | console.log("BharatWiki Common.js loaded"); | ||
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 infobox = document.querySelector('.infobox'); | |||
if (!infobox) return; | |||
const metaDesc = | const metaDesc = | ||
| Line 8: | Line 12: | ||
const pageUrl = location.href; | 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 entityType = null; | |||
if (categories.some(c => c.includes('companies') || c.includes('agency'))) { | |||
entityType = "Organization"; | |||
} else if (categories.some(c => c.includes('people') || c.includes('birth'))) { | |||
entityType = "Person"; | |||
} else if (categories.some(c => c.includes('films') || c.includes('movies'))) { | |||
entityType = "Movie"; | |||
} else if (categories.some(c => c.includes('places'))) { | |||
entityType = "Place"; | |||
} | } | ||
if (!entityType) return; | |||
const image = infobox.querySelector('img')?.src || null; | |||
/ | const schema = { | ||
"@context": "https://schema.org", | |||
"@type": "ProfilePage", | |||
"mainEntity": { | |||
"@type": entityType, | |||
"name": pageTitle, | |||
"url": pageUrl, | |||
"description": metaDesc | |||
} | |||
}; | |||
if (image) { | |||
if (entityType === "Organization") { | |||
schema.mainEntity.logo = image; | |||
} else { | |||
schema.mainEntity.image = image; | |||
} | |||
} | |||
} | } | ||
const script = document.createElement('script'); | |||
script.type = 'application/ld+json'; | |||
script.text = JSON.stringify(schema); | |||
document.head.appendChild(script); | |||
}); | }); | ||
Revision as of 19:09, 17 December 2025
console.log("BharatWiki Common.js loaded");
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 entityType = null;
if (categories.some(c => c.includes('companies') || c.includes('agency'))) {
entityType = "Organization";
} else if (categories.some(c => c.includes('people') || c.includes('birth'))) {
entityType = "Person";
} else if (categories.some(c => c.includes('films') || c.includes('movies'))) {
entityType = "Movie";
} else if (categories.some(c => c.includes('places'))) {
entityType = "Place";
}
if (!entityType) return;
const image = infobox.querySelector('img')?.src || null;
const schema = {
"@context": "https://schema.org",
"@type": "ProfilePage",
"mainEntity": {
"@type": entityType,
"name": pageTitle,
"url": pageUrl,
"description": metaDesc
}
};
if (image) {
if (entityType === "Organization") {
schema.mainEntity.logo = image;
} else {
schema.mainEntity.image = image;
}
}
const script = document.createElement('script');
script.type = 'application/ld+json';
script.text = JSON.stringify(schema);
document.head.appendChild(script);
});