(소스 테스트) |
(removeItem 테스트) |
||
20번째 줄: | 20번째 줄: | ||
libreNavLogin.navBarData = JSON.parse(getRaw(dataPageName)); | libreNavLogin.navBarData = JSON.parse(getRaw(dataPageName)); | ||
libreNavLogin.titleMap = { | |||
"dropRC": "위키의 최근 바뀐 목록", | |||
"dropRandom": "무작위로 선택된 문서 불러오기", | |||
"dropMap": "https://maps.librewiki.net/", | |||
"dropBoard": "게시판", | |||
"dropFreeBoard": "https://bbs.librewiki.net/freeboard", | |||
"dropTools": "도구", | |||
"dropUpload": "특수:올리기", | |||
"dropHelp": "도움말", | |||
"dropSyntax": "도움말:위키_문법", | |||
"dropTex": "도움말:TeX_문법" | |||
}; | |||
libreNavLogin.createItem = function(icon, title, url, desc) { | libreNavLogin.createItem = function(icon, title, url, desc) { | ||
43번째 줄: | 55번째 줄: | ||
}; | }; | ||
libreNavLogin.removeItem = function(name) { | libreNavLogin.removeItem = function(name) { | ||
var Selector = '[title="' + name + '"]'; | var Selector = '[title^="' + name + '"]'; // 시작하는 데이터로 수정 | ||
document.querySelector(Selector).remove(); | document.querySelector(Selector).remove(); | ||
}; | }; | ||
49번째 줄: | 61번째 줄: | ||
for (const item of this.navBarData.newNavItems) { | for (const item of this.navBarData.newNavItems) { | ||
this.addNavItem(item[0], item[1], item[2], item[3]); | this.addNavItem(item[0], item[1], item[2], item[3]); | ||
} | |||
for (const key in this.titleMap) { // titleMap에서 key값 추출 | |||
if (this.navBarData[key] ) { | |||
this.removeItem(this.titleMap[key]); | |||
} | |||
} | } | ||
}; | }; |
2022년 11월 11일 (금) 00:53 판
// 원본 : 사용자:하늘/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;
}
console.log(getRaw(dataPageName));
// 아이템 추가
var libreNavLogin = {};
libreNavLogin.navBarData = JSON.parse(getRaw(dataPageName));
libreNavLogin.titleMap = {
"dropRC": "위키의 최근 바뀐 목록",
"dropRandom": "무작위로 선택된 문서 불러오기",
"dropMap": "https://maps.librewiki.net/",
"dropBoard": "게시판",
"dropFreeBoard": "https://bbs.librewiki.net/freeboard",
"dropTools": "도구",
"dropUpload": "특수:올리기",
"dropHelp": "도움말",
"dropSyntax": "도움말:위키_문법",
"dropTex": "도움말:TeX_문법"
};
libreNavLogin.createItem = function(icon, title, url, desc) {
var a = document.createElement('a'); // 링크 추가
var iconHTML = document.createElement('i'); // icon 추가
iconHTML.className = "fa "+icon;
iconHTML.style.color = '#373a3c'; // 색상 변경
iconHTML.style.fontSize = '0.9em'; // 사이즈 변경
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(icon, name, url, desc);
document.getElementsByClassName('dropdown-divider')[0].before(node);
};
libreNavLogin.removeItem = function(name) {
var Selector = '[title^="' + name + '"]'; // 시작하는 데이터로 수정
document.querySelector(Selector).remove();
};
libreNavLogin.setNavItems = function() {
for (const item of this.navBarData.newNavItems) {
this.addNavItem(item[0], item[1], item[2], item[3]);
}
for (const key in this.titleMap) { // titleMap에서 key값 추출
if (this.navBarData[key] ) {
this.removeItem(this.titleMap[key]);
}
}
};
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();