MediaWiki:Common.js
From MediaWiki
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Blatantly based on Wookiepedia's ShowEras */
function showTitles(className) {
if(typeof(SKIP_ERAS) != 'undefined' && SKIP_ERAS)
return;
var titleDiv = document.getElementById(className);
if(titleDiv == null || titleDiv == undefined)
return;
var cloneNode = titleDiv.cloneNode(true);
var firstHeading = getFirstHeading();
firstHeading.insertBefore(cloneNode, firstHeading.childNodes[0]);
cloneNode.style.display = "block";
}// END JavaScript title rewrite
function getFirstHeading(){
//var a = getElementsByClassName(document.getElementById('content'), 'h1', 'firstHeading');
var a = document.getElementById('content').getElementsByTagName('h1');
return a[0];
}
/* Code for hiding segments */
function hide_block(id) {
document.getElementById(id).style.display = "inline";
document.getElementById(id + "-link").style.display = "none";
}
function replace_hides() {
//var hide_blocks = getElementsByClassName(document, "div", "hide");
var hide_blocks = document.getElementsByClassName('hide');
for (var i=0;i<hide_blocks.length;i++) {
var hide_link = document.createElement("div");
hide_link.innerHTML = "<a href='javascript:hide_block(\"" + hide_blocks[i].getAttribute("id") + "\");' id='" + hide_blocks[i].getAttribute("id") + "-link'><i>Show hidden content.</i></a>";
//hide_link.innerHTML = "<a href='javascript:hide_block("" + hide_blocks[i].getAttribute("id") + "");' id='" + hide_blocks[i].getAttribute("id") + "-link'><i>Show hidden content.</i></a>";
hide_blocks[i].parentNode.insertBefore(hide_link, hide_blocks[i]);
hide_blocks[i].style.display = "none";
}
}
function tfwiki_hooks() {
showTitles('title-factionicons');
replace_hides();
}
$( document ).ready(tfwiki_hooks);
/* ========== Upload preview ==========
Trying mw.Api */
$( function () {
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Upload' ) { return; }
var $desc = $( '#wpUploadDescription' );
if ( !$desc.length ) { return; }
var $btn = $( '<input>', { type: 'button', value: 'Preview' } );
var $preview = $( '<div>', { id: 'wikiPreview' } ).hide();
$desc.after( $preview ).after( $btn );
var api = new mw.Api();
$btn.on( 'click', function () {
var dest = $( '#wpDestFile' ).val() || 'Example.png';
$btn.prop( 'disabled', true );
$preview.text( 'Loading…' ).show();
api.post( {
action: 'parse',
text: $desc.val(),
title: 'File:' + dest,
contentmodel: 'wikitext',
prop: 'text|categorieshtml',
disablelimitreport: 1,
formatversion: 2
} ).then( function ( data ) {
var html = ( data.parse && data.parse.text ) || '';
var cats = ( data.parse && data.parse.categorieshtml ) || '';
$preview.html( html + cats );
}, function ( code, result ) {
$preview.text( 'Preview failed: ' + code +
( result && result.error ? ' — ' + result.error.info : '' ) );
} ).always( function () {
$btn.prop( 'disabled', false );
} );
} );
} );
/* Code for showing/hiding voice actor credits */
function showcredits(num) {
var spantohide=document.getElementById("ocreditl"+num)
spantohide.style.display="none";
var spantoshow=document.getElementById("ocredit"+num)
spantoshow.style.display="inline";
}
function hidecredits(num) {
var spantohide=document.getElementById("ocreditl"+num);
spantohide.style.display="inline";
var spantoshow=document.getElementById("ocredit"+num);
spantoshow.style.display="none";
}
function init_voiceactors(){
var list = document.getElementsByTagName("span");
for (var ii = 0; ii < list.length; ii++) {
if(list[ii].className=="ocreditspan") {
list[ii].innerHTML="<span id=\"ocreditl"+ii+"\"><a href=\"#\" onclick=\"showcredits("+ii+"); return false;\">more»</a></span><span id=\"ocredit"+ii+"\" style=\"display: none\">"+list[ii].innerHTML+" <a href=\"#\" onclick=\"hidecredits("+ii+"); return false;\">«less</a></span>";
}
}
}
$( document ).ready( init_voiceactors);
/* End of code for showing/hiding voice actor credits */
/* === TOC collapse/expand animation === */
$( document ).ready( function () {
var $ul = $( '#toc > ul' );
var $checkbox = $( '#toc .toctogglecheckbox' );
if ( !$ul.length || !$checkbox.length ) { return; }
if ( $checkbox.prop( 'checked' ) ) { $ul.hide(); }
$checkbox.on( 'change', function () {
if ( $checkbox.prop( 'checked' ) ) {
$ul.stop( true, false ).slideUp( 250 );
} else {
$ul.stop( true, false ).slideDown( 250 );
}
} );
} );

