Common.js – MediaWiki

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Layout changes only at this stage - making it easier to read, edit, and understand.)
m (Fix typo)
 
(81 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/*****************************************************************************************
/*****************************************************************************************
  * Any JavaScript here will be loaded for all users on every page load.
  * This JavaScript file will be loaded for all users on every page load
*
  * Its content must be kept as minimal as possible.
  * As such, the content of this file page should be kept as minimal as possible.
*
  *****************************************************************************************/
  *****************************************************************************************/


/*****************************************************************************************
mw.loader.using( ['mediawiki.util', 'jquery.client'], function() { /* BEGIN mw.loader WRAPPER */
* Redirect User:Name/skin.js and skin.css to the current skin's pages
* (unless the 'skin' page really exists)
* @source: http://www.mediawiki.org/wiki/Snippets/Redirect_skin.js
* @rev: 2
*/


if ( mw.config.get( 'wgArticleId' ) === 0 && mw.config.get( 'wgNamespaceNumber' ) == 2 ) {
/*****************************************************************************************
var titleParts = mw.config.get( 'wgPageName' ).split( '/' );
* Conditionally import further JavaScript for specific situations.
// Make sure there was a part before and after the slash
*/
// And that the latter is 'skin.js' or 'skin.css'
 
if ( titleParts.length == 2 ) {
function conditionallyImport() {
var userSkinPage = titleParts.shift() + '/' + mw.config.get( 'skin' );
if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
if ( titleParts.slice(-1) == 'skin.js' ) {
importScript( 'MediaWiki:Editing.js' );
window.location.href = mw.util.wikiGetlink( userSkinPage + '.js' );
// mw.loader.load( '/wiki/index.php?title=MediaWiki:Editing.js&action=raw&ctype=text/javascript' ); // proper thing to do - for future reference
} else if ( titleParts.slice(-1) == 'skin.css' ) {
}
window.location.href = mw.util.wikiGetlink( userSkinPage + '.css' );
if ( mw.config.get( 'wgCanonicalNamespace' ) != 'MediaWiki' && mw.config.get( 'wgArticleId' ) === 0 ) {
}
importScript( 'MediaWiki:Creation.js' );
}
if ( mw.config.get( 'wgPageName' ) === 'Special:Version' ) {
importScript( 'MediaWiki:SpecialVersion.js' );
}
}
}
}
mw.hook( 'wikipage.content' ).add( conditionallyImport );


/*****************************************************************************************/
/*****************************************************************************************
* Add a Purge button
* Maintainer: [[User:Lou Montana]]
*/


/*****************************************************************************************
function offerPurgeButton() {
* Map addPortletLink to mw.util
var protectButton = document.getElementById('ca-protect');
*/
if (protectButton == null) {
 
protectButton = document.getElementById('ca-unprotect');
window.addPortletLink = function() {
if (protectButton == null) {
return mw.util.addPortletLink.apply( mw.util, arguments );
return; // quick'n'not that dirty
};
}
 
}
/*****************************************************************************************/
var newButton = protectButton.cloneNode(true),
 
link = newButton.getElementsByTagName('a')[0];
/*****************************************************************************************
link.removeAttribute('accesskey');
* extract a URL parameter from the current URL
link.setAttribute('title', 'Purge the page\'s cache');
*
link.setAttribute('href', document.location.href + (document.location.href.indexOf('?') === -1 ? '?action=purge' : '&action=purge'));
* @deprecated: Use mw.util.getParamValue with proper escaping
link.innerHTML = 'Purge';
*/
protectButton.parentNode.appendChild(newButton);
 
function getURLParamValue(paramName, url) {
return mw.util.getParamValue(paramName, url);
}
}
mw.hook( 'wikipage.content' ).add( offerPurgeButton );


/*****************************************************************************************/
/*****************************************************************************************
* Add a Fix access button
* Maintainer: [[User:Lou Montana]]
*/


/*****************************************************************************************
function offerDeleteSessionCookieButton() {
* &withCSS= and &withJS= URL parameters *******
var protectButton = document.getElementById('ca-protect');
* Allow to try custom scripts from MediaWiki space
if (protectButton == null) {
* without editing personal .css or .js files
protectButton = document.getElementById('ca-unprotect');
*/
if (protectButton == null) {
 
return; // quick'n'not that dirty
var extraCSS = mw.util.getParamValue("withCSS");
}
if ( extraCSS && extraCSS.match(/^MediaWiki:[^&<>=%]*\.css$/) ) {
importStylesheet(extraCSS);
}
var extraJS = mw.util.getParamValue("withJS");
if ( extraJS && extraJS.match(/^MediaWiki:[^&<>=%]*\.js$/) ) {
importScript(extraJS);
}
 
/*****************************************************************************************/
 
/*****************************************************************************************
* Import more specific scripts if necessary
*/
 
