사용자:Zlzleking/liberty.js: 두 판 사이의 차이

편집 요약 없음
편집 요약 없음
3번째 줄: 3번째 줄:
elements.forEach(function(element) {
elements.forEach(function(element) {
console.log(element);
console.log(element);
const copybutton = document.createElement(button);
const copybutton = document.createElement('button');
const code = element.firstChild().innertext;
const code = element.firstChild().innertext;
copybutton.innertext = "copy";
copybutton.innertext = "copy";

2022년 4월 3일 (일) 13:06 판

function addCopyLinkToCodeElements() {
	const elements = document.querySelectorAll(".mw-parser-output .mw-highlight");
	elements.forEach(function(element) {
		console.log(element);
		const copybutton = document.createElement('button');
		const code = element.firstChild().innertext;
		copybutton.innertext = "copy";
		copybutton.addEventListener('click',function() {copyCode(code)});
		element.addChild(copybutton);
	});
}

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