ผู้ใช้:ZilentFyld/creattest.js

จาก วิกิพจนานุกรม พจนานุกรมเสรี

หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง

  • ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
  • กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
  • อินเทอร์เน็ตเอกซ์พลอเรอร์ และ Edge: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
  • โอเปร่า: กด Ctrl-F5
/* <source lang="javascript"> */

/* FOR VECTOR SKIN ONLY */
/* This script works best with stylesheet: catcat-wiktionary.css */

/* Predefined variables */
var catcatVersion = '0.5.14';
var pageName = mw.config.get('wgPageName');
var pageTitle = mw.config.get('wgTitle');
var indexUrl = mw.util.wikiScript('index');
var apiUrl = mw.util.wikiScript('api');
var commonsUrl = '//upload.wikimedia.org/wikipedia/commons';

var restrictionEdit = mw.config.get('wgRestrictionEdit');
var editable = (restrictionEdit === null || restrictionEdit.length === 0 ||
	mw.config.get('wgUserGroups').indexOf(restrictionEdit[0]) > -1);

/* legacyThai, wikiNamespace, wikiNamespaceKeys, replaceHeading, replaceHeadingKeys, categoryPattern */
mw.loader.load('//th.wiktionary.org/w/index.php?title=User:Octahedron80/catcat-wiktionary-data.js&action=raw&ctype=text/javascript');

/* Escaping string for regular expression */
RegExp.escape = function(text) {
	return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
};

function getHanSortKey(han) {
	var output = null;
	$.ajax({
		'type': 'POST',
		'url': '//api.bond.in.th/han.php',
		'data': { 'c': han },
		'success': function(data) {
			if (typeof data.sortKey !== 'undefined') {
				output = data.sortKey;
			}
		},
		'dataType': 'json',
		'async': false
	});
	return output;
}

function getLanguageCodeByName(langName) {
	var output = null;
	$.ajax({
		'type': 'POST',
		'url': apiUrl,
		'data': {
			'action': 'expandtemplates',
			'prop': 'wikitext',
			'text': '{{#invoke:languages/templates|getByCanonicalName|' + langName + '}}',
			'format': 'json',
			'utf8': '1'
		},
		'success': function(data) {
			if (data.expandtemplates.wikitext !== '') {
				output = data.expandtemplates.wikitext;
			}
		},
		'dataType': 'json',
		'async': false
	});
	if (output === null) { // second try
		$.ajax({
			'type': 'POST',
			'url': apiUrl,
			'data': {
				'action': 'expandtemplates',
				'prop': 'wikitext',
				'text': '{{#invoke:etymology languages/templates|getByCanonicalName|' + langName + '}}',
				'format': 'json',
				'utf8': '1'
			},
			'success': function(data) {
				if (data.expandtemplates.wikitext !== '') {
					output = data.expandtemplates.wikitext;
				}
			},
			'dataType': 'json',
			'async': false
		});
	}
	return output;
}

function getFamilyCodeByName(famName) {
	var output = null;
	$.ajax({
		'type': 'POST',
		'url': apiUrl,
		'data': {
			'action': 'expandtemplates',
			'prop': 'wikitext',
			'text': '{{#invoke:families/templates|getByCanonicalName|' + famName + '}}',
			'format': 'json',
			'utf8': '1'
		},
		'success': function(data) {
			if (data.expandtemplates.wikitext !== '') {
				output = data.expandtemplates.wikitext;
			}
		},
		'dataType': 'json',
		'async': false
	});
	return output;
}

function getScriptCodeByName(scName) {
	var output = null;
	$.ajax({
		'type': 'POST',
		'url': apiUrl,
		'data': {
			'action': 'expandtemplates',
			'prop': 'wikitext',
			'text': '{{#invoke:scripts/templates|getByCanonicalName|' + scName + '}}',
			'format': 'json',
			'utf8': '1'
		},
		'success': function(data) {
			if (data.expandtemplates.wikitext !== '') {
				output = data.expandtemplates.wikitext;
			}
		},
		'dataType': 'json',
		'async': false
	});
	return output;
}

