"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
No edit summary |
No edit summary |
||
| (2 ഉപയോക്താക്കൾ ചെയ്ത ഇടയ്ക്കുള്ള 15 നാൾപ്പതിപ്പുകൾ പ്രദർശിപ്പിക്കുന്നില്ല) | |||
| വരി 1: | വരി 1: | ||
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Joeytje50/JWB.js/load.js&action=raw&ctype=text/javascript'); | |||
/* ============================================================ | /* ============================================================ | ||
OpenStreetMap marker URL fix | OpenStreetMap marker URL fix | ||
============================================================ */ | ============================================================ */ | ||
window.onload = function () { | |||
var images = document.querySelectorAll('img'); | var images = document.querySelectorAll('img'); | ||
for (var i = 0; i < images.length; i++) { | for (var i = 0; i < images.length; i++) { | ||
var img = images[i]; | var img = images[i]; | ||
if (img.src && | if ( | ||
img.src. | img.src && | ||
img.src.startsWith("https://tile.openstreetmap.org/v4/marker/") | |||
) { | |||
img.src = img.src.replace( | img.src = img.src.replace( | ||
'https://tile.openstreetmap.org/v4/marker/', | 'https://tile.openstreetmap.org/v4/marker/', | ||
| വരി 13: | വരി 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: | വരി 28: | ||
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: | വരി 42: | ||
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: | വരി 55: | ||
'&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) return; | |||
if (!data || !data.query || !data.query.pages) | |||
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) return; | |||
if (!page || !page.imageinfo | |||
var info = page.imageinfo[0]; | var info = page.imageinfo[0]; | ||
| വരി 88: | വരി 76: | ||
var license = ''; | var license = ''; | ||
var licenseUrl = ''; | var licenseUrl = ''; | ||
var commonsUrl = info.descriptionurl || ''; | |||
if (info.extmetadata) { | if (info.extmetadata) { | ||
| വരി 106: | വരി 95: | ||
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%'; | ||
/* | /* Lightbox source: responsive, not original */ | ||
img.dataset.fullsrc = | var lightboxSrc = info.url; | ||
if (info.responsiveUrls) { | |||
if (window.devicePixelRatio > 1 && info.responsiveUrls['2']) { | |||
lightboxSrc = info.responsiveUrls['2']; | |||
} else if (info.responsiveUrls['1.5']) { | |||
lightboxSrc = info.responsiveUrls['1.5']; | |||
} | |||
} | |||
img.dataset.fullsrc = lightboxSrc; | |||
img.dataset.caption = | img.dataset.caption = | ||
( | (captionText ? captionText + '<br>' : '') + | ||
'© ' + artist + | '© ' + artist + | ||
(licenseUrl | (licenseUrl | ||
? ' – <a href="' + licenseUrl + | ? ' – <a href="' + licenseUrl + | ||
'" target="_blank">' + license + '</a>' | '" target="_blank" rel="noopener noreferrer">' + | ||
license + '</a>' | |||
: '') + | |||
(commonsUrl | |||
? '<br><a href="' + commonsUrl + | |||
'" target="_blank" rel="noopener noreferrer">' + | |||
'View on Wikimedia Commons</a>' | |||
: ''); | : ''); | ||
| വരി 126: | വരി 129: | ||
figcaption.style.fontSize = '0.75em'; | figcaption.style.fontSize = '0.75em'; | ||
figcaption.style.textAlign = 'center'; | figcaption.style.textAlign = 'center'; | ||
figcaption.innerHTML = img.dataset.caption; | |||
figcaption.innerHTML = | |||
figure.appendChild(figcaption); | figure.appendChild(figcaption); | ||
| വരി 141: | വരി 137: | ||
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'; | |||
}); | }); | ||
}); | }); | ||
}); | }); | ||
/* ============================================================ | /* ============================================================ | ||
Commons Gallery Lightbox Viewer ( | Commons Gallery Lightbox Viewer (with navigation) | ||
============================================================ */ | ============================================================ */ | ||
(function () { | (function () { | ||
var galleryImages = []; | |||
var currentIndex = -1; | |||
function collectGalleryImages() { | |||
galleryImages = Array.prototype.slice.call( | |||
document.querySelectorAll('.commons-gallery-item img') | |||
); | |||
} | |||
function createLightbox() { | function createLightbox() { | ||
if (document.getElementById('commons-lightbox')) | if (document.getElementById('commons-lightbox')) return; | ||
var overlay = document.createElement('div'); | var overlay = document.createElement('div'); | ||
| വരി 166: | വരി 173: | ||
'<div class="clb-backdrop"></div>' + | '<div class="clb-backdrop"></div>' + | ||
'<div class="clb-content">' + | '<div class="clb-content">' + | ||
'<button class="clb-prev" aria-label="Previous">‹</button>' + | |||
'<button class="clb-next" aria-label="Next">›</button>' + | |||
'<button class="clb-close" aria-label="Close">×</button>' + | '<button class="clb-close" aria-label="Close">×</button>' + | ||
'<img class="clb-image" alt="">' + | '<img class="clb-image" alt="">' + | ||
| വരി 173: | വരി 182: | ||
document.body.appendChild(overlay); | document.body.appendChild(overlay); | ||
overlay.querySelector('.clb-backdrop'). | overlay.querySelector('.clb-backdrop').onclick = closeLightbox; | ||
overlay.querySelector('.clb- | overlay.querySelector('.clb-close').onclick = closeLightbox; | ||
overlay.querySelector('.clb-prev').onclick = showPrev; | |||
overlay.querySelector('.clb-next').onclick = showNext; | |||
document.addEventListener('keydown', function (e) { | document.addEventListener('keydown', function (e) { | ||
if (e.key === 'Escape') | if (!overlay.classList.contains('active')) return; | ||
if (e.key === 'Escape') closeLightbox(); | |||
if (e.key === 'ArrowLeft') showPrev(); | |||
if (e.key === 'ArrowRight') showNext(); | |||
}); | }); | ||
} | } | ||
function openLightbox(img) { | function openLightbox(img) { | ||
collectGalleryImages(); | |||
currentIndex = galleryImages.indexOf(img); | |||
if (currentIndex === -1) return; | |||
createLightbox(); | createLightbox(); | ||
showImage(currentIndex); | |||
document.getElementById('commons-lightbox').classList.add('active'); | |||
document.body.style.overflow = 'hidden'; | |||
} | |||
function showImage(index) { | |||
var overlay = document.getElementById('commons-lightbox'); | var overlay = document.getElementById('commons-lightbox'); | ||
var lbImg = overlay.querySelector('.clb-image'); | var lbImg = overlay.querySelector('.clb-image'); | ||
var caption = overlay.querySelector('.clb-caption'); | var caption = overlay.querySelector('.clb-caption'); | ||
var img = galleryImages[index]; | |||
if (!img) return; | |||
lbImg.src = img.dataset.fullsrc || img.src; | lbImg.src = img.dataset.fullsrc || img.src; | ||
lbImg.alt = img.alt || ''; | lbImg.alt = img.alt || ''; | ||
caption.innerHTML = img.dataset.caption || ''; | |||
currentIndex = index; | |||
} | |||
function showPrev() { | |||
if (currentIndex > 0) showImage(currentIndex - 1); | |||
} | |||
function showNext() { | |||
if (currentIndex < galleryImages.length - 1) { | |||
showImage(currentIndex + 1); | |||
} | |||
} | } | ||
| വരി 202: | വരി 235: | ||
var overlay = document.getElementById('commons-lightbox'); | var overlay = document.getElementById('commons-lightbox'); | ||
if (!overlay) return; | if (!overlay) return; | ||
overlay.classList.remove('active'); | overlay.classList.remove('active'); | ||
document.body.style.overflow = ''; | document.body.style.overflow = ''; | ||
} | } | ||
/* | /* Event delegation (single, correct) */ | ||
document.addEventListener('click', function (e) { | document.addEventListener('click', function (e) { | ||
var img = e.target; | var img = e.target; | ||
if (img && img.matches('.commons-gallery-item img')) { | if (img && img.matches('.commons-gallery-item img')) { | ||
openLightbox(img); | openLightbox(img); | ||
} | } | ||
| വരി 220: | വരി 248: | ||
})(); | })(); | ||
/* ============================================================ | /* ============================================================ | ||
| വരി 337: | വരി 261: | ||
); | ); | ||
} | } | ||
/* ============================================================ | /* ============================================================ | ||
| വരി 390: | വരി 313: | ||
$(addCharSubsetMenu); | $(addCharSubsetMenu); | ||
/* ============================================================ | /* ============================================================ | ||
| വരി 435: | വരി 357: | ||
}); | }); | ||
}); | }); | ||
/* ============================================================ | /* ============================================================ | ||
| വരി 441: | വരി 362: | ||
============================================================ */ | ============================================================ */ | ||
var autoCollapse = 2; | var autoCollapse = 2; | ||
var collapseCaption = | var collapseCaption = 'മറയ്ക്കുക'; | ||
var expandCaption = | var expandCaption = 'പ്രദർശിപ്പിക്കുക'; | ||
function collapseTable(tableIndex) { | function collapseTable(tableIndex) { | ||
var Button = document.getElementById( | var Button = document.getElementById('collapseButton' + tableIndex); | ||
var Table = document.getElementById( | var Table = document.getElementById('collapsibleTable' + tableIndex); | ||
if (!Table || !Button) return; | if (!Table || !Button) return; | ||
| വരി 453: | വരി 374: | ||
for (var i = 1; i < Rows.length; i++) { | for (var i = 1; i < Rows.length; i++) { | ||
Rows[i].style.display = hide ? | Rows[i].style.display = hide ? 'none' : ''; | ||
} | } | ||
Button.firstChild.data = hide ? expandCaption : collapseCaption; | Button.firstChild.data = hide ? expandCaption : collapseCaption; | ||