본문으로 이동

사용자:Ykhwong/TalkBox.js

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

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

/*
* TalkBox
* @author ykhwong
* TO-DO: clean up
*/

const affectedPages = [
	'위키백과:사랑방', '위키백과:사랑방_\\(기술\\)', '위키백과:사랑방_\\(전체\\)',
	'위키백과:질문방', '위키백과:방명록', '위키백과:알찬_글_후보', '위키백과:좋은_글_후보', '위키백과:알찬_목록_후보',
	'위키백과:함께_검토하기', '위키백과:위키프로젝트\/제안',
	'위키백과:삭제_토론', '위키백과:복구_토론', '위키백과:관리자_알림판', '위키백과:봇_편집_요청',
	'위키백과:문서_관리_요청', '위키백과:이동_요청', '위키백과:사용자_관리_요청', '위키백과:사용자_권한_신청',
	'위키백과:다중_계정_검사_요청', '위키백과:봇\/등록_신청', '위키백과:파일_업로드_요청'
];
var isDiscussionToolsEnabled = $(".ext-discussiontools-init-section").length > 0;
var dateRegex;
if (isDiscussionToolsEnabled) {
	dateRegex = />답변</;
} else {
	dateRegex = /(\d+년 +\d+월 \d+일 \([일월화수목금토]\) +\d+:\d+ \(KST\)|저장할 때 자동으로 서명이 됩니다)/i;
}
$(function () {
	function addMarginIcon() {		
		var origHtml = ($(".mw-parser-output:first").html() || '').replace(/<h2>/mg, "\n<h2>").split(/\r\n|\n/);
		var outHtml = "";
		var headerReady = false;
		var cssFlag = false;
		var init = false;
		for (var i=0; i < origHtml.length; i++) {
			var curLine = origHtml[i];
			if (/^\s*(<h2><span |<h2 class=")/i.test(curLine)) {
				init = true;
				headerReady = true;
				if (cssFlag) {
					cssFlag = false;
					outHtml += "</div>\n";
				}
				outHtml += curLine + "\n";
				continue;
			}
			if (!init) {
				outHtml += curLine + "\n";
				continue;
			}
			if (headerReady) {
				outHtml += '<div class="css1">';
				outHtml += curLine;
				cssFlag = true;
				if (dateRegex.test(curLine)) {
					outHtml += "</div>";
					cssFlag = false;
				}
				outHtml += "\n";
				headerReady = false;
				continue;
			} else {
				if (!cssFlag) {
					outHtml += '<div class="css1">';
					cssFlag = true;
				}
				if (dateRegex.test(curLine)) {
					outHtml += curLine;
					outHtml += "</div>\n";
					cssFlag = false;
					continue;
				}
				outHtml += curLine + "\n";
			}
		}
		
		$(".mw-parser-output:first").html( outHtml );

		$(".mw-parser-output:first .css1").each(function( index ) {
			if(!/\S/.test($(this).text())) {
				$(this).removeClass("css1");
			}
		});

		$(".mw-parser-output:first .css1 dd").each(function( index ) {
			if ($(this).is("dd")) {
				if ($(this).text() === $($(".mw-parser-output:first .css1 dd")[index+1]).text()) {
					return true;
				}
				var myCss = $(this).css("margin-left");
				var ddCnt = $(this).parents("dd").length;
				if (ddCnt === 0) {
					ddCnt = 1;
				} else {
					ddCnt *= 2;
				}
				$(this).closest(".css1").css("margin-left", (myCss.replace("px","") * ddCnt) + "px");
			}
		});

		$(".mw-parser-output:first .css1 dd").each(function( index ) {
			if ($(this).is("dd")) {
				$(this).css("margin-left", "0");
			}
		});

		$(".css1").css({
			"height": "100%",
			"border-style": "solid",
			"border-width": "0px 0px 2px 2px",
			"padding": "12px",
			"word-break": "break-all",
			"border-color": "#dedede",
			"background-color":"#fafafa"
		});
	}

	function run() {
		addMarginIcon();
	}

	function main() {
		var currentPageName = mw.config.get( 'wgPageName' );
		var currentNamespace = mw.config.get( 'wgNamespaceNumber' );

		if (/^(1|3|5|7|9|11|13|15|101|103|119|829|2301|2303)$/.test(currentNamespace)) {
			run();
			return;
		}

		for (var i=0; i < affectedPages.length; i++) {
			var page = affectedPages[i];
			var re = new RegExp('^' + page + '$|#');
			if (currentPageName.startsWith(page.replace(/\\/g, "") + "/") ||
			re.exec(currentPageName)) {
				run();
				return;
			}
		}
	}
	main();
}());