User:Gone Postal/prefill.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// This script will preload some specific pages on their first edit
// <nowiki>
mw.loader.load('mediawiki.util');

$ = jQuery;

function escapeRegex(string) {
    return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

function prefill()
{
	let pageName = mw.config.get( 'wgPageName' ).replace(/_/g, ' ');
	if(mw.config.get( 'wgAction' ) != 'edit' && document.editform.wpTextbox1.value != '')
	{/*
		if(pageName=='Special:WhatLinksHere/User:Florstein/Inf')
		{
			let list=document.getElementsByClassName('mw-whatlinkshere-tools');
			let i=0;
			for(let item of list)
			{
				let link = item.getElementsByTagName('a')[1];
				if(link.getAttribute('title').startsWith('File') && ++i < 300)
				{
					console.log(link);
					console.log(link.getAttribute('href'));
					window.open(link.getAttribute('href'));
				}
			}
		}
	*/
		return;
	}
	let territoriesWithNavBoxes =
	[
		'Auvergne-Rhône-Alpes',
		'Berlin', 'Brandenburg', 'Bonn', 'Bavaria', 'Baden-Württemberg',
		'Crimea',
		'Dresden', 'Düsseldorf', 'Darmstadt',
		'Frankfurt am Main',
		'Hesse', 'Hamburg',
		'Jersey',
		'Leipzig', 'Ludwigshafen am Rhein', 'Lower Saxony',
		'Macau', 'Mainz', 'Mitte (district of Berlin)', 'Munich', 'Mecklenburg-Vorpommern',
		'North Rhine-Westphalia', 'Nagoya',
		'Oberhausen',
		'Pankow (district of Berlin)',
		'Rhineland-Palatinate',
		'Saarbrücken', 'Saarland', 'Saxony', 'Saxony-Anhalt', 'Schleswig-Holstein', 'Speyer',
		'Trier', 'Thuringia',
		'Wiesbaden', 'Wuppertal'
	];
	let conditionsTerritories = [];
	territoriesWithNavBoxes.forEach((e) => {
		const escaped = escapeRegex(e);
		conditionsTerritories.push(
			{
				page: new RegExp('Category:'+escaped+' photographs taken on \\d{4}-\\d{2}-\\d{2}'),
				func: function() {
					let template = e+' photographs taken on navbox';
					let date = pageName.replace('Category:', '').replace(e+' photographs taken on ', '').trim();
					let year = date.substring(0,4);
					let month = date.substring(5, 7);
					let day = date.substring(8,10);
					document.editform.wpTextbox1.value = '{{'+template+'|'+year+'|'+month+'|'+day+'}}';
				}
			}
		);
	});
	const conditionsCountries = [
		{
			page: /Category:Number \d+ on stamps/,
			func: function() {
				let template = 'number on object';
				let o = 'stamp';
				let num = pageName.replace('Category:Number ', '').replace(' on stamps', '');
				document.editform.wpTextbox1.value = '{{'+template+'|o='+o+'|n='+num+'}}';
			}
		}
		,
		{
			page: /^Category:\d{4} stamps of .*/,
			func: function() {
				let template = 'year stamps of country';
				let year = pageName.replace('Category:', '').substring(0,4);
				let country = pageName.substring(24);
				document.editform.wpTextbox1.value = '{{'+template+'|'+year+'|'+country+'}}';
			}
		}
		,
		{
			page: /Category:.* on stamps/,
			func: function() {
				let subject = pageName.replace('Category:', '').replace(' on stamps', '');
				document.editform.wpTextbox1.value = '[[Category:'+subject+' in art|stamps]]\n[[Category:Stamps by subject|'+subject+']]';
			}
		}
		,
		{
			page: /Category:.* in art/,
			func: function() {
				let subject = pageName.replace('Category:', '').replace(' in art', '');
				document.editform.wpTextbox1.value = '[[Category:'+subject+'|*Art]]\n[[Category:Art by subject|'+subject+']]';
			}
		}
		,
		{
			page: /Category:.+ photographs taken on \d{4}-\d{2}-\d{2}/,
			func: function() {
				let template = 'country photographs taken on';
				let country = pageName.replace('Category:', '').replace('photographs taken on', '').replace(/\d{4}-\d{2}-\d{2}$/, '').trim();
				let date = pageName.replace('Category:', '').replace(country, '').replace(' photographs taken on ', '').trim();
				let year = date.substring(0,4);
				let month = date.substring(5, 7);
				let day = date.substring(8,10);
				document.editform.wpTextbox1.value = '{{'+template+'|'+country+'|'+year+'|'+month+'|'+day+'}}';
			}
		}
		,
		{
			page: /Category:Photographs taken on \d{4}-\d{2}-\d{2}/,
			func: function() {
				let template = 'photographs taken on navbox';
				let date = pageName.substring(30).trim();
				let year = date.substring(0,4);
				let month = date.substring(5, 7);
				let day = date.substring(8,10);
				document.editform.wpTextbox1.value = '{{'+template+'|'+year+'|'+month+'|'+day+'}}';
			}
		}
		,
		{
			page: /Category:[^ ]+ \d{4} .+ photographs/,
			func: function() {
				let template = 'countryphotomonthyear';
				let country = pageName.replace('Category:', '').replace(/[^ ]+/, '').replace(/\d{4}/, '').replace(' photographs', '').trim();
				let year = pageName.replace('Category:', '').replace(/[^ ]+/, '').replace(country, '').replace(' photographs', '').trim();
				let month = pageName.replace('Category:', '').replace(/\d{4}/, '').replace(country, '').replace(' photographs', '').trim();
				document.editform.wpTextbox1.value = '{{'+template+'|'+country+'|'+year+'|'+month+'}}';
			}
		}
		,
		{
			page: /Category:\d{4} photographs of .*/,
			func: function() {
				let template = 'countryphotoyear2';
				let country = pageName.replace('Category:', '').replace(/\d{4}/, '').replace('photographs of', '').trim();
				if(country.substring(0,4)=='the ')
				{
					country = country.substring(4);
				}
				let year = pageName.replace('Category:', '').substring(0,4);
				let decade = year.substring(0,3);
				let year1 = year.substring(3);
				document.editform.wpTextbox1.value = '{{'+template+'|'+country+'|'+decade+'|'+year1+'}}';
			}
		}
		,
		{
			page: /Category:\d{4}s photographs of .*/,
			func: function() {
				let template = 'countryphotodecade';
				let country = pageName.substring(30);
				if(country.substring(0,4)=='the ')
				{
					country = country.substring(4);
				}
				let decade = pageName.replace('Category:', '').substring(0,3);
				document.editform.wpTextbox1.value = '{{'+template+'|'+country+'|'+decade+'}}';
			}
		}
		,
		{
			page: /Category:\d{2}..-century photographs of .*/,
			func: function() {
				let template = 'countryphotocentury';
				let country = pageName.substring(37);
				if(country.substring(0,4)=='the ')
				{
					country = country.substring(4);
				}
				let century = pageName.replace('Category:', '').substring(0,2);
				document.editform.wpTextbox1.value = '{{'+template+'|'+country+'|'+century+'}}';
			}
		}
		,
		{
			page: /Category:Historical photographs of .*/,
			func: function() {
				let template = 'historical photographs of country';
				let country = pageName.substring(35);
				if(country.substring(0,4)=='the ')
				{
					country = country.substring(4);
				}
				document.editform.wpTextbox1.value = '{{'+template+'|'+country+'}}';
			}
		}
		,
		{
			page: /Category:Videos of \d{4} from .*/,
			func: function() {
				let template = 'videos from country by year';
				let country = pageName.substring(29);
				if(country.substring(0,4)=='the ')
				{
					country = country.substring(4);
				}
				let year = pageName.replace('Category:Videos of ', '').substring(0,4);
				let decade = year.substring(0,3);
				let year1 = year.substring(3);
				document.editform.wpTextbox1.value = '{{'+template+'|'+country+'|'+decade+'|'+year1+'}}';
			}
		}
		,
		{
			page: /Category:Number \d+ on vehicles/,
			func: function() {
				let template = 'numbercategory-vehicle';
				let num = pageName.substring(16).replace(' on vehicles', '');
				let type = 'vehicle';
				let supercat = 'number '+num+' on objects';
				let country = pageName.substring(29);
				if(country.substring(0,4)=='the ')
				{
					country = country.substring(4);
				}
				let year = pageName.replace('Category:Videos of ', '').substring(0,4);
				let decade = year.substring(0,3);
				let year1 = year.substring(3);
				document.editform.wpTextbox1.value = '{{'+template+'|'+num+'|'+type+'|'+supercat+'|'+type+'}}';
			}
		},
		{
			page: /Category:0+\d+ \(number\)/,
			func: function() {
				let template = 'number-zero-padded cat';
				let num = pageName.substring(9).replace(' (number)', '');
				let zeros = num.replace(/[^0].*$/, '').length;
				num = num.replace(/^0+/, '');
				document.editform.wpTextbox1.value = '{{'+template+'|'+num+'|'+zeros+'}}';
			}
		},
		{
			page: /Category:\d+ \(number\)/,
			func: function() {
				let template = 'number cat';
				let num = pageName.substring(9).replace(' (number)', '');
				document.editform.wpTextbox1.value = '{{'+template+'|'+num+'}}';
			}
		},
		{
			page: /Category:Video display resolution \d+ x \d+/,
			func: function() {
				let template = 'video display resolution';
				let width = pageName.substring(34).replace(/ x \d+/, '');
				let height = pageName.substring(34).replace(/\d+ x /, '');
				document.editform.wpTextbox1.value = '{{'+template+'|'+width+'|'+height+'}}';
				//document.editform.submit();
			}
		}/*
		,
		{
			page: /File:/,
			func: function() {
				let tb = document.editform.wpTextbox1.value;
				tb = tb.replace(/(\|Day=\d+)/, '$1\n|location=Russia');
				document.editform.wpTextbox1.value=tb;
				document.editform.wpSummary.value = 'location';
				document.editform.submit();
			}
		}*/
		,
		{
			page: /PAGENAME/,
			func: function() {
				
			}
		}
	];
	
	const conditions = conditionsTerritories.concat(conditionsCountries);

	conditions.some(function(cond) {
		if(cond.page.test(pageName))
		{
			cond.func();
			//document.editform.submit();
			return true;
		}
		return false;
	});
}
$(document).ready(function(){
	prefill();
/*
// jQuery UI is not loaded on all pages yet:
if (jQuery.ui == undefined) {
	$.getScript(wgServer + '/w/extensions/UsabilityInitiative/css/vector/jquery-ui-1.7.2.css');
	$.getScript(wgServer + '/w/extensions/UsabilityInitiative/js/js2stopgap/jui.combined.min.js', function(){
		prefill();
	});
} else {
	prefill();
}
*/
});
// </nowiki>