Jump to content

User:Bility/biographyListasGenerator.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if(mw.config.get('wgNamespaceNumber')==1 && (mw.config.get('wgAction')=='edit' || mw.config.get('wgAction')=='submit')) {
$(document).ready(function() {
	var pagetitle = mw.config.get('wgTitle');
	var text1 = '';
	var text2 = '';
	var textMatch = -1;
	var particleText = / (de|von|du|del|zu|di|van|na) /i;
	if(pagetitle.search(" ")>=0) {
		if(pagetitle.search("List of")==0) {
			text1=pagetitle.replace(/^List of /,'');
			text1=text1.substring(0,1).toUpperCase()+text1.substring(1);
		} else {
			pagetitle=pagetitle.replace(/\(.*?\)/g,'').replace(/ +/g,' ').replace(/ $/,'');
			if(pagetitle.search(",")>=0) {
				pagetitle=pagetitle.substring(0,pagetitle.search(","));
			};
			if(pagetitle.search(" of ")>=0) {
				pagetitle=pagetitle.substring(0,pagetitle.search(" of "));
			};
			if(particleText.test(pagetitle)) {
				textMatch=pagetitle.search(particleText);
				text1=pagetitle.substring(textMatch+1,pagetitle.length);
				text2=', '+pagetitle.substring(0,textMatch);
			} else {
        			if(pagetitle.search(" ")==-1) {
        				text1=pagetitle;
				} else {
        				textMatch=pagetitle.search(/ [^ ]*?$/);
        				text1=pagetitle.substring(textMatch+1,pagetitle.length);
        				text2=', '+pagetitle.substring(0,textMatch);
				};
			};
		};
	};
	var listasButton = document.createElement('img');
	listasButton.width=23;
	listasButton.height=22;
	listasButton.className='mw-toolbar-editbutton';
	listasButton.id='wp-biog-listas-button';
	listasButton.src='http://upload.wikimedia.org/wikipedia/commons/c/cf/Button_encadr%C3%A9.png';
	listasButton.border=0;
	listasButton.title='Insert listas sort name';
	listasButton.style='cursor:pointer';
	if(document.getElementById('toolbar')) {
		document.getElementById('toolbar').appendChild(listasButton);
	} else {
		document.getElementById('editform').parentNode.insertBefore(listasButton,document.getElementById('editform'));
	};
	$('img#wp-biog-listas-button').click( function() {
		var editText = $('textarea#wpTextbox1').attr('value');
		var regex = /({{[\s]*(?:WikiProject Biography|WPBiography|WPBIO|WP Biography|WP Bio|Bio)[^}]*?\|listas=).*?(\||}|\n)/im;
		if(editText.search(regex)>=0) {
			var replaceText = regex.exec(editText);
			editText = editText.replace(replaceText[0],replaceText[1]+text1+text2+replaceText[2]);
		};
		$('textarea#wpTextbox1').attr('value',editText);
	});
});
};