사용자:Hsl0/연구소/2.js: 두 판 사이의 차이

리버티게임, 모두가 만들어가는 자유로운 게임
imported>Hsl0
편집 요약 없음
imported>Hsl0
편집 요약 없음
1번째 줄: 1번째 줄:
/**
/**
  * [[틀:뒤로]], [[틀:새로고침]], [[틀:앞으로]]에서 사용하는 방문기록 이동 함수
  * [[틀:CGI보호]]
  * class="historygo" data-page="(이동 위치)"
  * CGI나 주소를 조작하지 못하게 합니다.
  * 작성자: [[사용자:hsl0|hsl0]]
  * 제작자: [[사용자:hsl0|hsl0]]
**/
**/
 
$(function protectCGI() {
$(function historygo() {
function getTitle(pagename) {
$('.historygo').wrap('<a />').click(function() {
pagename = pagename.split('/')[0].split(':');
    history.go(this.dataset.page);
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;
}
var params = geturlSearch();
params.title = mw.config.get('wgPageName');
var title = getTitle(params.title);
var protect = document.getElementsByClassName('protectCGI')[0];
var cheat = null;
var temp = localStorage.getItem('protectCGI-temp');
var data = sessionStorage.getItem('protectCGI');
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);
if(res && !compareObject(params, data)) {
location.href = mw.util.getUrl(data.title, data);
}
});
});
}
localStorage.removeItem('protectCGI-temp');
}
if(protect) (function() {
var link = $(protect).find('a').click(function(event) {
event.preventDefault();
if('done' in this.dataset) {
event.stopImmediatePropagation();
if(this.href) location.replace(this.href);
} else this.dataset.done = '';
});
var href = link && link.href;
if(params.action || params.oldid) return;
else cheat = false;
if(data) {
data = JSON.parse(data);
cheat = !compareObject(data, params);
} else cheat = true;
if(cheat) {
if(data) {
title = data.title;
delete data.title;
location.replace(mw.util.getUrl(title, data));
} else if(link) link.click();
else history.back();
}
})();
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(document.getElementsByClassName('protectCGI-link-all')[0]) {
$('#mw-content-text a').not('.protectCGI-link-no a').filter(filterLink).addClass('protectCGI-link');
} else $('.protectCGI-link-container a').not('.protectCGI-link-no a').filter(filterLink).addClass('protectCGI-link');
if(cheat === false) $('.protectCGI-link').click(function(event) {
sessionStorage.setItem('protectCGI', JSON.stringify($(this).data('params')));
if(protect) {
event.preventDefault();
location.replace(this.href);
}
});
});
$('body').addClass('protectCGI-link-done');
window.addEventListener('hashchange', function(event) {
var data, dataObj;
if(location.hash === '##emergency-save') {
data = sessionStorage.getItem('protectCGI');
if(data) dataObj = JSON.parse(data);
if(data && dataObj.title === params.title) localStorage.setItem('protectCGI-temp', data);
}
})
});
});

2020년 12월 31일 (목) 15:59 판

/**
 * [[틀: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;
	}
	
	var params = geturlSearch();
	params.title = mw.config.get('wgPageName');
	var title = getTitle(params.title);
	var protect = document.getElementsByClassName('protectCGI')[0];
	var cheat = null;
	var temp = localStorage.getItem('protectCGI-temp');
	var data = sessionStorage.getItem('protectCGI');
	
	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);
					if(res && !compareObject(params, data)) {
						location.href = mw.util.getUrl(data.title, data);
					}
				});
			});
		}
		localStorage.removeItem('protectCGI-temp');
	}
	
	if(protect) (function() {
		var link = $(protect).find('a').click(function(event) {
			event.preventDefault();
			if('done' in this.dataset) {
				event.stopImmediatePropagation();
				if(this.href) location.replace(this.href);
			} else this.dataset.done = '';
		});
		var href = link && link.href;
		
		if(params.action || params.oldid) return;
		else cheat = false;
		
		if(data) {
			data = JSON.parse(data);
			cheat = !compareObject(data, params);
		} else cheat = true;
		
		if(cheat) {
			if(data) {
				title = data.title;
				delete data.title;
				location.replace(mw.util.getUrl(title, data));
			} else if(link) link.click();
			else history.back();
		}
	})();
	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(document.getElementsByClassName('protectCGI-link-all')[0]) {
		$('#mw-content-text a').not('.protectCGI-link-no a').filter(filterLink).addClass('protectCGI-link');
	} else $('.protectCGI-link-container a').not('.protectCGI-link-no a').filter(filterLink).addClass('protectCGI-link');
	
	if(cheat === false) $('.protectCGI-link').click(function(event) {
		sessionStorage.setItem('protectCGI', JSON.stringify($(this).data('params')));
		if(protect) {
			event.preventDefault();
			location.replace(this.href);
		}
	});
	
	$('body').addClass('protectCGI-link-done');
	
	window.addEventListener('hashchange', function(event) {
		var data, dataObj;
		if(location.hash === '##emergency-save') {
			data = sessionStorage.getItem('protectCGI');
			if(data) dataObj = JSON.parse(data);
			if(data && dataObj.title === params.title) localStorage.setItem('protectCGI-temp', data);
		}
	})
});