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

편집 요약 없음
편집 요약 없음
12번째 줄: 12번째 줄:
}, 500);
}, 500);
});
});
copybutton.style = "float:right";
copybutton.style = "float:right; margin-left: -9999999px; margin-top: 0.7em; margin-right: 0.7em;";
const endBlock = document.createElement('div');
const endBlock = document.createElement('div');
endBlock.style = "clear:both";
endBlock.style = "clear:both";

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

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; margin-left: -9999999px; margin-top: 0.7em; margin-right: 0.7em;";
		const endBlock = document.createElement('div');
		endBlock.style = "clear:both";
		element.prepend(copybutton);
		element.appendChild(endBlock);
	});
}

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

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