MediaWiki:Common.js: Difference between revisions
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 22: | Line 22: | ||
} | } | ||
}); | }); | ||
}); | |||
mw.loader.using(['jquery'], function() { | |||
function processPopup($popup) { | |||
// First, 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', '/wiki/' + encodeURIComponent(titleText.replace(/ /g, '_'))) | |||
.attr('target', '_blank') | |||
.text(titleText); | |||
$(this).html(link); | |||
} | |||
}); | |||
// Then, replace any wiki markup in the form [PageName] with clickable links. | |||
var htmlContent = $popup.html(); | |||
var newContent = htmlContent.replace(/\[([^\]]+)\]/g, function(match, p1) { | |||
var pageName = p1.replace(/ /g, '_'); | |||
return '<a href="/' + encodeURIComponent(pageName) + '" target="_blank">' + p1 + '</a>'; | |||
}); | |||
$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 { | |||
// Also check within any child nodes. | |||
$node.find('.leaflet-popup-content').each(function() { | |||
processPopup($(this)); | |||
}); | |||
} | |||
}); | |||
} | |||
}); | |||
}); | |||
// Observe the document body for new nodes (popups are added dynamically). | |||
observer.observe(document.body, { childList: true, subtree: true }); | |||
}); | }); | ||