"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
No edit summary |
No edit summary |
||
| വരി 17: | വരി 17: | ||
/* ============================================================ | /* ============================================================ | ||
Wikimedia Commons Gallery Loader ( | Wikimedia Commons Gallery Loader (Template-based, MULTILINE SAFE) | ||
============================================================ */ | ============================================================ */ | ||
mw.hook('wikipage.content').add(function ($content) { | mw.hook('wikipage.content').add(function ($content) { | ||
| വരി 24: | വരി 24: | ||
var container = this; | var container = this; | ||
var | var sources = container.querySelectorAll('.commons-gallery-source'); | ||
if (! | if (!sources.length) return; | ||
var width = | var width = 300; | ||
var grid = document.createElement('div'); | var grid = document.createElement('div'); | ||
| വരി 50: | വരി 38: | ||
container.appendChild(grid); | container.appendChild(grid); | ||
sources.forEach(function (srcEl) { | |||
var filename = srcEl.dataset.file; | |||
var captionText = srcEl.dataset.caption || ''; | |||
if (!filename) return; | |||
var apiUrl = | var apiUrl = | ||
| വരി 59: | വരി 52: | ||
'&iiprop=url|extmetadata' + | '&iiprop=url|extmetadata' + | ||
'&iiurlwidth=' + width + | '&iiurlwidth=' + width + | ||
'&titles=File:' + encodeURIComponent( | '&titles=File:' + encodeURIComponent(filename) + | ||
'&origin=*'; | '&origin=*'; | ||
fetch(apiUrl) | fetch(apiUrl) | ||
.then(function (r) { | .then(function (r) { | ||
if (!r.ok) | if (!r.ok) throw new Error('HTTP ' + r.status); | ||
return r.json(); | return r.json(); | ||
}) | }) | ||
.then(function (data) { | .then(function (data) { | ||
if (!data || !data.query || !data.query.pages) | if (!data || !data.query || !data.query.pages) return; | ||
var page = | var page = | ||
data.query.pages[Object.keys(data.query.pages)[0]]; | data.query.pages[Object.keys(data.query.pages)[0]]; | ||
if (!page || !page.imageinfo | if (!page || !page.imageinfo) return; | ||
var info = page.imageinfo[0]; | var info = page.imageinfo[0]; | ||
| വരി 106: | വരി 93: | ||
var img = document.createElement('img'); | var img = document.createElement('img'); | ||
img.src = imgUrl; | img.src = imgUrl; | ||
img.alt = | img.alt = captionText || filename; | ||
img.loading = 'lazy'; | img.loading = 'lazy'; | ||
img.style.width = '100%'; | img.style.width = '100%'; | ||
img.style.display = 'block'; | img.style.display = 'block'; | ||
/* | /* Lightbox data */ | ||
img.dataset.fullsrc = info.url; | img.dataset.fullsrc = info.url; | ||
img.dataset.caption = | img.dataset.caption = | ||
( | (captionText ? captionText + '<br>' : '') + | ||
'© ' + artist + | '© ' + artist + | ||
(licenseUrl | (licenseUrl | ||
| വരി 128: | വരി 115: | ||
figcaption.innerHTML = | figcaption.innerHTML = | ||
( | (captionText ? captionText + '<br>' : '') + | ||
'© ' + artist + | '© ' + artist + | ||
(licenseUrl | (licenseUrl | ||
| വരി 141: | വരി 128: | ||
console.warn( | console.warn( | ||
'Commons gallery image failed:', | 'Commons gallery image failed:', | ||
filename, | |||
err.message | err.message | ||
); | ); | ||
}); | }); | ||
}); | |||
/* Hide source placeholders */ | |||
sources.forEach(function (el) { | |||
el.style.display = 'none'; | |||
}); | }); | ||
}); | }); | ||