미디어위키:Gadget-ProtectCGI.js: 두 판 사이의 차이

리버티게임(개발), 모두가 만들어가는 자유로운 게임
둘러보기로 이동 검색으로 이동
imported>Senouis
(1월 2일 hsl0님 요청)
imported>Senouis
(1월 13일 hsl0님 요청)
 
(같은 사용자의 중간 판 4개는 보이지 않습니다)
4번째 줄: 4번째 줄:
  * 제작자: [[사용자:hsl0|hsl0]]
  * 제작자: [[사용자:hsl0|hsl0]]
**/
**/
$(function protectCGI() {
(function protectCGI() {
function getTitle(pagename) {
function getTitle(pagename) {
pagename = pagename.split('/')[0].split(':');
pagename = pagename.split('/')[0].split(':');
20번째 줄: 20번째 줄:
if(a[key] !== b[key]) return false;
if(a[key] !== b[key]) return false;
}
}
return true;
}
function filterLink() {
var href = new URL(this.href, location);
var params = geturlSearch(href);
if(href.host !== location.host) return false;
if(params.action || params.oldid) return false;
if(href.pathname.startsWith('/wiki/')) params.title = href.pathname.slice(6);
if(getTitle(params.title) !== title) return false;
$(this).data('params', params);
return true;
return true;
31번째 줄: 45번째 줄:
var temp = localStorage.getItem('protectCGI-temp');
var temp = localStorage.getItem('protectCGI-temp');
var data = sessionStorage.getItem('protectCGI');
var data = sessionStorage.getItem('protectCGI');
var link;
if(temp) {
registerRenderer(function() {
if(!data) {
$('.protectCGI-link-container a').filter(filterLink).addClass('protectCGI-link');
sessionStorage.setItem('protectCGI', temp);
$('.protectCGI-link-container a:not(.protectCGI-link)').addClass('protectCGI-link-no');
data = temp;
});
mw.loader.using('oojs-ui-windows').then(function() {
 
OO.ui.confirm('비상 저장된 세션이 있습니다. 다시 돌아가겠습니까?').then(function(res) {
registerHandler(function() {
var data = JSON.parse(temp);
function hijack(link) {
if(res && !compareObject(params, data)) {
var title;
location.href = mw.util.getUrl(data.title, data);
}
if(link.href && link.href != location.href) location.replace(link.href);
});
else if(data) {
data = JSON.parse(data);
title = data.title;
delete data.title;
location.replace(mw.util.getUrl(title, data));
} else if(document.referrer) history.back();
else location.replace(mw.util.getUrl(getTitle(mw.config.get('wgPageName'))));
}
if(protect) (function() {
var clicked;
link = $(protect).find('a').click(function(event) {
event.preventDefault();
if(this === clicked) {
if('wait' in this.dataset) {
if(this.dataset.wait <= 0) {
event.stopImmediatePropagation();
hijack(this);
}
} else hijack(this);
} else {
clicked = this;
this.click();
}
});
});
}
 
localStorage.removeItem('protectCGI-temp');
if(params.action || params.oldid) return;
}
else cheat = false;
if(protect) (function() {
if(data) {
var clicked;
data = JSON.parse(data);
var link = $(protect).find('a').click(function(event) {
cheat = !compareObject(data, params);
event.preventDefault();
} else cheat = true;
if(this === clicked) {
})();
if('wait' in this.dataset) {
else if(!(params.action || params.oldid)) cheat = false;
if(this.dataset.wait <= 0) {
event.stopImmediatePropagation();
if(cheat === false) $('.protectCGI-link').click(function(event) {
if(this.href) location.replace(this.href);
sessionStorage.setItem('protectCGI', JSON.stringify($(this).data('params')));
}
if(protect) {
} else if(this.href) location.replace(this.href);
event.preventDefault();
} else {
location.replace(this.href);
clicked = this;
this.click();
}
}
});
});
var href = link && link.href;
if(params.action || params.oldid) return;
window.addEventListener('hashchange', function(event) {
else cheat = false;
var data, dataObj;
if(location.hash.endsWith('##emergency-save')) {
data = sessionStorage.getItem('protectCGI');
if(data) dataObj = JSON.parse(data);
if(data && dataObj.title === params.title) localStorage.setItem('protectCGI-temp', data);
}
});
});
registerTrigger(function() {
if(temp) {
if(!data) {
sessionStorage.setItem('protectCGI', temp);
data = temp;
mw.loader.using('oojs-ui-windows').then(function() {
OO.ui.confirm('비상 저장된 세션이 있습니다. 다시 돌아가겠습니까?').then(function(res) {
var data = JSON.parse(temp);
var title;
if(res && !compareObject(params, data)) {
title = data.title;
delete data.title;
location.href = mw.util.getUrl(data.title, data);
}
});
});
}
localStorage.removeItem('protectCGI-temp');
}
if(data) {
if(cheat) {
data = JSON.parse(data);
if(link.length) link.click();
cheat = !compareObject(data, params);
} else cheat = true;
if(cheat) setTimeout(function() {
if(link) link.click();
else if(data) {
else if(data) {
title = data.title;
title = data.title;
81번째 줄: 138번째 줄:
location.replace(mw.util.getUrl(title, data));
location.replace(mw.util.getUrl(title, data));
}
}
else history.back();
else if(document.referrer) history.back();
}, 0);
else location.replace(mw.util.getUrl(getTitle(mw.config.get('wgPageName'))));
})();
}
else if(!(params.action || params.oldid)) cheat = false;
function filterLink() {
var href = new URL(this.href, location);
var params = geturlSearch(href);
if(href.host !== location.host) return false;
if(params.action || params.oldid) return false;
if(href.pathname.startsWith('/wiki/')) params.title = href.pathname.slice(6);
if(getTitle(params.title) !== title) return false;
$(this).data('params', params);
return true;
if(cheat === false) {
}
document.body.classList.add('protectCGI-done');
$('.protectCGI-only').show();
$('.protectCGI-link-container a').filter(filterLink).addClass('protectCGI-link');
$('.protectCGI-link-container a:not(.protectCGI-link)').addClass('protectCGI-link-no');
if(cheat === false) $('.protectCGI-link').click(function(event) {
sessionStorage.setItem('protectCGI', JSON.stringify($(this).data('params')));
if(protect) {
event.preventDefault();
location.replace(this.href);
}
}
});
});
})();
document.body.classList.add('protectCGI-done');
window.addEventListener('hashchange', function(event) {
var data, dataObj;
if(location.hash.endsWith('##emergency-save')) {
data = sessionStorage.getItem('protectCGI');
if(data) dataObj = JSON.parse(data);
if(data && dataObj.title === params.title) localStorage.setItem('protectCGI-temp', data);
}
});
});