if (wgAction == 'edit' || wgAction == 'submit' || wgPageName == 'Special:Upload') { //scripts specific to editing pages
importScript('MediaWiki:Common.js/edit.js');
} else if (mw.config.get('wgPageName') == 'Special:Watchlist') { //watchlist scripts
mw.loader.load(mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=MediaWiki:Common.js/watchlist.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400');
}
if ( wgNamespaceNumber == 6 ) {
importScript('MediaWiki:Common.js/file.js');
}
 
/*****************************************************************************************/
 
/*****************************************************************************************
* For sysops and accountcreators
*
* Description: Allows for sysop-specific Javascript at [[MediaWiki:Sysop.js]],
*              and accountcreator-specific CSS at [[MediaWiki:Accountcreator.css]].
*/
 
if ( $.inArray( 'sysop', wgUserGroups) > -1 ) {
if ( !window.disableSysopJS ) {
$(function(){
importScript('MediaWiki:Sysop.js');
});
}
}
var newButton = protectButton.cloneNode(true),
link = newButton.getElementsByTagName('a')[0];
link.removeAttribute('accesskey');
link.setAttribute('title', 'Delete the community_session cookie to fix the DbQueryError issue');
link.setAttribute('href', '/clearcookie.php?url=' + document.location.href);
link.innerHTML = 'Fix access';
protectButton.parentNode.appendChild(newButton);
}
}
// mw.hook( 'wikipage.content' ).add( offerDeleteSessionCookieButton ); // disabled for now, -should- be fixed


/*****************************************************************************************/
/*****************************************************************************************
* Disable Talk pages
* Maintainer: [[User:Lou Montana]]
*/


/*****************************************************************************************
function disableTalkPages() {
* WikiMiniAtlas
if ($.inArray('sysop', mw.config.get('wgUserGroups')) === -1) { // not admin
*
* Description: WikiMiniAtlas is a popup click and drag world map.
*              This script causes all of our coordinate links to display the WikiMiniAtlas popup button.
*              The script itself is located on meta because it is used by many projects.
*              See [[Meta:WikiMiniAtlas]] for more information.  
* Maintainers: [[User:Dschwen]]
*/


mw.loader.load('//meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400');
if (mw.config.get('wgCanonicalNamespace').indexOf('talk') !== -1 && // talk page
mw.config.get('wgNamespaceNumber') !== 3) { // User Talk exception


/*****************************************************************************************/
if (document.getElementById('ca-edit') != null) {
document.getElementById('ca-edit').remove();
}


/*****************************************************************************************
if (document.getElementById('ca-addsection') != null) {
* Scripts specific to Internet Explorer
document.getElementById('ca-addsection').remove();
*/
 
/*****************************************************************************************
* Internet Explorer bug fix
*
* Description: Fixes IE horizontal scrollbar bug
* Maintainers: [[User:Tom-]]?
*/
 
if (navigator.appName == 'Microsoft Internet Explorer') {var oldWidth;
var docEl = document.documentElement;
var fixIEScroll = function() {
if (!oldWidth || docEl.clientWidth > oldWidth) {
doFixIEScroll();
} else {
setTimeout(doFixIEScroll, 1);
}
}
oldWidth = docEl.clientWidth;
};
var doFixIEScroll = function () {
docEl.style.overflowX = (docEl.scrollWidth - docEl.clientWidth < 4) ? "hidden" : "";
};
document.attachEvent("onreadystatechange", fixIEScroll);
document.attachEvent("onresize", fixIEScroll);
// In print IE (7?) does not like line-height
mw.util.addCSS( '@media print { sup, sub, p, .documentDescription { line-height: normal; }}');
// IE overflow bug
mw.util.addCSS('div.overflowbugx { overflow-x: scroll !important; overflow-y: hidden !important; } div.overflowbugy { overflow-y: scroll !important; overflow-x: hidden !important; }');
// IE zoomfix
// Use to fix right floating div/table inside tables
mw.util.addCSS('.iezoomfix div, .iezoomfix table { zoom: 1;}');
// .hlist fix for IE - remove dot after last list item
if ( $.browser.version < '9.0' ) {
$(function() {
$( '.hlist' ).find( 'li:last-child' ).css( 'background', 'none' ).css( 'padding-right', '0' );
});
}
}
 
// Import scripts specific to Internet Explorer 6
var talkTab = document.getElementById('ca-talk');
if (navigator.appVersion.substr(22, 1) == '6') {
if (talkTab != null && talkTab.classList.contains('new')) {
importScript('MediaWiki:Common.js/IE60Fixes.js');
talkTab.remove();
}
}
}
}
 
}
/*****************************************************************************************/
mw.hook( 'wikipage.content' ).add( disableTalkPages );


/*****************************************************************************************
/*****************************************************************************************
* Load fixes for Windows font rendering
* Function that adds a Sandbox button
*/
* Maintainer: [[User:Lou Montana]]
*/


