MediaWiki:Common.js

A Madoka Magica Wiki: 《魔法少女小圆》中文百科全书。
跳转至: 导航搜索
/* 此处的JavaScript将加载于所有用户每一个页面。 */

/* ----- Modify Sidebar ----- */
function addInputFormToSidebar() {
  theNode = document.getElementById('p-.E5.B7.A5.E5.85.B7.E7.AE.B12').getElementsByTagName('div')[0].getElementsByTagName('ul')[0];
  aInput = document.createElement('input');
  aInput.name='title';
  aInput.size="15";
  aInput.placeholder="条目名称";
  aButton = document.createElement('button');
  aButton.type='submit';
  aButton.style="visibility: hidden;";
  aForm = document.createElement('form');
  aForm.action='/index.php';
  aForm.target='_blank';
  aForm.appendChild(aInput);
  aForm.appendChild(aButton);
  liNode = document.createElement('li');
  liNode.appendChild(aForm);
  theNode.appendChild(liNode);
}
addOnloadHook( addInputFormToSidebar );

/* ----- Modify Sidebar Toolbox ----- */
/* copied from http://www.mediawiki.org/wiki/Manual:Interface/Sidebar#Add_or_remove_sections_.28JavaScript.29 */
/* 不再使用 */
/* function ModifySidebar( action, section, name, link ) {
	try {
		switch ( section ) {
			case 'languages':
				var target = 'p-lang';
				break;
			case 'toolbox':
				var target = 'p-tb';
				break;
			case 'navigation':
				var target = 'p-navigation';
				break;
			default:
				var target = 'p-' + section;
				break;
		}
 
		if ( action == 'add' ) {
			var node = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];
 			var aNode = document.createElement( 'a' );
			var liNode = document.createElement( 'li' );
 			aNode.appendChild( document.createTextNode( name ) );
			aNode.setAttribute( 'href', link );
			liNode.appendChild( aNode );
			liNode.className = 'plainlinks';
			node.appendChild( liNode );
		}
 
		if ( action == 'remove' ) {
			var list = document.getElementById( target )
							   .getElementsByTagName( 'div' )[0]
							   .getElementsByTagName( 'ul' )[0];
 			var listelements = list.getElementsByTagName( 'li' );
 			for ( var i = 0; i < listelements.length; i++ ) {
				if (
					listelements[i].getElementsByTagName( 'a' )[0].innerHTML == name ||
					listelements[i].getElementsByTagName( 'a' )[0].href == link
				)
				{
					list.removeChild( listelements[i] );
				}
			}
		}
	} catch( e ) {
		// let's just ignore what's happened
		return;
	}
}

function CustomizeModificationsOfSidebar() {
	ModifySidebar('add', 'toolbox', '清除服务器缓存', 'http://wiki.madomagi.info/index.php?title='+document.getElementById('firstHeading').firstChild.innerHTML+"&action=purge");
	//ModifySidebar( 'remove', 'toolbox', 'Upload file', 'http://en.wikipedia.org/wiki/Special:Upload' );
}
addOnloadHook( CustomizeModificationsOfSidebar );  */


/* --------- one-click broken link report ------- */
/* cf: https://www.mediawiki.org/wiki/API:Edit */
/* cf: https://www.mediawiki.org/wiki/API:Tokens */
function emailUploader( content, user, editToken ) {
    $.ajax({
      url: 'http://wiki.madomagi.info/api.php',
      data: 'action=query&list=users&ususers='+encodeURIComponent(user)+'&usprop=emailable&format=json',
      dataType: 'json',
      success: function( data ) {
        if ( typeof data.query.users[0]['emailable'] !== 'undefined' ) {
          emailable = true;
        } else {
          emailable = false;
        }
      }
    });
  if (emailable) {
    $.ajax({
        url: 'http://wiki.madomagi.info/api.php',
        data: {
            format: 'json',
            action: 'emailuser',
            target: user,
            subject: '坏链求补档',
            text: content,
            token: editToken
        },
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
            if ( data && data.emailuser && data.emailuser.result == 'Success' ) {
                alert( '成功向'+user+'发送邮件提醒补档' );
            } else if ( data && data.error ) {
                alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
            } else {
                alert( 'Error: Unknown result from API.' );
            }
        },
        error: function( xhr ) {
            alert( 'Error: Request failed.' );
        }
    });
  }
}

