"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
No edit summary |
No edit summary |
||
| വരി 14: | വരി 14: | ||
} | } | ||
} | } | ||
}); | |||
/* ============================================================ | |||
Wikimedia Commons Gallery Loader (with per-image captions) | |||
============================================================ */ | |||
mw.hook('wikipage.content').add(function ($content) { | |||
$content.find('.commons-gallery').each(function () { | |||
var container = this; | |||
var rawItems = container.dataset.items; | |||
if (!rawItems) return; | |||
var width = parseInt(container.dataset.imageWidth, 10) || 300; | |||
var items = rawItems.split('|').map(function (entry) { | |||
var parts = entry.split('::'); | |||
return { | |||
filename: parts[0] ? parts[0].trim() : '', | |||
caption: parts[1] ? parts.slice(1).join('::').trim() : '' | |||
}; | |||
}).filter(function (i) { | |||
return i.filename; | |||
}); | |||
if (!items.length) return; | |||
var grid = document.createElement('div'); | |||
grid.className = 'commons-gallery-grid'; | |||
grid.style.display = 'grid'; | |||
grid.style.gridTemplateColumns = | |||
'repeat(auto-fill, minmax(' + width + 'px, 1fr))'; | |||
grid.style.gap = '8px'; | |||
container.appendChild(grid); | |||
items.forEach(function (item) { | |||
var apiUrl = | |||
'https://commons.wikimedia.org/w/api.php' + | |||
'?action=query' + | |||
'&format=json' + | |||
'&prop=imageinfo' + | |||
'&iiprop=url|extmetadata' + | |||
'&iiurlwidth=' + width + | |||
'&titles=File:' + encodeURIComponent(item.filename) + | |||
'&origin=*'; | |||
fetch(apiUrl) | |||
.then(function (r) { return r.json(); }) | |||
.then(function (data) { | |||
if (!data.query || !data.query.pages) return; | |||
var page = | |||
data.query.pages[Object.keys(data.query.pages)[0]]; | |||
if (!page.imageinfo) 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.className = 'commons-gallery-item'; | |||
var img = document.createElement('img'); | |||
img.src = imgUrl; | |||
img.alt = item.caption || item.filename; | |||
img.loading = 'lazy'; | |||
img.style.width = '100%'; | |||
img.style.display = 'block'; | |||
figure.appendChild(img); | |||
var figcaption = document.createElement('figcaption'); | |||
figcaption.style.fontSize = '0.75em'; | |||
figcaption.style.textAlign = 'center'; | |||
figcaption.innerHTML = | |||
(item.caption ? item.caption + '<br>' : '') + | |||
'© ' + artist + | |||
(licenseUrl | |||
? ' – <a href="' + licenseUrl + | |||
'" target="_blank">' + license + '</a>' | |||
: ''); | |||
figure.appendChild(figcaption); | |||
grid.appendChild(figure); | |||
}); | |||
}); | |||
}); | |||
}); | }); | ||