MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* | /* | ||
* ========================================================== | |||
* GladiusRO Wiki | * GladiusRO Wiki | ||
* Move Contents below | * | ||
* 1. Move the Table of Contents below Tools. | |||
* 2. Hide technical links from logged-out players. | |||
* 3. Keep technical links visible for logged-in administrators. | |||
* ========================================================== | |||
*/ | */ | ||
(function () { | (function (mw) { | ||
'use strict'; | 'use strict'; | ||
var attempts = 0; | var attempts = 0; | ||
var maximumAttempts = | var maximumAttempts = 40; | ||
function | /* | ||
* Move Contents below the Tools section. | |||
*/ | |||
function moveContentsBelowTools() { | |||
var tools = document.getElementById('p-tb'); | |||
var toc = document.getElementById('toc'); | var toc = document.getElementById('toc'); | ||
var | var tocContainer = | ||
document.getElementById('gladius-sidebar-toc'); | document.getElementById('gladius-sidebar-toc'); | ||
if (!tools || !tools.parentNode) { | |||
return false; | |||
return | |||
} | } | ||
/* | /* | ||
* | * If no Gladius container exists yet, | ||
* and the | * create one and move the original TOC into it. | ||
*/ | */ | ||
if (!toc | if (!tocContainer && toc) { | ||
tocContainer = document.createElement('div'); | |||
tocContainer.id = 'gladius-sidebar-toc'; | |||
tocContainer.className = 'portal mw-portlet'; | |||
tocContainer.setAttribute( | |||
'role', | |||
'navigation' | |||
); | |||
tocContainer.setAttribute( | |||
'aria-label', | |||
'Page contents' | |||
); | |||
tocContainer.appendChild(toc); | |||
} | |||
if (!tocContainer) { | |||
return false; | |||
} | |||
/* | /* | ||
* | * Always place the container immediately after Tools. | ||
* | |||
* This also corrects the position when an older script | |||
* accidentally leaves Contents inside the article. | |||
*/ | */ | ||
tools. | tools.parentNode.insertBefore( | ||
tocContainer, | |||
tools.nextSibling | |||
); | ); | ||
return true; | return true; | ||
} | } | ||
/* | /* | ||
* | * Hide technical sidebar items from players | ||
* who are not logged in. | |||
*/ | */ | ||
function hidePlayerTechnicalLinks() { | |||
var userName = mw.config.get('wgUserName'); | |||
/* | /* | ||
* | * A non-null username means the visitor is logged in. | ||
* | * Administrators and logged-in users keep the links. | ||
*/ | */ | ||
if ( | if (userName !== null) { | ||
return; | return; | ||
} | } | ||
var elementsToHide = [ | |||
var | |||
'n-recentchanges', | 'n-recentchanges', | ||
'n-randompage', | 'n-randompage', | ||
'n-help' | 'n-help', | ||
'p-tb' | |||
]; | ]; | ||
elementsToHide.forEach(function (elementId) { | |||
var | var element = document.getElementById(elementId); | ||
if ( | if (element) { | ||
element.style.setProperty( | |||
'display', | 'display', | ||
'none', | 'none', | ||
| Line 134: | Line 100: | ||
} | } | ||
}); | }); | ||
} | |||
/* | |||
* Run both functions in the correct order. | |||
* | |||
* Contents must be moved first because the script | |||
* uses the Tools panel as its placement reference. | |||
* Tools can then safely be hidden from players. | |||
*/ | |||
function initialiseGladiusSidebar() { | |||
attempts++; | |||
var moved = moveContentsBelowTools(); | |||
if (moved) { | |||
hidePlayerTechnicalLinks(); | |||
return; | |||
} | |||
/* | /* | ||
* | * Retry while MediaWiki finishes creating the sidebar. | ||
*/ | */ | ||
if (attempts < maximumAttempts) { | |||
window.setTimeout( | |||
if ( | initialiseGladiusSidebar, | ||
200 | |||
); | ); | ||
} | } | ||
| Line 156: | Line 131: | ||
/* | /* | ||
* | * Start after the HTML is ready. | ||
*/ | */ | ||
if (document.readyState === 'loading') { | if (document.readyState === 'loading') { | ||
document.addEventListener( | document.addEventListener( | ||
'DOMContentLoaded', | 'DOMContentLoaded', | ||
initialiseGladiusSidebar | |||
); | ); | ||
} else { | } else { | ||
initialiseGladiusSidebar(); | |||
} | } | ||
/* | /* | ||
* Run | * Run again after images and skin resources load. | ||
*/ | */ | ||
window.addEventListener( | window.addEventListener( | ||
'load', | |||
function () { | |||
attempts = 0; | |||
initialiseGladiusSidebar(); | |||
} | |||
); | ); | ||
/* | |||
* Run again if MediaWiki updates article content. | |||
*/ | |||
if (mw.hook) { | |||
mw.hook('wikipage.content').add(function () { | |||
attempts = 0; | |||
window.setTimeout( | |||
initialiseGladiusSidebar, | |||
100 | |||
); | |||
}); | |||
} | |||
}(mediaWiki)); | }(mediaWiki)); | ||
Revision as of 03:06, 2 August 2026
/*
* ==========================================================
* GladiusRO Wiki
*
* 1. Move the Table of Contents below Tools.
* 2. Hide technical links from logged-out players.
* 3. Keep technical links visible for logged-in administrators.
* ==========================================================
*/
(function (mw) {
'use strict';
var attempts = 0;
var maximumAttempts = 40;
/*
* Move Contents below the Tools section.
*/
function moveContentsBelowTools() {
var tools = document.getElementById('p-tb');
var toc = document.getElementById('toc');
var tocContainer =
document.getElementById('gladius-sidebar-toc');
if (!tools || !tools.parentNode) {
return false;
}
/*
* If no Gladius container exists yet,
* create one and move the original TOC into it.
*/
if (!tocContainer && toc) {
tocContainer = document.createElement('div');
tocContainer.id = 'gladius-sidebar-toc';
tocContainer.className = 'portal mw-portlet';
tocContainer.setAttribute(
'role',
'navigation'
);
tocContainer.setAttribute(
'aria-label',
'Page contents'
);
tocContainer.appendChild(toc);
}
if (!tocContainer) {
return false;
}
/*
* Always place the container immediately after Tools.
*
* This also corrects the position when an older script
* accidentally leaves Contents inside the article.
*/
tools.parentNode.insertBefore(
tocContainer,
tools.nextSibling
);
return true;
}
/*
* Hide technical sidebar items from players
* who are not logged in.
*/
function hidePlayerTechnicalLinks() {
var userName = mw.config.get('wgUserName');
/*
* A non-null username means the visitor is logged in.
* Administrators and logged-in users keep the links.
*/
if (userName !== null) {
return;
}
var elementsToHide = [
'n-recentchanges',
'n-randompage',
'n-help',
'p-tb'
];
elementsToHide.forEach(function (elementId) {
var element = document.getElementById(elementId);
if (element) {
element.style.setProperty(
'display',
'none',
'important'
);
}
});
}
/*
* Run both functions in the correct order.
*
* Contents must be moved first because the script
* uses the Tools panel as its placement reference.
* Tools can then safely be hidden from players.
*/
function initialiseGladiusSidebar() {
attempts++;
var moved = moveContentsBelowTools();
if (moved) {
hidePlayerTechnicalLinks();
return;
}
/*
* Retry while MediaWiki finishes creating the sidebar.
*/
if (attempts < maximumAttempts) {
window.setTimeout(
initialiseGladiusSidebar,
200
);
}
}
/*
* Start after the HTML is ready.
*/
if (document.readyState === 'loading') {
document.addEventListener(
'DOMContentLoaded',
initialiseGladiusSidebar
);
} else {
initialiseGladiusSidebar();
}
/*
* Run again after images and skin resources load.
*/
window.addEventListener(
'load',
function () {
attempts = 0;
initialiseGladiusSidebar();
}
);
/*
* Run again if MediaWiki updates article content.
*/
if (mw.hook) {
mw.hook('wikipage.content').add(function () {
attempts = 0;
window.setTimeout(
initialiseGladiusSidebar,
100
);
});
}
}(mediaWiki));