function appendReport( content, editToken ) {
    $.ajax({
        url: 'http://wiki.madomagi.info/api.php',
        data: {
            format: 'json',
            action: 'edit',
            title: '坏链报告',
            appendtext: content,
            token: editToken
        },
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
            if ( data && data.edit && data.edit.result == 'Success' ) {
                alert( '成功添加到[[坏链报告]]条目' );
            } else if ( data && data.error ) {
                alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
            } else {
                alert( 'Error: Unknown result from API.' );
            }
        },
        error: function( xhr ) {
            alert( 'Error: Request failed.' );
        }
    });
}

function obtainToken(f) {
    $.ajax({
        url: 'http://wiki.madomagi.info/api.php',
        data: {
            format: 'json',
            action: 'tokens'
        },
        dataType: 'json',
        type: 'GET',
        success: f,
        error: function( xhr ) {
            alert( 'Error: Request failed' );
        }
    })
}

function brokenLinkReport(text, contact) {
    if (confirm('确定要报告这链接坏了?')) {
        obtainToken(function( data ) {
            if (data && data.tokens && data.tokens.edittoken.length==34) {
                appendReport( "\r\n* "+text, data.tokens.edittoken );
                if (contact!=="" && contact!=="undefined") {emailUploader(text, contact, data.tokens.edittoken );}
            } else {
                alert( 'Error: cannot retrieve edit token' );
            }
        });
    }
}

//mw.loader.load( 'jquery.ui.brokenLinkGuard' ); 

function renderBrokenLinkGuard() {
    pagename = document.getElementById("firstHeading").firstChild.innerHTML;
    $('.brokenLinkGuard').after(function() {
        s = "<a href='#' onclick='brokenLinkReport(\""+(new Date().toLocaleDateString()+" [["+pagename+"]] ["+this.firstChild.href+" "+this.firstChild.innerHTML).replace(/\\/g, "\\\\").replace(/"/g, "\\\"")+"]\", \"";
        if (this.nextSibling!=null) {
            s = s+this.nextSibling.innerHTML.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
        }
        s = s+"\");'><small>(点击报告坏链)</small></a>";
        return s;
    });
}

addOnloadHook( renderBrokenLinkGuard );
//jQuery( document ).ready( function( $ ) {
//        mw.loader.using( 'jquery.ui.brokenLinkGuard', function() {renderBrokenLinkGuard();} );
//} );

// Popups
/* disabled
// changed to reference tooltips: http://en.wikipedia.org/wiki/User_talk:Yair_rand/ReferenceTooltips
// translation
// ref: http://en.wikipedia.org/wiki/Wikipedia:Tools/Navigation_popups/Translation
mw.loader.load("//en.wikipedia.org/w/index.php?title=User:Theslboat/Popups-strings-chs.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400");
// ref: http://en.wikipedia.org/wiki/Wikipedia:Tools/Navigation_popups
mw.loader.load("//en.wikipedia.org/w/index.php?title=User:Lupin/popups.js&action=raw&ctype=text/javascript&smaxage=21600&maxage=86400");

// window.popupSubpopups = false;
window.popupImages = false;
window.popupOnEditSelection = false;
window.popupImageLinks = false;
*/


// 展开/折叠 所有 封面/插图
function expandOrCollapse() {
	$("#expand-all-cover").click(function(){ $("table.cover span.mw-collapsible-toggle-collapsed a").click();});
	$("#collapse-all-cover").click(function(){ $("table.cover span.mw-collapsible-toggle-expanded a").click();});
	$("#expand-all-illust").click(function(){ $("table.illust span.mw-collapsible-toggle-collapsed a").click();});
	$("#collapse-all-illust").click(function(){ $("table.illust span.mw-collapsible-toggle-expanded a").click();});
};
addOnloadHook( expandOrCollapse );