User:魔琴/gadgets/Re/Re.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//<nowiki>
mw.loader.using( ['mediawiki.util', 'mediawiki.api', 'mediawiki.Title', 'mediawiki.messagePoster','ext.gadget.site-lib'/*,'ext.gadget.HanAssist'*/ ], function () {
mw.loader.load('/w/index.php?title=User:魔琴/gadgets/Re/Re.css&action=raw&ctype=text/css', 'text/css');
var Re = {};
var dl = null;
var html = '';

if ($('.mw-editsection').length > 0) {
	var reBtn = $('<span>').append($('<span>').addClass('mw-editsection-divider').text(' | ')).append($('<a>').addClass('Re-Btn').attr('href', 'javascript:void(0)').text('Re'));
	$( ".mw-heading2 .mw-editsection" ).each(function(){
		var _reBtn = reBtn.clone();
		$(this).find('.mw-editsection-bracket').last().before(_reBtn);
	});
}

$('.Re-Btn').click(function(){
	Re= {};
	Re.linkpage = mw.config.get('wgPageName');
	Re.user = JSON.parse($(this).closest(".mw-heading2").html().match(/<!--__DTSUBSCRIBEBUTTONDESKTOP__.*?-->/)[0].replace(/<!--__DTSUBSCRIBEBUTTONDESKTOP__(.*)-->/g,'$1').replace(/&quot;/g,'"')).name.replace(/h-(.*?)-\d{14}/,"$1");
	Re.id = $(this).closest(".mw-heading2").find('h2').attr("id");
	Re.h2 = `Re: [[${Re.linkpage}#${Re.id}|${Re.id}]]`;
	loadDialog();
});

function han (s,t) {
	return wgULS( s, t );
	//return HanAssist.localize( { hans: s, hant: t } );
}

function loadDialog(){
	Re.placeholder = han('无需签名。','無需簽名。');
	html =
		'<div id="Re-dialog">' +
		`<div class="mw-body-content"><h2>Re: <a href="/wiki/${Re.linkpage}#${Re.id}" title="${Re.linkpage}#${Re.id}">${Re.id}</a></h2></div>` +
		'<br />' +
		`<textarea name="Re-text" id="Re-text" rows=6 placeholder=${Re.placeholder}></textarea>` +
		'</div>';
	if (dl) {
		dl.html(html).closest(".ui-dialog").remove();
	}
	dl = $(html).dialog({
		title: `Re @ <a href="/wiki/User_talk:${Re.user}" title="${Re.user}">User talk:${Re.user}</a>`,
		minWidth: 450,
		minHeight: 150,
		buttons: [
			{
				text: han('预览','預覽'),
				click: function() {
					processConfig();
					preview();
				}
			},
			{
				text: han('发送','發送'),
				click: function() {
					processConfig();
					action();
					$(this).dialog('close');
				}
			},
			{
				text: '取消',
				click: function() { 
					$(this).dialog('close');
				}
			}
		]
	});
}

function processConfig(){
	Re.text = $('#Re-dialog').find('#Re-text').val() + '&nbsp;~~~~';
}

function preview() {
	var previewPageName = 'User_talk:' + Re.user;
	var API = new mw.Api();
	if (mw.config.get('wgPageContentModel') !== 'wikitext') {
		previewPageName = 'Draft:' + previewPageName;
	}
	var previewPromise = API.post({
		action: 'parse',
		contentmodel: 'wikitext',
		text: '== '+  Re.h2 + ' ==\r' + Re.text,
		pst: 'true',    // PST = pre-save transform; this makes substitution work properly
		title: previewPageName,
		prop: 'text',
		formatversion: '2'
	} ).done(function(data){
		var dlp = null;
		var htmlp =
			'<div id="Re-preview" class="mw-body-content">' + data.parse.text + '</div>';
		if (dlp) {
			dlp.html(htmlp).dialog("open");
			return null;
		}
		dlp = $(htmlp).dialog({
			title: han('预览','預覽') + `Re @ <a href="/wiki/User_talk:${Re.user}" title="${Re.user}">User talk:${Re.user}</a>`,
			minWidth: 600,
			minHeight: 100,
			buttons: [
				{
					text: han('确定','確定'),
					click: function() {
						action();
						$(this).dialog('close');
						$("#Re-dialog").closest(".ui-dialog").remove();
					}
				},
				{
					text: '取消',
					click: function() { $(this).dialog('close'); }
				}
			]
		});
	}).fail(function(error) {
		mw.notify( han('发送失败','發送失敗') + error );
	});
}

function action() {
    var title = new mw.Title( 'User_talk:' + Re.user );
    var posterPromise = mw.messagePoster.factory.create( title );
    posterPromise.done( function ( poster ) {
        poster.post( Re.h2, Re.text );
        setTimeout( function () {
			window.location.href = mw.util.getUrl( 'User_talk:' + Re.user );
		}, 4000 );
		mw.notify( han('发送完毕,正在重定向……','發送完畢,正在重新導向……'));
    } );
}
});
//</nowiki>