2021년 1월 13일 (수) 17:04 기준 최신판

/**
 * [[틀:CGI보호]]
 * CGI나 주소를 조작하지 못하게 합니다.
 * 제작자: [[사용자:hsl0|hsl0]]
**/
(function protectCGI() {
	function getTitle(pagename) {
		pagename = pagename.split('/')[0].split(':');
		var ns = pagename[0].replace(/talk|토론/gi, '');
		return ns + (ns && ':') + pagename[1];
	}
	
	function compareObject(a, b) {
		var aKey = Object.keys(a);
		var bKey = Object.keys(b);
		
		if(aKey.length !== bKey.length) return false;
		
		for(var key in a) {
			if(a[key] !== b[key]) return false;
		}
		
		return true;
	}
	
	function filterLink() {
		var href = new URL(this.href, location);
		var params = geturlSearch(href);
		
		if(href.host !== location.host) return false;
		if(params.action || params.oldid) return false;
		if(href.pathname.startsWith('/wiki/')) params.title = href.pathname.slice(6);
		if(getTitle(params.title) !== title) return false;
		
		$(this).data('params', params);
		
		return true;
	}
	
	var params = geturlSearch();
	params.title = mw.config.get('wgPageName');
	var title = getTitle(params.title);
	var protect = document.getElementById('protectCGI');
	var cheat = null;
	var temp = localStorage.getItem('protectCGI-temp');
	var data = sessionStorage.getItem('protectCGI');
	var link;
	
	registerRenderer(function() {
		$('.protectCGI-link-container a').filter(filterLink).addClass('protectCGI-link');
		$('.protectCGI-link-container a:not(.protectCGI-link)').addClass('protectCGI-link-no');
	});

	registerHandler(function() {
		function hijack(link) {
			var title;
			
			if(link.href && link.href != location.href) location.replace(link.href);
			else if(data) {
				data = JSON.parse(data);
				title = data.title;
				delete data.title;
				location.replace(mw.util.getUrl(title, data));
			} else if(document.referrer) history.back();
			else location.replace(mw.util.getUrl(getTitle(mw.config.get('wgPageName'))));
		}
		
		if(protect) (function() {
			var clicked;
			link = $(protect).find('a').click(function(event) {
				event.preventDefault();
				if(this === clicked) {
					if('wait' in this.dataset) {
						if(this.dataset.wait <= 0) {
							event.stopImmediatePropagation();
							hijack(this);
						}
					} else hijack(this);
				} else {
					clicked = this;
					this.click();
				}
			});

			if(params.action || params.oldid) return;
			else cheat = false;
			
			if(data) {
				data = JSON.parse(data);
				cheat = !compareObject(data, params);
			} else cheat = true;
		})();
		else if(!(params.action || params.oldid)) cheat = false;
		
		if(cheat === false) $('.protectCGI-link').click(function(event) {
			sessionStorage.setItem('protectCGI', JSON.stringify($(this).data('params')));
			if(protect) {
				event.preventDefault();
				location.replace(this.href);
			}
		});
		
		window.addEventListener('hashchange', function(event) {
			var data, dataObj;
			if(location.hash.endsWith('##emergency-save')) {
				data = sessionStorage.getItem('protectCGI');
				if(data) dataObj = JSON.parse(data);
				if(data && dataObj.title === params.title) localStorage.setItem('protectCGI-temp', data);
			}
		});
	});
	
	registerTrigger(function() {
		if(temp) {
			if(!data) {
				sessionStorage.setItem('protectCGI', temp);
				data = temp;
				mw.loader.using('oojs-ui-windows').then(function() {
					OO.ui.confirm('비상 저장된 세션이 있습니다. 다시 돌아가겠습니까?').then(function(res) {
						var data = JSON.parse(temp);
						var title;
						if(res && !compareObject(params, data)) {
							title = data.title;
							delete data.title;
							location.href = mw.util.getUrl(data.title, data);
						}
					});
				});
			}
			localStorage.removeItem('protectCGI-temp');
		}
		
		if(cheat) {
			if(link.length) link.click();
			else if(data) {
				title = data.title;
				delete data.title;
				location.replace(mw.util.getUrl(title, data));
			}
			else if(document.referrer) history.back();
			else location.replace(mw.util.getUrl(getTitle(mw.config.get('wgPageName'))));
		}
		
		if(cheat === false) {
			document.body.classList.add('protectCGI-done');
			$('.protectCGI-only').show();
		}
	});	
})();