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

편집 요약 없음
편집 요약 없음
2번째 줄: 2번째 줄:
const elements = document.querySelectorAll(".mw-parser-output .mw-highlight");
const elements = document.querySelectorAll(".mw-parser-output .mw-highlight");
elements.forEach(function(element) {
elements.forEach(function(element) {
console.log(element);
const copybutton = document.createElement('button');
const copybutton = document.createElement('button');
const code = element.firstChild.innertext;
const code = element.firstChild.innertext;
console.log(code);
copybutton.innertext = "copy";
copybutton.innertext = "copy";
copybutton.addEventListener('click',function() {copyCode(code)});
copybutton.addEventListener('click',function() {copyCode(code)});

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

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;
		console.log(code);
		copybutton.innertext = "copy";
		copybutton.addEventListener('click',function() {copyCode(code)});
		element.appendChild(copybutton);
	});
}

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