사용자:기나ㅏㄴ/Restorer.js

위키백과, 우리 모두의 백과사전.

참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다. 구글 크롬, 파이어폭스, 마이크로소프트 엣지, 사파리: ⇧ Shift 키를 누른 채 "새로 고침" 버튼을 클릭하십시오. 더 자세한 정보를 보려면 위키백과:캐시 무시하기 항목을 참고하십시오.

/*** Restorer ***/

// Easily restore an older version of a page
// 이 스크립트는 한국어 위키백과 전용이며, 영어 위키백과의 원본은 [[:en:w:User:BrandonXLF/Restorer.js]]에 있습니다.

$(function() {
	if (mw.config.get('wgAction') != 'history') return;

	window.restorerSummary = window.restorerSummary ||
		'[[Special:Contributions/$USER|$USER]]의 판 $ID으로 되돌림 ([[w:ko:User:기나ㅏㄴ/Restorer|Restorer]])';

	function restore(revid) {
		var api = new mw.Api();

		return api.get({
			action: 'query',
			revids: revid,
			prop: 'revisions',
			rvprop: 'user',
			format: 'json',
			formatversion: '2'
		}).then(function(res) {
			var user = res.query.pages[0].revisions[0].user;

			return api.postWithEditToken({
				action: 'edit',
				pageid: mw.config.get('wgArticleId'),
				undo: mw.config.get('wgCurRevisionId'),
				undoafter: revid,
				summary: window.restorerSummary.replace(/\$ID/g, revid).replace(/\$USER/g, user)
			});
		}).then(
			function() {
				mw.notify('정상적으로 복구되었습니다.');
				location.reload();
			},
			function(_, data) {
				mw.notify(api.getErrorMessage(data), {type: 'error'});
			}
		);
	}

	function addLink(item) {
		var revid = item.getAttribute('data-mw-revid');
		if (revid == mw.config.get('wgCurRevisionId')) return;

		var links = item.querySelector('.comment + .mw-changeslist-links');
		if (!links) return;

		var parent = document.createElement('span'),
			el = document.createElement('a');

		el.addEventListener('click', function() {
			el.className = 'restorer-loading';

			restore(revid).always(function() {
				el.className = '';
			});
		});

		el.textContent = '복구하기';
		parent.appendChild(el);
		links.appendChild(parent);
	}

	var parents = document.querySelectorAll('li[data-mw-revid]');

	for (var i = 0; i < parents.length; i++) {
		addLink(parents[i]);
	}

	mw.loader.addStyleTag(
		'@keyframes restorer-loading {' +
		'0%, 100% {content: " ⡁"} 16% {content: " ⡈"} 33% {content: " ⠔"} 50% {content: " ⠒"} 66% {content: " ⠢"} 83% {content: " ⢁"}}' +
		'.restorer-loading::after {white-space: pre; content: ""; animation: restorer-loading 0.5s infinite}'
	);
});