MediaWiki:Common.js: Difference between revisions
새 문서: →이 자바스크립트 설정은 모든 문서, 모든 사용자에게 적용됩니다.: →TOP 버튼: var topButton = document.createElement('div'); topButton.id = 'top-button'; document.body.appendChild(topButton); window.addEventListener('scroll', function (e) { if (window.scrollY > 300) { topButton.classList.add('active'); } else { topButton.classList.remove('active'); } }); topButton.addEventListener('click', function (e) { try {... |
No edit summary |
||
| Line 101: | Line 101: | ||
document.body.appendChild(kakao_cdn); | document.body.appendChild(kakao_cdn); | ||
window. | // 페이지 로딩 이후 초기화해야할 내용 정의 | ||
initScript(); | |||
function initScript() { | |||
document.querySelector(' | var retry = false; | ||
// 카카오 링크 초기화 | |||
if (window.initKakaoLink != true) { | |||
}); | var $kakao = document.querySelector('[title="Share on KakaoTalk"]'); | ||
}; | if (window.Kakao && $kakao) { | ||
// 사용할 앱의 JavaScript 키를 설정해 주세요. | |||
Kakao.init('132b053079230ffc0880fd0a8155f444'); | |||
$kakao.children[0].style.display = 'inline'; | |||
}; | $kakao.onclick = sendScrap; // 카카오 Link 공유 API 사용 | ||
window.initKakaoLink = true; | |||
} else retry = true; | |||
} | |||
// 링크 복사 초기화 | |||
if (window.initCopyLink != true) { | |||
var $copy = document.querySelector('.wikishare_button_Copy'); | |||
if ($copy) { | |||
$copy.style.display = 'inline'; | |||
$copy.addEventListener('click', function (e) { | |||
copyStringToClipboard(decodeURI(location.href)); | |||
alert('링크를 클립보드에 저장했습니다.'); | |||
e.preventDefault(); | |||
}); | |||
window.initCopyLink = true; | |||
} else retry = true; | |||
} | |||
// 애널리틱스 | |||
if (window.initWcs != true) { | |||
if (window.wcs) { | |||
wcs_do(); | |||
window.initWcs = true; | |||
} else retry = true; | |||
} | |||
if (retry) setTimeout(initScript, 300); | |||
} | |||
} | |||
// 문자열을 클립보드에 저장 | |||
function copyStringToClipboard(str) { | |||
var el = document.createElement('textarea'); | |||
el.value = str; | |||
el.setAttribute('readonly', ''); | |||
el.style = { position: 'absolute', left: '-9999px' }; | |||
document.body.appendChild(el); | |||
el.select(); | |||
document.execCommand('copy'); | |||
document.body.removeChild(el); | |||
} | |||
function sendScrap() { | |||
// 카카오 Link 공유 API 사용 | |||
Kakao.Link.sendScrap({ | |||
requestUrl: location.href, | |||
templateId: 82911, | |||
templateArgs: { | |||
TITLE: og('title', '하나님의 교회 지식사전'), | |||
DESC: og('description'), | |||
THUMB: thumb(), | |||
PAGE: path(), | |||
}, | |||
}); | |||
function og(name, defaultVal) { | |||
var $meta = document.querySelector('meta[property="og:' + name + '"]'); | |||
if ($meta) return $meta.getAttribute('content'); | |||
else return defaultVal ? defaultVal : ''; | |||
} | |||
function thumb() { | |||
var img = og('image'); | |||
if (!img) { | |||
var $img = document.querySelector('img[src]'); | |||
if ($img) { | |||
img = $img.src; | |||
} | |||
} | |||
if (img.indexOf('http') != 0) img = location.origin + (img[0] == '/' ? img : '/' + img); | |||
return img; | |||
} | |||
function path() { | |||
if (location.pathname[0] == '/') return location.pathname.substring(1) + location.search; | |||
else return location.pathname + location.search; | |||
} | |||
} | |||
// 앨범 레이아웃 보정 | |||
updateAllAlbum(); | |||
window.addEventListener('load', updateAllAlbum); | |||
window.addEventListener('resize', updateAllAlbum); | |||
function updateAllAlbum() { | |||
var $albums = document.querySelectorAll('.custom-album'); | |||
for (var i = 0; i < $albums.length; i++) { | |||
updateAlbum($albums[i]); | |||
} | |||
} | |||
function updateAlbum($elem) { | |||
var lineMax = 3; | |||
for (var i = 0; i < $elem.classList.length; i++) { | |||
if ($elem.classList[i].indexOf('line-max-') == 0) { | |||
lineMax = Number($elem.classList[i].replace('line-max-', '')); | |||
} | |||
} | |||
var totalWidth = $elem.clientWidth; | |||
if ($elem.children.length > 0) { | |||
for (var i = $elem.children.length - 1; i >= 0; i--) { | |||
if (!$elem.children[i].classList.contains('albumitem')) { | |||
$elem.children[i].remove(); | |||
continue; | |||
} | |||
$elem.children[i].style.width = ''; | |||
$elem.children[i].style.height = ''; | |||
} | |||
if (window.innerWidth < 720) return; | |||
var lineTotalRatio = []; | |||
for (var i = 0; i < $elem.children.length; i++) { | |||
var lineIdx = Math.floor(i / lineMax); | |||
var $img = $elem.children[i].querySelector('img'); | |||
if (!$img || $img.naturalWidth == 0) continue; | |||
if (lineTotalRatio[lineIdx] == undefined) { | |||
lineTotalRatio[lineIdx] = 0; | |||
} | |||
lineTotalRatio[lineIdx] += $img.naturalWidth / $img.naturalHeight; | |||
} | |||
for (var i = 0; i < $elem.children.length; i++) { | |||
var totalRatio = lineTotalRatio[Math.floor(i / lineMax)]; | |||
var $img = $elem.children[i].querySelector('img'); | |||
if (!$img || $img.naturalWidth == 0) continue; | |||
var ratio = $img.naturalWidth / $img.naturalHeight; | |||
$elem.children[i].style.width = (ratio / totalRatio) * (totalWidth - 6 * lineMax) + 6 + 'px'; | |||
} | |||
} | |||
} | } | ||