if( navigator.platform.indexOf( "Win" ) != -1 ) {
function addSandboxButton() {
importStylesheet( 'MediaWiki:Common.css/WinFixes.css' );
var userpageLink = document.getElementById('pt-userpage');
var talkLink = document.getElementById('pt-mytalk');
if (talkLink == null) {
return;
}
}
var sandboxLink = talkLink.cloneNode(true),
link = sandboxLink.getElementsByTagName('a')[0];
link.removeAttribute('accesskey');
link.setAttribute('title', 'Your Sandbox');
link.setAttribute('href', userpageLink.getElementsByTagName('a')[0].href + '/Sandbox');
link.innerHTML = 'Sandbox';
talkLink.parentNode.insertBefore(sandboxLink, talkLink);
}
mw.hook( 'wikipage.content' ).add( addSandboxButton );


/*****************************************************************************************/
/*****************************************************************************************
* Functions that adds Tabs
* Maintainer: [[User:Lou Montana]]
*/


/*****************************************************************************************
function tabSystem_selectTab(titles, contents, tabIndex) {
* Test if an element has a certain class
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
*
* @deprecated:  Use $(element).hasClass() instead.
*/


var hasClass = (function () {
var i;
var reCache = {};
for (i = 0; i < titles.length; i++) {
return function (element, className) {
titles[i].classList.remove('selected');
return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
}
};
for (i = 0; i < contents.length; i++) {
})();
contents[i].style.display = 'none';
 
}
/*****************************************************************************************/


/*****************************************************************************************
if (tabIndex < 0 || tabIndex > titles.length -1 || tabIndex > contents.length -1) {
* Interwiki links to featured articles
return;
*
* Description: Highlights interwiki links to featured articles (or
*              equivalents) by changing the bullet before the interwiki link
*              into a star.
* Maintainers: [[User:R. Koot]]
*/
 
function LinkFA() {
if ( document.getElementById( "p-lang" ) ) {
var InterwikiLinks = document.getElementById( "p-lang" ).getElementsByTagName( "li" );
for ( var i = 0; i < InterwikiLinks.length; i++ ) {
if ( document.getElementById( InterwikiLinks[i].className + "-fa" ) ) {
InterwikiLinks[i].className += " FA";
InterwikiLinks[i].title = "This is a featured article in another language.";
} else if ( document.getElementById( InterwikiLinks[i].className + "-ga" ) ) {
InterwikiLinks[i].className += " GA";
InterwikiLinks[i].title = "This is a good article in another language.";
}
}
}
}
titles[tabIndex].classList.add('selected');
contents[tabIndex].style.display = 'block';
}
}
$( LinkFA );
/*****************************************************************************************/


/*****************************************************************************************
function tabSystem_implementTabs() {
* Collapsible tables
*
* Description: Allows tables to be collapsed, showing only the header. See
*              [[Wikipedia:NavFrame]].
* Maintainers: [[User:R. Koot]] (copied from Wikipedia Common.js)
*/


var autoCollapse = 2;
var tabBlocks = document.getElementsByClassName('biki-tabs');
var collapseCaption = "hide";
var expandCaption = "show";
function collapseTable( tableIndex ){
var Button = document.getElementById( "collapseButton" + tableIndex );
var Table = document.getElementById( "collapsibleTable" + tableIndex );
if ( !Table || !Button ) {
return false;
}
var Rows = Table.rows;
if ( Button.firstChild.data == collapseCaption ) {
for ( var i = 1; i < Rows.length; i++ ) {
Rows[i].style.display = "none";
}
Button.firstChild.data = expandCaption;
} else {
for ( var i = 1; i < Rows.length; i++ ) {
Rows[i].style.display = Rows[0].style.display;
}
Button.firstChild.data = collapseCaption;
}
}
function createCollapseButtons(){
var tableIndex = 0;
var NavigationBoxes = new Object();
var Tables = document.getElementsByTagName( "table" );
for ( var i = 0; i < Tables.length; i++ ) {
if ( hasClass( Tables[i], "collapsible" ) ) {
/* only add button and increment count if there is a header row to work with */
var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
if (!HeaderRow) continue;
var Header = HeaderRow.getElementsByTagName( "th" )[0];
if (!Header) continue;
NavigationBoxes[ tableIndex ] = Tables[i];
Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
var Button = document.createElement( "span" );
var ButtonLink = document.createElement( "a" );
var ButtonText = document.createTextNode( collapseCaption );
Button.className = "collapseButton";  //Styles are declared in Common.css
ButtonLink.style.color = Header.style.color;
ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
ButtonLink.setAttribute( "href", "#" );
addHandler( ButtonLink,  "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return killEvt( evt );") );
ButtonLink.appendChild( ButtonText );
Button.appendChild( document.createTextNode( "[" ) );
Button.appendChild( ButtonLink );
Button.appendChild( document.createTextNode( "]" ) );
Header.insertBefore( Button, Header.childNodes[0] );
tableIndex++;
}
}
for ( var i = 0;  i < tableIndex; i++ ) {
if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
collapseTable( i );
}
else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
var element = NavigationBoxes[i];
while (element = element.parentNode) {
if ( hasClass( element, "outercollapse" ) ) {
collapseTable ( i );
break;
}
}
}
}
}
$( createCollapseButtons );


