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

편집 요약 없음
편집 요약 없음
15번째 줄: 15번째 줄:
const endBlock = document.createElement('div');
const endBlock = document.createElement('div');
endBlock.style = "clear:both";
endBlock.style = "clear:both";
element.appendChild(copybutton);
element.prepend(copybutton);
element.appendChild(endBlock);
element.appendChild(endBlock);
});
});

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

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.prepend(copybutton);
		element.appendChild(endBlock);
	});
}

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

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