Участник:Absconditus/reporter.js

Материал из Википедии — свободной энциклопедии
/* 
Скрипт для запросов на ЗКА или УЗ, делаемых с любой страницы, как в Твинкле. 
Автор — ru.wikipedia.org/wiki/user:Absconditus
v.1.0
*/

var newPageName = mw.config.get('wgPageName')
	.replace( /_/g, ' ' )
	.replace(/(Special|Служебная)\:(Contributions|Вклад)\//i, 'User:')
	.replace(/(User talk\:|Обсуждение участни(ка|цы)\:)/i, 'User:');
var newPGregexp = '[[' + newPageName +
	(mw.config.get('wgPageName').match(/(user|contrib|участни|вклад)/i)
		? '|' + mw.config.get('wgPageName')
			.replace( /_/g, ' ' )
			.replace(/(Special|Служебная)\:(Вклад|Contributions)\//i, '')
			.replace(/(User talk\:|Обсуждение участни(ка|цы)\:)/i, '')
			.replace(/(user|участни(к|ца))\:/i, '')+']]'
		: ']]'
	);
var newPGregexp2 = mw.config.get('wgPageName')
	.replace( /_/g, ' ' )
	.replace(/(Special|Служебная)\:(Вклад|Contributions)\//i, '')
	.replace(/(User talk\:|Обсуждение участни(ка|цы)\:)/i, '')
	.replace(/(user|участни(к|ца))\:/i, '');
var reportPage = mw.config.get('wgPageName').match(/(user|contrib|участни[^\/]*$|вклад)/i)
	? 'Википедия:Запросы к администраторам'
	: 'Википедия:Установка защиты';

$(function () {
	if (mw.config.get('wgNamespaceNumber') !== -1 ||
		(mw.config.get('wgNamespaceNumber') === -1 &&
			mw.config.get('wgCanonicalSpecialPageName') === 'Contributions'
		)
	) {
		mw.loader.using('mediawiki.util').done( function () {
			mw.util.addPortletLink(
				'p-cactions',
				'javascript:showWindow2();',
				'Запрос',
				'ca-reporter',
				'Добавить запрос на ЗКА или УЗ'
			);
		});
	}
});

function showWindow2() {
	var windown = '<div xmlns="http://www.w3.org/1999/xhtml" id="reporter-window" style="background:white; border:1px double black; padding:15px; position:fixed; top:200px; left:200px; z-index:2; height:250px; width:700px; text-align:left;"><table>' +
		'<tr><td>Отправить на страницу: ' + reportPage + '</td></tr>' +
        '<tr><td>Тема/заголовок: <input type="text" id="reporterHeader" style="width:80%;" value="' + newPGregexp + '" /></td></tr>' +
		'<tr><td><textarea id="reporterTextarea" rows="8" cols="200" width="90%" align="left" placeholder="Введите сюда текст запроса. Подпись будет добавлена автоматически после введённого текста."></textarea></td></tr>' +
		'<tr><td width="80%"><input type="button" id="reporter-confirm" value="Отправить" /><input type="button" id="reporter-cancel" value="Не отправлять" /><input type="button" id="reporter-fast" value="Отправить быстрый запрос" /><p align="right" width="20%" style="float:right; font-size:95%;"><a href="//ru.wikipedia.org/wiki/user_talk:Absconditus/reporter.js" target="_blank">О скрипте</a></p></td></tr>' +
		'</table></div>';
	$('#bodyContent').append($.parseXML(windown).firstChild);
	$('#reporter-confirm')[0].onclick = function () { makeReport(); };
	$('#reporter-fast')[0].onclick = function () { makeReportFast(); };
	$('#reporter-cancel')[0].onclick = function () {
		$('div#reporter-window').remove();
	};
}

function extractDisplayedText(s) {
	return s.replace(/\[\[:?(?:[^|\]]+\|)?(.+?)\]\]/g, '$1');
}

function makeReport() {
    $.getJSON(mw.config.get('wgScriptPath') + '/api.php?action=query&meta=tokens&type=csrf&format=json', function (resp) {
    	if (!resp || !resp.query || !resp.query.tokens) {
    		return;
    	}
		var reporterHeaderVal = $('#reporterHeader').val();
		if (reportPage === 'Википедия:Запросы к администраторам') {
		    $.post(mw.config.get('wgScriptPath') + '/api.php', {
		    	action: 'edit',
		    	title: reportPage,
		    	token: resp.query.tokens.csrftoken,
		    	summary: '\/*' + extractDisplayedText(reporterHeaderVal) +' *\/ новая тема ([[user talk:Absconditus/reporter.js|запросчик.js]])',
		    	section: '0',
		    	appendtext: '\n== ' + reporterHeaderVal + ' ==\n* \{\{userlinks|' + newPGregexp2 +
		    		((/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/).test(newPGregexp2) ? '|ip=1' : '') +
		    		'}}\n' + $('#reporterTextarea').val() + ' ~~' + '~~'
		    }, function() {
		    	window.location = mw.config.get('wgServer') + '/wiki/' + reportPage;
		    });
		}
		if (reportPage === 'Википедия:Установка защиты') {
			$.post( mw.config.get('wgScriptPath') + '/api.php', {
				action: 'edit',
				title: reportPage,
				token: resp.query.tokens.csrftoken,
				summary: '\/* ' + extractDisplayedText(reporterHeaderVal) + ' *\/ новая тема ([[user talk:Absconditus/reporter.js|запросчик.js]])',
				appendtext: '\n== ' + reporterHeaderVal + ' ==\n* \{\{pagelinks|' + newPageName + '}}\n' + $('#reporterTextarea').val() + ' ~~' + '~~'
			}, function () {
				window.location = mw.config.get('wgServer') + '/wiki/' + reportPage;
			});
		}
	});
}

function makeReportFast() {
	var reportPageFast;
	$.getJSON(mw.config.get('wgScriptPath') + '/api.php?action=query&meta=tokens&type=csrf&format=json', function (resp) {
    	if (!resp || !resp.query || !resp.query.tokens) {
    		return;
    	}
		reportPageFast = 'Википедия:Запросы к администраторам/Быстрые';
		if (reportPage === 'Википедия:Запросы к администраторам')  {
    		$.post( mw.config.get('wgScriptPath') + '/api.php', {
    			action: 'edit',
    			title: reportPageFast,
    			token: resp.query.tokens.csrftoken,
    			summary: '[[Special:Contributions/' + newPGregexp2 + ']] — новый запрос ([[user talk:Absconditus/reporter.js|запросчик.js]])',
    			appendtext: '\n\n\{\{sub' + 'st:t:preload/ЗКАБ/subst|\n | участник = ' + newPGregexp2 +
    				'| страница = | пояснение = ' + $("#reporterTextarea").val() + '}}\n'
    		}, function () {
    			window.location = mw.config.get('wgServer') + '/wiki/' + reportPageFast;
    		});
		}
		if (reportPage === 'Википедия:Установка защиты') {
			$.post(mw.config.get('wgScriptPath') + '/api.php', {
				action: 'edit',
				title: reportPageFast,
				token: resp.query.tokens.csrftoken,
				summary: '[[' + newPGregexp2 + ']] — новый запрос ([[user talk:Absconditus/reporter.js|запросчик.js]])',
				appendtext: '\n\n\{\{sub' + 'st:t:preload/ЗКАБ/subst|\n | участник = '  +'| страница = '+ newPageName +
					'| пояснение = ' + $("#reporterTextarea").val() + '}}\n'
			}, function () {
				window.location = mw.config.get('wgServer') + '/wiki/' + reportPageFast;
			});
		}
	});
}