User:Sreejithk2000/Cosmetics.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.
if (mw.config.get("wgAction") == "edit" && mw.config.get("wgCanonicalNamespace") == "File") {
	var wpTextBox1 = $('#wpTextbox1').val();
	wpTextBox1 = wpTextBox1.replace(/==\s*Summary\s*==/g, '== {{int:filedesc}} ==');
	wpTextBox1 = wpTextBox1.replace(/==\s*Licensing\s*==/g, '== {{int:license-header}} ==');
	wpTextBox1 = wpTextBox1.replace(/==\s*{{int:license}}\s*==/g, '== {{int:license-header}} ==');
	wpTextBox1 = wpTextBox1.replace(/==\s*License information\s*==/g, '== {{int:license-header}} ==');

    wpTextBox1 = FixMalayalamInformationTemplate(wpTextBox1);
    
	if (wpTextBox1.indexOf("== {{int:filedesc}} ==") < 0 && wpTextBox1.indexOf("=={{int:filedesc}}==") < 0 && wpTextBox1.indexOf("{Information") > 0) 
	{
		wpTextBox1 = "== {{int:filedesc}} ==\n" + wpTextBox1;
	}

	wpTextBox1 = FixLanguageLinks('w', 'w', 'User', wpTextBox1);
	wpTextBox1 = FixLanguageLinks('en', 'w', 'User', wpTextBox1);
	wpTextBox1 = FixLanguageLinks('ml', 'വി', 'ഉപയോക്താവ്', wpTextBox1);

	wpTextBox1 = FixHyperlinks(wpTextBox1);
	if (wpTextBox1.indexOf("Category") < 0 && wpTextBox1.indexOf("{{Uncategorized") < 0 && wpTextBox1.trim() != '') 
	{
		wpTextBox1 = wpTextBox1 + "\n{" + "{subst:unc}}";
	}
	
	$('#wpTextbox1').val(wpTextBox1);
	
    // Trigger cleanup JS (fast, unsafe)
    if(location.href.indexOf('mto_cleanup') == -1) { $('#p-cl a:first').click(); }
}

function FixLanguageLinks(lang, langLink, userNS, currentText) {
	// Finds all strings that match [[:lang:Blah Blah|Blah]] excepts User and Image links.
	var pattern = '\\[\\[:' + lang + ':((?!(\\[\\[|(User)|(Image)|(File)|(Category))).)*\\]\\]';
	var regex = new RegExp(pattern, 'gi');
	var matchedValues = currentText.match(regex);
	if (matchedValues != null) {
		matchedValues.forEach(function(val) {
			var insideVal = val.substring(lang.length + 4, val.length - 2);
			var valArray = insideVal.split('|');
			var newVal;

			var userRegex = new RegExp(userNS, 'gi');
			if (valArray[0].match(userRegex)) return true;

			if (valArray.length == 1) newVal = '{{' + langLink + '|' + valArray[0] + '}}';
			else if (valArray.length == 2) {
				if (valArray[0] == valArray[1]) newVal = '{{' + langLink + '|' + valArray[0] + '}}';
				else newVal = '{{' + langLink + '|' + valArray[0] + '|' + valArray[1] + '}}';
			}
			currentText = currentText.replace(val, newVal);
		});
	}
	
	return currentText;
}

function FixMalayalamInformationTemplate(wpTextBox1)
{
	wpTextBox1 = wpTextBox1.replace(/==\s*ചുരുക്കം\s*==/g, '== {{int:filedesc}} ==');
	wpTextBox1 = wpTextBox1.replace(/==\s*പകർപ്പാവകാശം\s*==/g, '== {{int:license-header}} ==');
	wpTextBox1 = wpTextBox1.replace(/{{വിവരങ്ങൾ/g, '{{Information');
	wpTextBox1 = wpTextBox1.replace(/\|വിവരണം/g, '|Description');
	wpTextBox1 = wpTextBox1.replace(/\|ഉറവിടം/g, '|Source');
	wpTextBox1 = wpTextBox1.replace(/\|തിയ്യതി/g, '|Date');
	wpTextBox1 = wpTextBox1.replace(/\|രചയിതാവ്/g, '|Author');
	wpTextBox1 = wpTextBox1.replace(/\|മറ്റു_പതിപ്പുകൾ/g, '|Other versions');
	return wpTextBox1;
}

function FixHyperlinks(wpTextBox1)
{
	const regex = /<a href="(.*?)".*?>(.*?)<\/a>/gis;
	const subst = `[$1 $2]`;
	return wpTextBox1.replace(regex, subst);
}