"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
No edit summary |
No edit summary |
||
| വരി 239: | വരി 239: | ||
})(); | })(); | ||
/* ============================================================ | |||
Wikimedia Commons Single Image Loader (CommonsImage) | |||
============================================================ */ | |||
mw.hook('wikipage.content').add(function ($content) { | |||
$content.find('.commons-image').each(function () { | |||
var el = this; | |||
var filename = el.dataset.filename; | |||
if (!filename) return; | |||
var width = parseInt(el.dataset.width, 10) || 300; | |||
var align = el.dataset.align || 'none'; | |||
var captionText = el.dataset.caption || ''; | |||
var 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(function (r) { | |||
if (!r.ok) throw new Error('HTTP ' + r.status); | |||
return r.json(); | |||
}) | |||
.then(function (data) { | |||
if (!data || !data.query || !data.query.pages) { | |||
el.textContent = 'Error loading image'; | |||
return; | |||
} | |||
var page = | |||
data.query.pages[Object.keys(data.query.pages)[0]]; | |||
if (!page || !page.imageinfo) { | |||
el.textContent = 'Image not found'; | |||
return; | |||
} | |||
var info = page.imageinfo[0]; | |||
var imgUrl = info.thumburl || info.url; | |||
var artist = 'Unknown'; | |||
var license = ''; | |||
var licenseUrl = ''; | |||
if (info.extmetadata) { | |||
if (info.extmetadata.Artist) { | |||
artist = info.extmetadata.Artist.value; | |||
} | |||
if (info.extmetadata.LicenseShortName) { | |||
license = info.extmetadata.LicenseShortName.value; | |||
} | |||
if (info.extmetadata.LicenseUrl) { | |||
licenseUrl = info.extmetadata.LicenseUrl.value; | |||
} | |||
} | |||
var 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.marginLeft = 'auto'; | |||
figure.style.marginRight = 'auto'; | |||
} | |||
var img = document.createElement('img'); | |||
img.src = imgUrl; | |||
img.alt = captionText || filename; | |||
img.loading = 'lazy'; | |||
img.style.width = '100%'; | |||
figure.appendChild(img); | |||
if (captionText || license) { | |||
var figcaption = document.createElement('figcaption'); | |||
figcaption.style.fontSize = '0.8em'; | |||
figcaption.style.textAlign = 'center'; | |||
figcaption.innerHTML = | |||
(captionText ? captionText + '<br>' : '') + | |||
'© ' + artist + | |||
(licenseUrl | |||
? ' – <a href="' + licenseUrl + | |||
'" target="_blank">' + license + '</a>' | |||
: ''); | |||
figure.appendChild(figcaption); | |||
} | |||
el.replaceWith(figure); | |||
}) | |||
.catch(function () { | |||
el.textContent = 'Error loading image from Commons'; | |||
}); | |||
}); | |||
}); | |||