/*****************************************************************************************/
for (var i = 0; i < tabBlocks.length; i++) {


/*****************************************************************************************
var tabBlock = tabBlocks[i];
* Dynamic Navigation Bars (experimental)
*
* Description: See [[Wikipedia:NavFrame]].
* Maintainers: UNMAINTAINED
*/


// set up the words in your language
var titles = tabBlock.getElementsByClassName('biki-tab-title');
var NavigationBarHide = '[' + collapseCaption + ']';
var contents = tabBlock.getElementsByClassName('biki-tab-content');
var NavigationBarShow = '[' + expandCaption + ']';
if (titles.length < 1 || contents.length < 1 || titles.length != contents.length) {
continue;
// shows and hides content and picture (if available) of navigation bars
// Parameters:
//    indexNavigationBar: the index of navigation bar to be toggled
function toggleNavigationBar(indexNavigationBar){
var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
if (!NavFrame || !NavToggle) {
return false;
}
// if shown now
if (NavToggle.firstChild.data == NavigationBarHide) {
for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
if (hasClass(NavChild, 'NavContent') || hasClass(NavChild, 'NavPic')) {
NavChild.style.display = 'none';
}
}
}
NavToggle.firstChild.data = NavigationBarShow;
 
for (var j = 0; j < titles.length; j++) {
// if hidden now
(function(titles, contents, index) {
} else if (NavToggle.firstChild.data == NavigationBarShow) {
titles[j].addEventListener('click', function () { tabSystem_selectTab(titles, contents, index) })
for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
})(titles, contents, j);
if (hasClass(NavChild, 'NavContent') || hasClass(NavChild, 'NavPic')) {
NavChild.style.display = 'block';
}
}
}
NavToggle.firstChild.data = NavigationBarHide;
 
}
var selectedTab = 0;
}
if (tabBlock.dataset && tabBlock.dataset.selectedtab) {
var dataSelectedTab = +tabBlock.dataset.selectedtab;
// adds show/hide-button to navigation bars
if (!isNaN(dataSelectedTab)) {
function createNavigationBarToggleButton(){
selectedTab = Math.max(0, Math.min(dataSelectedTab, titles.length -1));
var indexNavigationBar = 0;
// iterate over all < div >-elements
var divs = document.getElementsByTagName("div");
for (var i = 0; NavFrame = divs[i]; i++) {
// if found a navigation bar
if (hasClass(NavFrame, "NavFrame")) {
indexNavigationBar++;
var NavToggle = document.createElement("a");
NavToggle.className = 'NavToggle';
NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
var isCollapsed = hasClass( NavFrame, "collapsed" );
/*
* Check if any children are already hidden. This loop is here for backwards compatibility:
* the old way of making NavFrames start out collapsed was to manually add style="display:none"
* to all the NavPic/NavContent elements.  Since this was bad for accessibility (no way to make
* the content visible without JavaScript support), the new recommended way is to add the class
* "collapsed" to the NavFrame itself, just like with collapsible tables.
*/
for (var NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling) {
if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
if ( NavChild.style.display == 'none' ) {
isCollapsed = true;
}
}
}
}
if (isCollapsed) {
for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
NavChild.style.display = 'none';
}
}
}
var NavToggleText = document.createTextNode(isCollapsed ? NavigationBarShow : NavigationBarHide);
NavToggle.appendChild(NavToggleText);
// Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
for(var j=0; j < NavFrame.childNodes.length; j++) {
if (hasClass(NavFrame.childNodes[j], "NavHead")) {
NavToggle.style.color = NavFrame.childNodes[j].style.color;
NavFrame.childNodes[j].appendChild(NavToggle);
}
}
NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
}
}
tabSystem_selectTab(titles, contents, selectedTab);
}
}
}
}
mw.hook( 'wikipage.content' ).add( tabSystem_implementTabs );
$( createNavigationBarToggleButton );
 
/*****************************************************************************************
* Function that adds Dark/Default Mode button for non logged-in users
* (logged-in users can select their skin in Parameters)
* Maintainer: [[User:Lou Montana]]
*/


/*****************************************************************************************/
const SKIN_USAGE = 'useskin=';


/*****************************************************************************************
function addDarkOrDefaultModeButton() {
* Main Page layout fixes
const DARKMODE_SKIN = 'darkvector';
*
const DARKMODE_SKIN_USAGE = SKIN_USAGE + DARKMODE_SKIN;
* Description: Adds an additional link to the complete list of languages available.
* Maintainers: [[User:AzaToth]], [[User:R. Koot]], [[User:Alex Smotrov]]
*/


if (wgPageName == 'Main_Page' || wgPageName == 'Talk:Main_Page') {
var leftTabHolder = document.getElementById('p-namespaces').getElementsByTagName('ul')[0]; // <ul>
$(function () {
if (leftTabHolder == null)
mw.util.addPortletLink('p-lang', '//meta.wikimedia.org/wiki/List_of_Wikipedias',
return;
'Complete list', 'interwiki-completelist', 'Complete list of Wikipedias');
});
}


