사용자:하늘/addCopyButtonToCode.js: 두 판 사이의 차이

편집 요약 없음
편집 요약 없음
7번째 줄: 7번째 줄:
copybutton.addEventListener('click', function() {
copybutton.addEventListener('click', function() {
copyCode(code);
copyCode(code);
copybutton.innerText = "복사됨";
copybutton.innerText = "copy!";
setTimeout(function(){
setTimeout(function(){
copybutton.innerText = "copy";
copybutton.innerText = "copy";
}, 100);
}, 500);
});
});
copybutton.style = "float:right";
copybutton.style = "float:right";

2022년 4월 3일 (일) 21:30 판

function addCopyLinkToCodeElements() {
	const elements = document.querySelectorAll(".mw-parser-output .mw-highlight");
	elements.forEach(function(element) {
		const copybutton = document.createElement('button');
		const code = element.firstChild.innerText;
		copybutton.innerText = "copy";
		copybutton.addEventListener('click', function() {
			copyCode(code);
			copybutton.innerText = "copy!";
			setTimeout(function(){
				copybutton.innerText = "copy";
			}, 500);
		});
		copybutton.style = "float:right";
		const endBlock = document.createElement('div');
		endBlock.style = "clear:both";
		element.appendChild(copybutton);
		element.appendChild(endBlock);
	});
}

function copyCode(codeText) {
	navigator.clipboard.writeText(codeText);
}

$(function() {addCopyLinkToCodeElements()});