$(document).ready(function() {

	/* Auto-creation of Esparanto non-lemma forms */
	var $eoSection = $('h2:contains("ภาษาเอสเปรันโต")').nextUntil('h2');
	$eoSection.find('.form-of a').each(function() {
		var $wordLink = $(this);
		var uncountable = $wordLink.parent().parent().hasClass('uncountable-accusative-form-of');
		var capital = /^[A-ZĈĜĤĴŜŬ]/.test(pageTitle)
		if ($wordLink.hasClass('new')) {
			var $createButton = $('<button>✱</button>').click(function() {
				$(this).fadeOut();
				$wordLink.fadeOut();
				
				var the_form = $wordLink.text();
				var root = pageTitle.slice(0, -1); /* cut off last letter */
				var suffix = the_form.slice(root.length)
				
				var buffer = '== ภาษาเอสเปรันโต ==\n=== การออกเสียง ===\n{{eo-IPA}}\n\n';
				if (suffix == 'on' || suffix == 'oj' || suffix == 'ojn'
					|| suffix == 'anto' || suffix == 'anton' || suffix == 'antoj' || suffix == 'antojn'
					|| suffix == 'into' || suffix == 'inton' || suffix == 'intoj' || suffix == 'intojn'
					|| suffix == 'onto' || suffix == 'onton' || suffix == 'ontoj' || suffix == 'ontojn'
					|| suffix == 'ato' || suffix == 'aton' || suffix == 'atoj' || suffix == 'atojn'
					|| suffix == 'ito' || suffix == 'iton' || suffix == 'itoj' || suffix == 'itojn'
					|| suffix == 'oto' || suffix == 'oton' || suffix == 'otoj' || suffix == 'otojn') {
					if (capital) {
						buffer += '=== คำวิสามานยนาม ===\n';
					} else {
						buffer += '=== คำนาม ===\n';
					}
				} else if (suffix == 'an' || suffix == 'aj' || suffix == 'ajn'
					|| suffix == 'anta' || suffix == 'antan' || suffix == 'antaj' || suffix == 'antajn'
					|| suffix == 'inta' || suffix == 'intan' || suffix == 'intaj' || suffix == 'intajn'
					|| suffix == 'onta' || suffix == 'ontan' || suffix == 'ontaj' || suffix == 'ontajn'
					|| suffix == 'ata' || suffix == 'atan' || suffix == 'ataj' || suffix == 'atajn'
					|| suffix == 'ita' || suffix == 'itan' || suffix == 'itaj' || suffix == 'itajn'
					|| suffix == 'ota' || suffix == 'otan' || suffix == 'otaj' || suffix == 'otajn') {
					buffer += '=== คำคุณศัพท์ ===\n';
				} else if (suffix == 'is' || suffix == 'as' || suffix == 'os' || suffix == 'us' || suffix == 'u') {
					buffer += '=== คำกริยา ===\n';
				} else if (suffix == 'ante' || suffix == 'inte' || suffix == 'onte'
					|| suffix == 'ate' || suffix == 'ite' || suffix == 'ote') {
					buffer += '=== คำกริยาวิเศษณ์ ===\n';
				} else {
					buffer += '=== คำ??? ===\n';
				}
				buffer += '{{eo-head}}\n# {{eo-form of|' + root + '|' + suffix;
				if (uncountable) {
					if (capital) {
						buffer += '-proper';
					} else {
						buffer += '|unc=yes';
					}
				}
				buffer += '}}\n';
				
				$.post(apiUrl, {
					'action': 'edit',
					'title': the_form,
					'summary': 'สร้างคำอัตโนมัติ',
					'createonly': '1',
					'appendtext': buffer,
					'token': mw.user.tokens.get('editToken'),
					'format': 'xml'
				}, function() {
					mw.util.jsMessage('สร้างหน้า ' + the_form + ' เรียบร้อยแล้ว');
					$wordLink.removeClass('new').attr('href', function(i, val) {
						return val.replace('&action=edit&redlink=1', '');
					}).fadeIn();
				}, 'xml');
			});
			$wordLink.after($createButton).after('&lrm;');
		}
	});

	/* Auto-creation of Esparanto non-lemma forms */
	var $enSection = $('h2:contains("ภาษาอังกฤษ")').nextUntil('h2');
	$enSection.find('.form-of a').each(function() {
		var $wordLink = $(this);
		$wordLink.style.color = "#22CC00";
		if ($wordLink.hasClass('new')) {
			var $createButton = $('<button>✱</button>').click(function() {
				$(this).fadeOut();
				$wordLink.fadeOut();
				var the_form = $wordLink.text();			
				var buffer = '== ภาษาอังกฤษ ==\n=== คำนาม===\n{{head|en|รูปผันคำนามพหูพจน์}}\n\n';
				
				$.post(apiUrl, {
					'action': 'edit',
					'title': the_form,
					'summary': 'สร้างคำอัตโนมัติ',
					'createonly': '1',
					'appendtext': buffer,
					'token': mw.user.tokens.get('editToken'),
					'format': 'xml'
				}, function() {
					mw.util.jsMessage('สร้างหน้า ' + the_form + ' เรียบร้อยแล้ว');
					$wordLink.removeClass('new').attr('href', function(i, val) {
						return val.replace('&action=edit&redlink=1', '');
					}).fadeIn();
				}, 'xml');
			});
			$wordLink.after($createButton).after('&lrm;');
		}
	});

});