MediaWiki:Gadget-autosign.js

来自Minecraft Wiki
跳转到导航 跳转到搜索
其他语言中

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → 选项),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
$(function () {
	'use strict';
	var conv = require('ext.gadget.HanAssist').conv;

	// only run when user prefers to show toolbar
	if (mw.user.options.get('showtoolbar') &&
		mw.user.options.get('usebetatoolbar')) {

		mw.hook('wikiEditor.toolbarReady').add(function ($textarea) {
			// 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
			}).then(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
					// made some localization here
					var timestamp = date.toLocaleDateString('zh', {
						year: 'numeric',
						month: 'long',
						day: 'numeric',
						timeZone: 'UTC'
					}) + ' (' + new Intl.DateTimeFormat('zh', {
						weekday: 'narrow',
						timeZone: 'UTC'
					}).format(date) + ') ' + date.toLocaleTimeString('zh', {
						hour: '2-digit',
						minute: '2-digit',
						hour12: false,
						timeZone: 'UTC'
					});
					insert = '{{subst:' + 'Unsigned|' + user + '|' + timestamp + '}}';
				}

				// add the editor button
				$textarea.wikiEditor('addToToolbar', {
					section: 'advanced',
					group: 'insert',
					tools: {
						autosign: {
							label: conv({ hans: '自动签名上一次留言', hant: '自動簽名上一次留言' }),
							type: 'button',
							icon: 'https://upload.wikimedia.org/wikipedia/commons/b/b3/Insert-signature.svg',
							action: { type: 'replace', options: { pre: insert } }
						}
					}
				});
			});
		});
	}
});