Difference between revisions of "MediaWiki:Common.js"
From Summertime Saga Wiki
Strayerror (talk | contribs) |
Strayerror (talk | contribs) |
||
| Line 91: | Line 91: | ||
activateTab(pref); | activateTab(pref); | ||
| − | rewriteAnchors('div.vt- | + | rewriteAnchors('div.vt-0-20-16', '-0.20.16'); |
if (location.hash) { | if (location.hash) { | ||
Revision as of 14:43, 11 October 2025
/* Any JavaScript here will be loaded for all users on every page load. */
// Wait for document ready
$(function() {
var htmle = function(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
};
if ($('body').hasClass('page-Summertime_Saga_Wiki') && $('body').hasClass('action-view') && $('body').hasClass('skin-vector') && $('#ajax-posts').length && $('#ajax-version').length) {
var maxPosts = 5;
$.getJSON('/ssdata.json', function(data) {
$('#ajax-version').text(data.version);
$('#ajax-posts').html('');
data.posts.forEach(function(post, i) {
if (i >= maxPosts) {
return;
}
$('#ajax-posts').append('<dd><b>▪ ' + htmle(post.date) + ':</b> <a rel="nofollow" class="external text" href="https://www.patreon.com' + htmle(post.url) + '">' + htmle(post.title) + '</a></dd>');
});
});
}
}());
mw.hook('wikipage.content').add(function ($content) {
// Manage the Preview / Stable tabs
const $tabs = $content.find('ul.version-tabs');
if (!$tabs) return;
const allow = ['21', '0.20.16'];
const pref = location.hash
? location.hash.endsWith('-0.20.16') ? '0.20.16' : '21'
: localStorage.getItem('preferredVersion') || 'current';
function activateTab(tab) {
if (allow.indexOf(tab) === -1) return;
$content.find('div.vt-content').hide();
$content.find('div.vt-' + tab.replaceAll('.', '-')).show();
$tabs.find('li').removeClass('active');
$tabs.find('li[data-tab="' + tab + '"]').addClass('active');
localStorage.setItem('preferredVersion', tab);
}
function rewriteAnchors(scopeSelector, suffix) {
const $scope = $(scopeSelector);
// Map old ID → new ID
const idMap = {};
// Rewrite all elements with `id`
$scope.find('[id]').each(function () {
const $el = $(this);
const oldId = $el.attr('id');
const newId = oldId + suffix;
$el.attr('id', newId);
idMap[oldId] = newId;
});
// Rewrite all anchor links pointing to those IDs
$scope.find('a[href^="#"]').each(function () {
const $a = $(this);
const href = $a.attr('href').substring(1); // strip leading #
if (idMap[href]) {
$a.attr('href', '#' + idMap[href]);
}
});
// Rewrite `label[for]` attributes too (optional)
$scope.find('label[for]').each(function () {
const $label = $(this);
const forId = $label.attr('for');
if (idMap[forId]) {
$label.attr('for', idMap[forId]);
}
});
}
$content.find('ul.version-tabs li').click(function (e) {
e.preventDefault();
const selected = this.dataset.tab;
activateTab(selected);
});
activateTab(pref);
rewriteAnchors('div.vt-0-20-16', '-0.20.16');
if (location.hash) {
location.hash = location.hash;
}
});