사용자:LiteHell2/breadcumb.js

< 사용자:LiteHell2
LiteHell2 (토론 | 기여)님의 2015년 11월 5일 (목) 19:38 판

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

  • 파이어폭스 / 사파리: Shift 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5 또는 Ctrl-R을 입력 (Mac에서는 ⌘-R)
  • 구글 크롬: Ctrl-Shift-R키를 입력 (Mac에서는 ⌘-Shift-R)
  • 인터넷 익스플로러 / 엣지: Ctrl 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5를 입력.
  • 오페라: Ctrl-F5를 입력.
/*
 * written By. LiteHell
 * Warning : This script uses localStorage of your browser so BREADCUMB WILL BE DELETED IF YOU CLEAR STORAGE DATA FROM YOUR BROWSER.
 */
if (mw.config.exists("wgIsArticle") && mw.config.exists("wgPageName") && mw.config.exists("wgArticlePath")) {
    var conf = mw.config.get(["wgIsArticle", "wgPageName", "wgArticlePath"]);
    if (conf.wgIsArticle) {
        var itemName = "pageBreadcumbs";

        function initStorageIfRequired() {
            if (localStorage.getItem(itemName) == null)
                localStorage.setItem(itemName, "[]");
        }

        function setStorage(value) {
            initStorageIfRequired();
            localStorage.setItem(itemName, JSON.stringify(value));
        }

        function getStorage() {
            initStorageIfRequired();
            return JSON.parse(localStorage.getItem(itemName));
        }

        function makeBreadBar(arr, active) {
            var ol = document.createElement("ol");
            ol.className = "breadcrumb";
            for (var i = 0; i < arr.length; i++) {
                var now = arr[i];
                var li = document.createElement("li");
                if (now == active) {
                    li.className = "active";
                    li.textContent = now;
                } else {
                    var anchor = document.createElement("a");
                    anchor.href = conf.wgArticlePath.replace('$1', now);
                    anchor.title = now;
                    anchor.textContent = now;
                    li.appendChild(anchor);
                }
                ol.appendChild(li);
            }
            return ol;
        }

        var articles = getStorage();
        articles.push(conf.wgPageName);
        setStorage(articles, conf.wgPageName);
        var breadbar = makeBreadBar(articles, conf.wgPageName);
        var contentButtons = document.querySelector(".libre_content_tools");
        contentButtons.parentNode.insertBefore(breadbar, contentButtons);
    }
}