/*****************************************************************************************/
var firstButton = leftTabHolder .children[0]; // <li>
if (firstButton == null)
return; // quick'n'not that dirty


/*****************************************************************************************
var buttonName;
* Table sorting fixes ************************************************
var title;
*
var href = document.documentURI;
* Description: Disables code in table sorting routine to set classes on even/odd rows
if (href.indexOf(DARKMODE_SKIN_USAGE) !== -1)
* Maintainers: [[User:Random832]]
{
*/
buttonName = 'Default Mode';
title = 'Go back to the default skin';
ts_alternate_row_colors = false;
href = href.replace(/[?&]useskin=[a-zA-Z0-9_]+/, '');
}
else // not in URL
{
buttonName = 'Dark Mode';
title = 'Use Dark Mode';
if (href.includes('?'))
href += '&' + DARKMODE_SKIN_USAGE;
else
href += '?' + DARKMODE_SKIN_USAGE;
}


/*****************************************************************************************/
var newButton = firstButton.cloneNode(true),
link = newButton.getElementsByTagName('a')[0];


/*****************************************************************************************
newButton.removeAttribute('class');
* uploadwizard_newusers
link.removeAttribute('accesskey');
* Switches in a message for non-autoconfirmed users at [[Wikipedia:Upload]]
link.removeAttribute('rel');
*
link.setAttribute('title', title);
* Maintainers: [[User:Krimpet]]
link.setAttribute('href', href);
*/
link.innerHTML = buttonName;
leftTabHolder.appendChild(newButton);
function uploadwizard_newusers() {
if (wgNamespaceNumber == 4 && wgTitle == "Upload" && wgAction == "view") {
var oldDiv = document.getElementById("autoconfirmedusers"),
newDiv = document.getElementById("newusers");
if (oldDiv && newDiv) {
if (typeof wgUserGroups == "object" && wgUserGroups) {
for (i = 0; i < wgUserGroups.length; i++) {
if (wgUserGroups[i] == "autoconfirmed") {
oldDiv.style.display = "block";
newDiv.style.display = "none";
return;
}
}
}
oldDiv.style.display = "none";
newDiv.style.display = "block";
return;
}
}
}
}
$(uploadwizard_newusers);


/*****************************************************************************************/
/*****************************************************************************************
* Function that adds the currently-used 'useskin' URL parameter to all relevant links in the page
* Maintainer: [[User:Lou Montana]]
*/


/*****************************************************************************************
function setAllLinksToUseForcedSkin() {
* IPv6 AAAA connectivity testing
var usedSkin = mw.util.getParamValue('useskin');
*
if (usedSkin == null || usedSkin == '')
return;
var __ipv6wwwtest_factor = 100;
 
var __ipv6wwwtest_done = 0;
var allLinks = document.getElementsByTagName('a');
if ((wgServer != "https://secure.wikimedia.org") && (Math.floor(Math.random()*__ipv6wwwtest_factor)==42)) {
var link;
importScript("MediaWiki:Common.js/IPv6.js");
var href;
}
for (var i = 0, count = allLinks.length - 1; i < count; i++)
{
link = allLinks[i];
href = link.href;
if (href == null || href == '')
continue;


/*****************************************************************************************/
// only httpS wiki links, no javascript:, no #, no enfusion:// or other protocol
if (!(href.startsWith('https://community.bistudio.com/') || href.startsWith('https://community.bohemia.net/') || href.startsWith('/wiki/')))
continue;


/*****************************************************************************************
if (href.indexOf(SKIN_USAGE) !== -1)
* Magic editintros ****************************************************
continue; // already has a skin assigned
*
* Description: Adds editintros on disambiguation pages and BLP pages.
* Maintainers: [[User:RockMFR]]
*/


function addEditIntro( name ) {
var poundIndex = href.indexOf('#');
$( '.editsection, #ca-edit' ).find( 'a' ).each( function( i, el ) {
if (href.indexOf('?') !== -1)
el.href = $(this).attr("href") + '&editintro=' + name;
{
});
if (poundIndex !== -1)
}
href = href.replace('#', '&' + SKIN_USAGE + usedSkin + '#');
else
if (wgNamespaceNumber === 0) {
href += '&' + SKIN_USAGE + usedSkin;
$(function(){
if (document.getElementById('disambigbox')) {
addEditIntro('Template:Disambig_editintro');
}
}
});
else // no ?x=y parameters
{
$(function(){
if (poundIndex !== -1)
var cats = document.getElementById('mw-normal-catlinks');
href = href.replace('#', '?' + SKIN_USAGE + usedSkin + '#');
if (!cats) {
else
return;
href += '?' + SKIN_USAGE + usedSkin;
}
}
cats = cats.getElementsByTagName('a');
for (var i = 0; i < cats.length; i++) {
if (cats[i].title == 'Category:Living people' || cats[i].title == 'Category:Possibly living people') {
addEditIntro('Template:BLP_editintro');
break;
}
}
});
}


