사용자:Utolee90/custom-navbar.js

< 사용자:Utolee90
Utolee90 (토론 | 기여)님의 2022년 11월 11일 (금) 00:08 판 (일단 테스트용으로)

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

  • 파이어폭스 / 사파리: Shift 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5 또는 Ctrl-R을 입력 (Mac에서는 ⌘-R)
  • 구글 크롬: Ctrl-Shift-R키를 입력 (Mac에서는 ⌘-Shift-R)
  • 인터넷 익스플로러 / 엣지: Ctrl 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5를 입력.
  • 오페라: Ctrl-F5를 입력.
// 원본 : 사용자:하늘/custom-navbar.js
// 수정 : 사용자:Utolee90
// custom-navbar-data 불러오기 -> navBarData 형식으로 값이 호출됨.
var dataPageName = mw.config.get().wgPageName.replace('.js', '-data.json'); // custom-navbar-data 파일이름

// csv 파일 RAW 데이터를 얻기 위해서 필요
function getRaw(title) {
    var raw;
    $.ajax({
        async: false,
        url: location.origin + mw.util.getUrl(title || mw.title, { action: 'raw' }),
        success: function (data) { raw = data; }
    });
    return raw;
}

// 아이템 추가
var libreNavLogin = {};

libreNavLogin.createItem = function(icon, title, url, desc) {
  var a = document.createElement('a'); // 링크 추가
  var iconHTML = document.createElement('span'); // icon 추가
  iconHTML.className = "fas "+icon;
  iconHTML.style.color = 'grey'; // 색상 변경
  iconHTML.style.fontSize = '1em'; // 사이즈 변경
  a.className = "dropdown-item",
    a.href = url,
    a.innerHTML = title,
    a.title = desc;
  // icon이 빈 값이 아닐 때 아이콘 추가 
  if (icon != '' && icon !=undefined) {
     a.prepend(iconHTML); 
  }
  return a;
};

libreNavLogin.addNavItem = function(icon, name, url, desc) {
  var node = this.createItem(name, url, desc);
  document.getElementsByClassName('dropdown-divider')[0].before(node);
};
libreNavLogin.setNavItems = function() {
  var navBarData = getRaw(dataPageName);
  console.log(navBarData);
  for (const item of navBarData.newNavItems) {
    this.addNavItem(item[0], item[1], item[2], item[3]);
  }
};

libreNavLogin.removeItem = function(name) {
  var Selector = '[title="' + name + '"]';
  document.querySelector(Selector).remove();
};


libreNavLogin.setNavItems();

var api_params = {
  action: 'query',
  list: 'categorymembers',
  cmtitle: '분류:한자/문자',
  cmlimit: 'max',
  format: 'json',
  cmtype: 'page'
};

function navItem() {
  this.root = document.createElement('li');
  this.root.id = 'hanja-random';
  this.root.className = "nav-item";
  document.querySelector('.nav.navbar-nav').append(this.root);

  function link() {
    var span = () => { return document.createElement('span'); };
    var a = document.createElement('a');

    this.root = a;
    this.root.className = "nav-link";
    this.root.title = "한자 랜덤문서";

    this.icon = span();
    this.icon.style.fontWeight = "600",
      this.icon.innerText = '漢';

    this.title = span();
    this.title.className = "hide-title",
    this.title.innerText = ' 한자 랜덤';

    this.root.append(this.icon);
    this.root.append(this.title);

    var isloaded = false;
    this.root.onmouseenter = function() {
      if (isloaded === false) {
        var link = document.querySelector('#hanja-random a');
    	new mw.Api().get(api_params).done(function(data) {
          var pages = data.query.categorymembers;
          var randomTitle = pages[Math.floor(Math.random() * pages.length)].title;
          link.href = "/wiki/" + randomTitle;
          isloaded = true;
          console.log(randomTitle);
        });
      }
    };
  }
  this.link = new link();
  this.root.append(this.link.root);
}
// document.querySelector('.hide-logout.logout-btn').remove();
new navItem();