Difference between revisions of "MediaWiki:Common.js"
Jump to navigation
Jump to search
| (17 intermediate revisions by the same user not shown) | |||
| Line 11: | Line 11: | ||
var script = document.createElement('script'); | var script = document.createElement('script'); | ||
script.src = '/w/resources/lightbox2/lightbox.min.js'; | script.src = '/w/resources/lightbox2/lightbox.min.js'; | ||
| + | script.onload = function () { | ||
| + | if (window.lightbox) { | ||
| + | lightbox.option({ | ||
| + | fadeDuration: 100, | ||
| + | resizeDuration: 0, | ||
| + | wrapAround: true, | ||
| + | alwaysShowNavOnTouchDevices: true | ||
| + | }); | ||
| + | |||
| + | |||
| + | // Force recalculation of layout after delay | ||
| + | setTimeout(function() { | ||
| + | var event = new Event("resize"); | ||
| + | window.dispatchEvent(event); | ||
| + | }, 300); | ||
| + | } | ||
| + | }; | ||
document.body.appendChild(script); | document.body.appendChild(script); | ||
}); | }); | ||
| Line 22: | Line 39: | ||
var caption = $link.closest('.gallerybox').find('.gallerytext').text().trim(); | var caption = $link.closest('.gallerybox').find('.gallerytext').text().trim(); | ||
| − | if (caption) { | + | if (caption) { |
| − | + | caption += "<br><span style='display:block;margin-top:5px;font-size:9px;font-decoration:none;font-weight:normal;color:#ccc;'>[press ESC or click out to exit, use arrow keys to browse]</span>"; | |
| − | + | $link.attr('data-title', caption); | |
| − | } | + | } |
| + | }); | ||
| + | }); | ||
| + | |||
| + | mw.hook('wikipage.content').add(function($content) { | ||
| + | $content.find('.infobox a.image').each(function() { | ||
| + | var $link = $(this); | ||
| + | var $img = $link.find('img'); | ||
| + | if (!$img.length) return; | ||
| + | var src = $img.attr('src'); | ||
| + | |||
| + | // Convert thumb path to full image path | ||
| + | var fileUrl = src | ||
| + | .replace(/\/thumb\//, '/') | ||
| + | .replace(/\/\d+px-[^\/]+$/, ''); // removes /220px-filename.jpg | ||
| + | |||
| + | $link.attr('href', fileUrl); | ||
| + | $link.attr('data-lightbox', 'infobox'); | ||
| + | var caption = $link.attr('title') || $img.attr('alt') || ''; | ||
| + | if (caption) { | ||
| + | caption += "<br><small style='display:block;margin-top:5px;font-size:9px;color:#ccc;'>click outside or press ESC to close</small>"; | ||
| + | $link.attr('data-title', caption); | ||
| + | } | ||
}); | }); | ||
}); | }); | ||
Latest revision as of 18:25, 23 May 2025
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using('jquery').then(function() {
// Load Lightbox2 CSS
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/w/resources/lightbox2/lightbox.css'; // or .min.css
document.head.appendChild(link);
// Load Lightbox2 JS
var script = document.createElement('script');
script.src = '/w/resources/lightbox2/lightbox.min.js';
script.onload = function () {
if (window.lightbox) {
lightbox.option({
fadeDuration: 100,
resizeDuration: 0,
wrapAround: true,
alwaysShowNavOnTouchDevices: true
});
// Force recalculation of layout after delay
setTimeout(function() {
var event = new Event("resize");
window.dispatchEvent(event);
}, 300);
}
};
document.body.appendChild(script);
});
mw.hook('wikipage.content').add(function($content) {
$content.find('.gallerybox a.image').each(function() {
var $link = $(this);
var fileUrl = $link.find('img').attr('src').replace(/thumb\//, '').replace(/\/[^\/]+$/, '');
$link.attr('href', fileUrl);
$link.attr('data-lightbox', 'gallery');
var caption = $link.closest('.gallerybox').find('.gallerytext').text().trim();
if (caption) {
caption += "<br><span style='display:block;margin-top:5px;font-size:9px;font-decoration:none;font-weight:normal;color:#ccc;'>[press ESC or click out to exit, use arrow keys to browse]</span>";
$link.attr('data-title', caption);
}
});
});
mw.hook('wikipage.content').add(function($content) {
$content.find('.infobox a.image').each(function() {
var $link = $(this);
var $img = $link.find('img');
if (!$img.length) return;
var src = $img.attr('src');
// Convert thumb path to full image path
var fileUrl = src
.replace(/\/thumb\//, '/')
.replace(/\/\d+px-[^\/]+$/, ''); // removes /220px-filename.jpg
$link.attr('href', fileUrl);
$link.attr('data-lightbox', 'infobox');
var caption = $link.attr('title') || $img.attr('alt') || '';
if (caption) {
caption += "<br><small style='display:block;margin-top:5px;font-size:9px;color:#ccc;'>click outside or press ESC to close</small>";
$link.attr('data-title', caption);
}
});
});