Ця сторінка повністю захищена, оскільки містить текст інтерфейсу цієї вікі.

MediaWiki:Gadget-autosign.js

Матеріал з Minecraft Wiki
Перейти до навігації Перейти до пошуку
Іншими мовами

Note: After saving, you have to bypass your browser's cache to see the changes.

Google Chrome, Firefox, Microsoft Edge, and Safari: Hold down the Shift key and click the Reload toolbar button.
For details and instructions about other browsers, see Wikipedia:Bypass your cache.

// pads a time to 2 digits
function padtime(time) {
	return (time + '').padStart(2, '0');
}

$(function() {
'use strict';

// only run when editing
if (mw.user.options.get('usebetatoolbar') &&
	/edit|submit/.test(mw.config.get('wgAction'))) {
		
	// wait on the API library
	mw.loader.using('mediawiki.api').then(function() {
		// fetch the username and timestamp of the last revision
		new mw.Api().get({
			action: 'query',
			titles: mw.config.get('wgPageName'),
			prop: 'revisions',
			rvprop: 'user|timestamp',
			formatversion: 2
		}).done(function(data) {
			var page = data.query.pages[0];
			// if the revision is missing, skip
			if (page.missing) {
				return;
			}
			
			// fetch data from the revision
			var revision = page.revisions[0];
			var user = revision.user;
			var date = new Date(revision.timestamp);
			
			// on the odd chance it fails to parse the date, skip date
			var insert;
			if (isNaN(date.getTime())) {
				insert = '{{subst:'+'unsigned|'+user+'}}';
			} else {
				// wish there was an easier way to do this, I miss moment.js
				var timestamp = padtime(date.getUTCHours())+':'+padtime(date.getUTCMinutes())+', '+date.getUTCDate()+' '+date.toLocaleDateString('uk-ua', {month:'long', timeZone:'UTC'})+' '+date.getUTCFullYear();
				insert = '{{subst:'+'unsigned|'+user+'|'+timestamp+'}}';
			}
			
			// add the editor button
			mw.loader.using('ext.wikiEditor').then(function() {
				$('#wpTextbox1').wikiEditor('addToToolbar', {
					section: 'advanced',
					group: 'insert',
					tools: {
						autosign: {
							label: 'Автопідпис попереднього коментаря',
							type: 'button',
							icon: 'https://upload.wikimedia.org/wikipedia/commons/b/b3/Insert-signature.svg',
							action: {type: 'replace', options: {pre: insert}}
						}
					}
				});
			});
		});
	});
}
});