MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
// ------------------------- | |||
// Infobox cleanup | |||
// ------------------------- | |||
$(document).ready(function() { | $(document).ready(function() { | ||
$('.infoboxTable th').each(function() { | $('.infoboxTable th').each(function() { | ||
if ($(this).text().trim() === "Image") { | if ($(this).text().trim() === "Image") { | ||
$(this).text(""); | $(this).text(""); | ||
} | } | ||
}); | }); | ||
}); | }); | ||
// ------------------------- | |||
// Remove unwanted Introduction headers | |||
// ------------------------- | |||
$(document).ready(function() { | $(document).ready(function() { | ||
$('h2').each(function() { | $('h2').each(function() { | ||
if ($(this).text().trim() === "Introduction") { | if ($(this).text().trim() === "Introduction") { | ||
$(this).remove(); | $(this).remove(); | ||
} | } | ||
}); | }); | ||
}); | }); | ||
// ------------------------- | |||
// Remove first empty paragraph | |||
// ------------------------- | |||
$(document).ready(function() { | $(document).ready(function() { | ||
$('p').first().each(function() { | $('p').first().each(function() { | ||
if ($(this).html().trim() === "<br>") { | if ($(this).html().trim() === "<br>") { | ||
| Line 24: | Line 32: | ||
}); | }); | ||
// ------------------------- | |||
// Load Leaflet | |||
// ------------------------- | |||
mw.loader.load('https://unpkg.com/leaflet@1.9.4/dist/leaflet.js'); | |||
mw.loader.load('/maps/orethil/leaflet-map.js'); | |||
// ------------------------- | |||
// Leaflet popup cleanup / wiki-link conversion | |||
// ------------------------- | |||
mw.loader.using(['jquery'], function() { | mw.loader.using(['jquery'], function() { | ||
function processPopup($popup) { | function processPopup($popup) { | ||
| Line 41: | Line 60: | ||
var htmlContent = $popup.html(); | var htmlContent = $popup.html(); | ||
// 3. Replace wiki-style link markup [PageName] with clickable links | // 3. Replace wiki-style link markup [PageName] with clickable links. | ||
var newContent = htmlContent.replace(/\[([^\]]+)\]/g, function(match, p1) { | var newContent = htmlContent.replace(/\[([^\]]+)\]/g, function(match, p1) { | ||
if (p1.trim().toLowerCase().startsWith('img:')) { | if (p1.trim().toLowerCase().startsWith('img:')) { | ||
| Line 51: | Line 69: | ||
}); | }); | ||
// 4. Replace custom image markup [img:...] with | // 4. Replace custom image markup [img:...] with image element. | ||
newContent = newContent.replace(/\[img:([^\]]+)\]/gi, function(match, p1) { | newContent = newContent.replace(/\[img:([^\]]+)\]/gi, function(match, p1) { | ||
var imageFile = p1.trim(); | var imageFile = p1.trim(); | ||
var src; | var src; | ||
if (/^https?:\/\//.test(imageFile)) { | if (/^https?:\/\//.test(imageFile)) { | ||
src = imageFile; | src = imageFile; | ||
| Line 61: | Line 79: | ||
src = '/images/' + encodeURIComponent(imageFile); | src = '/images/' + encodeURIComponent(imageFile); | ||
} | } | ||
return '<img src="' + src + '" alt="' + imageFile + '" style="max-width:50%; display:block; margin-top:5px; margin-bottom:5px;">'; | return '<img src="' + src + '" alt="' + imageFile + '" style="max-width:50%; display:block; margin-top:5px; margin-bottom:5px;">'; | ||
}); | }); | ||
| Line 74: | Line 93: | ||
$(mutation.addedNodes).each(function() { | $(mutation.addedNodes).each(function() { | ||
var $node = $(this); | var $node = $(this); | ||
if ($node.hasClass('leaflet-popup-content')) { | if ($node.hasClass('leaflet-popup-content')) { | ||
processPopup($node); | processPopup($node); | ||
} else { | } else { | ||
$node.find('.leaflet-popup-content').each(function() { | $node.find('.leaflet-popup-content').each(function() { | ||
processPopup($(this)); | processPopup($(this)); | ||
| Line 87: | Line 106: | ||
}); | }); | ||
observer.observe(document.body, { childList: true, subtree: true }); | observer.observe(document.body, { childList: true, subtree: true }); | ||
}); | }); | ||
Revision as of 22:55, 3 May 2026
// -------------------------
// Infobox cleanup
// -------------------------
$(document).ready(function() {
$('.infoboxTable th').each(function() {
if ($(this).text().trim() === "Image") {
$(this).text("");
}
});
});
// -------------------------
// Remove unwanted Introduction headers
// -------------------------
$(document).ready(function() {
$('h2').each(function() {
if ($(this).text().trim() === "Introduction") {
$(this).remove();
}
});
});
// -------------------------
// Remove first empty paragraph
// -------------------------
$(document).ready(function() {
$('p').first().each(function() {
if ($(this).html().trim() === "<br>") {
$(this).remove();
}
});
});
// -------------------------
// Load Leaflet
// -------------------------
mw.loader.load('https://unpkg.com/leaflet@1.9.4/dist/leaflet.js');
mw.loader.load('/maps/orethil/leaflet-map.js');
// -------------------------
// Leaflet popup cleanup / wiki-link conversion
// -------------------------
mw.loader.using(['jquery'], function() {
function processPopup($popup) {
// 1. Update any <strong> title tags that are not already links.
$popup.find('strong').each(function() {
var titleText = $(this).text().trim();
if (titleText.length > 0 && $(this).find('a').length === 0) {
var link = $('<a>')
.attr('href', '/' + encodeURIComponent(titleText.replace(/ /g, '_')))
.attr('target', '_blank')
.text(titleText);
$(this).html(link);
}
});
// 2. Get the popup's HTML content.
var htmlContent = $popup.html();
// 3. Replace wiki-style link markup [PageName] with clickable links.
var newContent = htmlContent.replace(/\[([^\]]+)\]/g, function(match, p1) {
if (p1.trim().toLowerCase().startsWith('img:')) {
return match;
}
var pageName = p1.replace(/ /g, '_');
return '<a href="/' + encodeURIComponent(pageName) + '" target="_blank">' + p1 + '</a>';
});
// 4. Replace custom image markup [img:...] with image element.
newContent = newContent.replace(/\[img:([^\]]+)\]/gi, function(match, p1) {
var imageFile = p1.trim();
var src;
if (/^https?:\/\//.test(imageFile)) {
src = imageFile;
} else {
src = '/images/' + encodeURIComponent(imageFile);
}
return '<img src="' + src + '" alt="' + imageFile + '" style="max-width:50%; display:block; margin-top:5px; margin-bottom:5px;">';
});
// 5. Update the popup content.
$popup.html(newContent);
}
// Use a MutationObserver to detect when new popup content is added.
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes) {
$(mutation.addedNodes).each(function() {
var $node = $(this);
if ($node.hasClass('leaflet-popup-content')) {
processPopup($node);
} else {
$node.find('.leaflet-popup-content').each(function() {
processPopup($(this));
});
}
});
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
});