/*****************************************************************************************/
link.href = href;
 
}
/*****************************************************************************************
* Description: Stay on the secure server as much as possible
* Maintainers: [[User:TheDJ]]
*/
 
if ( mw.config.get('wgServer') == 'https://secure.wikimedia.org' ) {
/* Old secure server */
importScript( 'MediaWiki:Common.js/secure.js');
} else if ( document.location && document.location.protocol  && document.location.protocol == "https:" ) {
/* New secure servers */
importScript('MediaWiki:Common.js/secure new.js');
}
}


/*****************************************************************************************/
// if 'useskin' is used, apply it to all links
if (mw.util.getParamValue('useskin'))
mw.hook( 'wikipage.content' ).add( setAllLinksToUseForcedSkin );


/*****************************************************************************************
// no ES5 promise, use jQuery
* Text area function for the account creation process
$.getJSON('/wikidata/api.php?action=query&meta=userinfo&format=json').done(function(response) {
*/
if (response != null && response.query.userinfo.id === 0) { // not logged in
 
mw.hook( 'wikipage.content' ).add( addDarkOrDefaultModeButton );
jQuery(function() {
// hopefully -after- setAllLinksToUseForcedSkin so the button's URL is not changed by it
if (!(document.getElementById('signupuserpagefillmagic'))) return;
}
/*
* Puts an userpage edit-box inside a div with the ID 'signupuserpagefillmagic'
* Created for [[:outreach:Account Creation Improvement Project]] by
* [[:sv:User:Sertion]] on the behalf of [[:outreach:User:Hannibal]]
*
* Below are variables for internationalization. Please use \n for linebreaks
* and escape all single quotation marks (') with \'
*/
var preComment = '<!-- BELOW IS THE TEXT ABOUT YOU. YOU CAN CHANGE IT COMPLETELY OR IN PARTS AND THEN COME BACK TO IT. AFTER YOU ARE DONE, SCROLL DOWN A BIT FURTHER AND CLICK "SAVE PAGE".--\>{'+'{New user bar}}\n',
postComment = '\n\n<!-- NOW, CLICK THE "SAVE PAGE" BUTTON. CONGRATULATIONS, YOU\'VE JUST MADE YOUR FIRST EDIT TO WIKIPEDIA. --\>',
preSubmitButton ='Do not forget to click SAVE PAGE when you get to the next page!',
submitText = 'Create my user page for me now!',
SUPeditSummary = 'New user page through [[:outreach:Account Creation Improvement Project|Outreach:ACIP]]',
preFilltemplate = 'Replace this example text with information about you. \n\n';
/*
* The actual magic:
* Inserts a form with a single visible field that simulates the normal
* edit-field. It uses the variables from above to set a text example (pre
* filled), an automated edit summary and the label of the submit button.
* 'fakewpTextbox1' is used to hide the assembling of the final output that
* is made below.
*/
jQuery('#signupuserpagefillmagic').html('<form action="'+wgServer+wgScript+'?title='+wgFormattedNamespaces[2]+':'+wgUserName+'&action=submit" method="post"><textarea id="fakewpTextbox1" style="width:46em;height:20em;">'+preFilltemplate+'</textarea><textarea name="wpTextbox1" id="wpTextbox1" style="display:none;"></textarea><input type="hidden" name="wpSummary" id="wpSummary" value="'+SUPeditSummary+'" /><br/>'+preSubmitButton+'<br/><input type="submit" value="'+submitText+'"/></form>');
// Waits for the form to be submitted.
jQuery('#signupuserpagefillmagic form').live('submit',function(r) {
// Stops the form from submitting
r.preventDefault();
/*
* Uses the previously defined variables preComment and postComment
* to assemble the final output in the hidden textarea.
*/
$('#wpTextbox1').text( preComment + jQuery('#fakewpTextbox1').text() + postComment );
/*
* Submits the form.
* For unknown reasons jQuery('#signupuserpagefillmagic form').submit() crashes
* Firefox (only tested in version 4.0). This method seam to work cross browser.
*/
document.getElementById('signupuserpagefillmagic').getElementsByTagName('form')[0].submit();
});
});
});


/*****************************************************************************************/
}); /* END mw.loader WRAPPER */
 
/*****************************************************************************************
* Next ***
*/

Latest revision as of 00:46, 11 March 2024

/*****************************************************************************************
 * This JavaScript file will be loaded for all users on every page load
 * Its content must be kept as minimal as possible.
 *****************************************************************************************/

