"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
(ചെ.)No edit summary |
No edit summary |
||
| വരി 19: | വരി 19: | ||
} | } | ||
}; | }; | ||
//Load images from Wikimedia commons using template | |||
mw.loader.using(['mediawiki.util']).then(function () { | |||
function loadCommonsImages() { | |||
const elements = document.querySelectorAll('.commons-image'); | |||
elements.forEach(function (el) { | |||
const filename = el.dataset.filename; | |||
if (!filename) return; | |||
const width = el.dataset.width || 300; | |||
const align = el.dataset.align || 'none'; | |||
const caption = el.dataset.caption || ''; | |||
const apiUrl = | |||
'https://commons.wikimedia.org/w/api.php' + | |||
'?action=query' + | |||
'&format=json' + | |||
'&prop=imageinfo' + | |||
'&iiprop=url|extmetadata' + | |||
'&iiurlwidth=' + width + | |||
'&titles=File:' + encodeURIComponent(filename) + | |||
'&origin=*'; | |||
fetch(apiUrl) | |||
.then(response => response.json()) | |||
.then(data => { | |||
const pages = data.query.query.pages; | |||
const page = Object.values(pages)[0]; | |||
if (!page.imageinfo) { | |||
el.textContent = 'Image not found on Wikimedia Commons'; | |||
return; | |||
} | |||
const info = page.imageinfo[0]; | |||
const imgUrl = info.thumburl || info.url; | |||
const artist = | |||
info.extmetadata?.Artist?.value || 'Unknown'; | |||
const license = | |||
info.extmetadata?.LicenseShortName?.value || ''; | |||
const licenseUrl = | |||
info.extmetadata?.LicenseUrl?.value || ''; | |||
const figure = document.createElement('figure'); | |||
figure.style.maxWidth = width + 'px'; | |||
if (align === 'right') figure.style.float = 'right'; | |||
if (align === 'left') figure.style.float = 'left'; | |||
if (align === 'center') figure.style.margin = 'auto'; | |||
const img = document.createElement('img'); | |||
img.src = imgUrl; | |||
img.alt = caption || filename; | |||
img.loading = 'lazy'; | |||
img.style.width = '100%'; | |||
figure.appendChild(img); | |||
if (caption || license) { | |||
const figcaption = document.createElement('figcaption'); | |||
figcaption.style.fontSize = '0.8em'; | |||
figcaption.style.textAlign = 'center'; | |||
figcaption.innerHTML = | |||
caption + | |||
'<br>' + | |||
'© ' + artist + | |||
(licenseUrl | |||
? ' – <a href="' + licenseUrl + '" target="_blank">' + | |||
license + | |||
'</a>' | |||
: ''); | |||
figure.appendChild(figcaption); | |||
} | |||
el.replaceWith(figure); | |||
}) | |||
.catch(() => { | |||
el.textContent = 'Error loading image from Wikimedia Commons'; | |||
}); | |||
}); | |||
} | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', loadCommonsImages); | |||
} else { | |||
loadCommonsImages(); | |||
} | |||
}); | |||
//Quick Edit Box | //Quick Edit Box | ||