미디어위키:Common.js: 두 판 사이의 차이

잉여 위키
둘러보기로 이동 검색으로 이동
새 문서: 이 자바스크립트 설정은 모든 문서, 모든 사용자에게 적용됩니다.: document.addEventListener("DOMContentLoaded", function () { document.querySelectorAll("div.mw-highlight").forEach(function (box) { var pre = box.querySelector("pre"); if (!pre) return; // 복사 버튼 중복 방지 if (box.querySelector(".code-copy-btn")) return; var button = document.createElement("button"); button.textContent = "...
태그: 다시 만듦
 
편집 요약 없음
 
1번째 줄: 1번째 줄:
/* 이 자바스크립트 설정은 모든 문서, 모든 사용자에게 적용됩니다. */
/* 이 자바스크립트 설정은 모든 문서, 모든 사용자에게 적용됩니다. */
document.addEventListener("DOMContentLoaded", function () {
/* SyntaxHighlight 코드 블록에 복사 버튼 추가 */
     document.querySelectorAll("div.mw-highlight").forEach(function (box) {
mw.hook('wikipage.content').add(function ($content) {
         var pre = box.querySelector("pre");
     $content.find('div.mw-highlight').each(function () {
        if (!pre) return;
        var box = this;
         var pre = box.querySelector('pre');


        // 복사 버튼 중복 방지
         if (!pre || box.querySelector('.code-copy-btn')) {
         if (box.querySelector(".code-copy-btn")) return;
            return;
        }


         var button = document.createElement("button");
         var button = document.createElement('button');
         button.textContent = "복사";
         button.type = 'button';
         button.className = "code-copy-btn";
         button.className = 'code-copy-btn';
         button.addEventListener("click", function () {
        button.textContent = '복사';
        button.setAttribute('aria-label', '코드 복사');
 
         button.addEventListener('click', function () {
             navigator.clipboard.writeText(pre.innerText).then(function () {
             navigator.clipboard.writeText(pre.innerText).then(function () {
                 button.textContent = "복사됨!";
                 button.textContent = '복사됨!';
                 setTimeout(() => button.textContent = "복사", 2000);
                 button.classList.add('copied');
 
                window.setTimeout(function () {
                    button.textContent = '복사';
                    button.classList.remove('copied');
                }, 2000);
            }).catch(function () {
                button.textContent = '복사 실패';
 
                window.setTimeout(function () {
                    button.textContent = '복사';
                }, 2000);
             });
             });
         });
         });


        // 박스 위치 보장
         box.classList.add('has-copy-button');
         box.style.position = "relative";
         box.appendChild(button);
         box.appendChild(button);
     });
     });
});
});

2026년 7월 14일 (화) 23:36 기준 최신판

/* 이 자바스크립트 설정은 모든 문서, 모든 사용자에게 적용됩니다. */
/* SyntaxHighlight 코드 블록에 복사 버튼 추가 */
mw.hook('wikipage.content').add(function ($content) {
    $content.find('div.mw-highlight').each(function () {
        var box = this;
        var pre = box.querySelector('pre');

        if (!pre || box.querySelector('.code-copy-btn')) {
            return;
        }

        var button = document.createElement('button');
        button.type = 'button';
        button.className = 'code-copy-btn';
        button.textContent = '복사';
        button.setAttribute('aria-label', '코드 복사');

        button.addEventListener('click', function () {
            navigator.clipboard.writeText(pre.innerText).then(function () {
                button.textContent = '복사됨!';
                button.classList.add('copied');

                window.setTimeout(function () {
                    button.textContent = '복사';
                    button.classList.remove('copied');
                }, 2000);
            }).catch(function () {
                button.textContent = '복사 실패';

                window.setTimeout(function () {
                    button.textContent = '복사';
                }, 2000);
            });
        });

        box.classList.add('has-copy-button');
        box.appendChild(button);
    });
});