미디어위키:Gadget-dictionary.js

삼쩌모 (토론 | 기여)님의 2025년 6월 29일 (일) 02:59 판

참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다.

  • 파이어폭스 / 사파리: Shift 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5 또는 Ctrl-R을 입력 (Mac에서는 ⌘-R)
  • 구글 크롬: Ctrl-Shift-R키를 입력 (Mac에서는 ⌘-Shift-R)
  • 엣지: Ctrl 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5를 입력.
/* 모든 페이지에서 동작하게끔 Gadgets 정의 뒤, 아래 코드 저장 */
mw.loader.using(['mediawiki.util', 'oojs-ui-core'], function () {
	$(function () {
		/* ① 숨은 JSON 가져오기 */
		var holder = document.getElementById('dictionary-json');
		if (!holder) return;                         // 사전 없는 페이지는 건너뜀
		var dict   = JSON.parse(holder.textContent); // ← 그대로 파싱

		/* ② 표 찾고, 검색창 끼우고, 실시간 필터 */
		var $table = $('.mw-dictionary').first();
		var $input  = new OO.ui.TextInputWidget({
			placeholder: '단어·뜻 검색…',
			icons: ['search'],
			indicator: null
		}).$element.css({ width: '100%', 'margin-bottom': '0.5em' });

		$table.before($input);

		// 3) 필터링 로직
		$input.on('input', function () {
			var q = $(this).val().trim().toLowerCase();
			$table.find('tr').each(function () {
				var $row = $(this);
				if ($row.find('th').length) return; // 헤더는 건너뜀
				var term = $row.children('td').eq(0).text().toLowerCase();
				var def  = $row.children('td').eq(1).text().toLowerCase();
				$qMatch = !q || term.includes(q) || def.includes(q);
				$row.toggle($qMatch);
			});
		});
	});
});