mw.loader.using( ['mediawiki.util', 'jquery.client'], function() { /* BEGIN mw.loader WRAPPER */

	/*****************************************************************************************
	 * Conditionally import further JavaScript for specific situations.
	 */

	function conditionallyImport() {
		if ( mw.config.get( 'wgAction' ) === 'edit' || mw.config.get( 'wgAction' ) === 'submit' ) {
			importScript( 'MediaWiki:Editing.js' );
			// mw.loader.load( '/wiki/index.php?title=MediaWiki:Editing.js&action=raw&ctype=text/javascript' ); // proper thing to do - for future reference
		}
		if ( mw.config.get( 'wgCanonicalNamespace' ) != 'MediaWiki' && mw.config.get( 'wgArticleId' ) === 0 ) {
			importScript( 'MediaWiki:Creation.js' );
		}
		if ( mw.config.get( 'wgPageName' ) === 'Special:Version' ) {
			importScript( 'MediaWiki:SpecialVersion.js' );
		}
	}
	mw.hook( 'wikipage.content' ).add( conditionallyImport );

	/*****************************************************************************************
	 * Add a Purge button
	 * Maintainer: [[User:Lou Montana]]
	 */

	function offerPurgeButton() {
		var protectButton = document.getElementById('ca-protect');
		if (protectButton == null) {
			protectButton = document.getElementById('ca-unprotect');
			if (protectButton == null) {
				return; // quick'n'not that dirty
			}
		}
		var newButton = protectButton.cloneNode(true),
			link = newButton.getElementsByTagName('a')[0];
		link.removeAttribute('accesskey');
		link.setAttribute('title', 'Purge the page\'s cache');
		link.setAttribute('href', document.location.href + (document.location.href.indexOf('?') === -1 ? '?action=purge' : '&action=purge'));
		link.innerHTML = 'Purge';
		protectButton.parentNode.appendChild(newButton);
	}
	mw.hook( 'wikipage.content' ).add( offerPurgeButton );

	/*****************************************************************************************
	 * Add a Fix access button
	 * Maintainer: [[User:Lou Montana]]
	 */

	function offerDeleteSessionCookieButton() {
		var protectButton = document.getElementById('ca-protect');
		if (protectButton == null) {
			protectButton = document.getElementById('ca-unprotect');
			if (protectButton == null) {
				return; // quick'n'not that dirty
			}
		}
		var newButton = protectButton.cloneNode(true),
			link = newButton.getElementsByTagName('a')[0];
		link.removeAttribute('accesskey');
		link.setAttribute('title', 'Delete the community_session cookie to fix the DbQueryError issue');
		link.setAttribute('href', '/clearcookie.php?url=' + document.location.href);
		link.innerHTML = 'Fix access';
		protectButton.parentNode.appendChild(newButton);
	}
	// mw.hook( 'wikipage.content' ).add( offerDeleteSessionCookieButton ); // disabled for now, -should- be fixed

	/*****************************************************************************************
	 * Disable Talk pages
	 * Maintainer: [[User:Lou Montana]]
	 */

	function disableTalkPages() {
		if ($.inArray('sysop', mw.config.get('wgUserGroups')) === -1) { // not admin

			if (mw.config.get('wgCanonicalNamespace').indexOf('talk') !== -1 && // talk page
				mw.config.get('wgNamespaceNumber') !== 3) { // User Talk exception

				if (document.getElementById('ca-edit') != null) {
					document.getElementById('ca-edit').remove();
				}

				if (document.getElementById('ca-addsection') != null) {
					document.getElementById('ca-addsection').remove();
				}
			}

			var talkTab = document.getElementById('ca-talk');
			if (talkTab != null && talkTab.classList.contains('new')) {
				talkTab.remove();
			}
		}
	}
	mw.hook( 'wikipage.content' ).add( disableTalkPages );

	/*****************************************************************************************
	 * Function that adds a Sandbox button
	 * Maintainer: [[User:Lou Montana]]
	 */

	function addSandboxButton() {
		var userpageLink = document.getElementById('pt-userpage');
		var talkLink = document.getElementById('pt-mytalk');
		if (talkLink == null) {
			return;
		}
		var sandboxLink = talkLink.cloneNode(true),
			link = sandboxLink.getElementsByTagName('a')[0];
		link.removeAttribute('accesskey');
		link.setAttribute('title', 'Your Sandbox');
		link.setAttribute('href', userpageLink.getElementsByTagName('a')[0].href + '/Sandbox');
		link.innerHTML = 'Sandbox';
		talkLink.parentNode.insertBefore(sandboxLink, talkLink);
	}
	mw.hook( 'wikipage.content' ).add( addSandboxButton );

	/*****************************************************************************************
	 * Functions that adds Tabs
	 * Maintainer: [[User:Lou Montana]]
	 */

	function tabSystem_selectTab(titles, contents, tabIndex) {

		var i;
		for (i = 0; i < titles.length; i++) {
			titles[i].classList.remove('selected');
		}
		for (i = 0; i < contents.length; i++) {
			contents[i].style.display = 'none';
		}

		if (tabIndex < 0 || tabIndex > titles.length -1 || tabIndex > contents.length -1) {
			return;
		}
		titles[tabIndex].classList.add('selected');
		contents[tabIndex].style.display = 'block';
	}

	function tabSystem_implementTabs() {

		var tabBlocks = document.getElementsByClassName('biki-tabs');

		for (var i = 0; i < tabBlocks.length; i++) {

			var tabBlock = tabBlocks[i];

			var titles = tabBlock.getElementsByClassName('biki-tab-title');
			var contents = tabBlock.getElementsByClassName('biki-tab-content');
			if (titles.length < 1 || contents.length < 1 || titles.length != contents.length) {
				continue;
			}

			for (var j = 0; j < titles.length; j++) {
				(function(titles, contents, index) {
					titles[j].addEventListener('click', function () { tabSystem_selectTab(titles, contents, index) })
				})(titles, contents, j);
			}

			var selectedTab = 0;
			if (tabBlock.dataset && tabBlock.dataset.selectedtab) {
				var dataSelectedTab = +tabBlock.dataset.selectedtab;
				if (!isNaN(dataSelectedTab)) {
					selectedTab = Math.max(0, Math.min(dataSelectedTab, titles.length -1));
				}
			}

			tabSystem_selectTab(titles, contents, selectedTab);
		}
	}
	mw.hook( 'wikipage.content' ).add( tabSystem_implementTabs );

	/*****************************************************************************************
	 * Function that adds Dark/Default Mode button for non logged-in users
	 * (logged-in users can select their skin in Parameters)
	 * Maintainer: [[User:Lou Montana]]
	 */

	const SKIN_USAGE = 'useskin=';

	function addDarkOrDefaultModeButton() {
		const DARKMODE_SKIN = 'darkvector';
		const DARKMODE_SKIN_USAGE = SKIN_USAGE + DARKMODE_SKIN;

		var leftTabHolder = document.getElementById('p-namespaces').getElementsByTagName('ul')[0]; // <ul>
		if (leftTabHolder == null)
			return;

		var firstButton = leftTabHolder .children[0]; // <li>
		if (firstButton == null)
			return; // quick'n'not that dirty

		var buttonName;
		var title;
		var href = document.documentURI;
		if (href.indexOf(DARKMODE_SKIN_USAGE) !== -1)
		{
			buttonName = 'Default Mode';
			title = 'Go back to the default skin';
			href = href.replace(/[?&]useskin=[a-zA-Z0-9_]+/, '');
		}
		else // not in URL
		{
			buttonName = 'Dark Mode';
			title = 'Use Dark Mode';
			if (href.includes('?'))
				href += '&' + DARKMODE_SKIN_USAGE;
			else
				href += '?' + DARKMODE_SKIN_USAGE;
		}

		var newButton = firstButton.cloneNode(true),
			link = newButton.getElementsByTagName('a')[0];

		newButton.removeAttribute('class');
		link.removeAttribute('accesskey');
		link.removeAttribute('rel');
		link.setAttribute('title', title);
		link.setAttribute('href', href);
		link.innerHTML = buttonName;
		leftTabHolder.appendChild(newButton);
	}

	/*****************************************************************************************
	 * Function that adds the currently-used 'useskin' URL parameter to all relevant links in the page
	 * Maintainer: [[User:Lou Montana]]
	 */

	function setAllLinksToUseForcedSkin() {
		var usedSkin = mw.util.getParamValue('useskin');
		if (usedSkin == null || usedSkin == '')
			return;

		var allLinks = document.getElementsByTagName('a');
		var link;
		var href;
		for (var i = 0, count = allLinks.length - 1; i < count; i++)
		{
			link = allLinks[i];
			href = link.href;
			if (href == null || href == '')
				continue;

			// only httpS wiki links, no javascript:, no #, no enfusion:// or other protocol
			if (!(href.startsWith('https://community.bistudio.com/') || href.startsWith('https://community.bohemia.net/') || href.startsWith('/wiki/')))
				continue;

			if (href.indexOf(SKIN_USAGE) !== -1)
				continue; // already has a skin assigned

			var poundIndex = href.indexOf('#');
			if (href.indexOf('?') !== -1)
			{
				if (poundIndex !== -1)
					href = href.replace('#', '&' + SKIN_USAGE + usedSkin + '#');
				else
					href += '&' + SKIN_USAGE + usedSkin;
			}
			else // no ?x=y parameters
			{
				if (poundIndex !== -1)
					href = href.replace('#', '?' + SKIN_USAGE + usedSkin + '#');
				else
					href += '?' + SKIN_USAGE + usedSkin;
			}

			link.href = href;
		}
	}

	// if 'useskin' is used, apply it to all links
	if (mw.util.getParamValue('useskin'))
		mw.hook( 'wikipage.content' ).add( setAllLinksToUseForcedSkin );

	// no ES5 promise, use jQuery
	$.getJSON('/wikidata/api.php?action=query&meta=userinfo&format=json').done(function(response) {
		if (response != null && response.query.userinfo.id === 0) {	// not logged in
			mw.hook( 'wikipage.content' ).add( addDarkOrDefaultModeButton );
			// hopefully -after- setAllLinksToUseForcedSkin so the button's URL is not changed by it
		}
	});

}); /* END mw.loader WRAPPER */