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

편집 요약 없음
편집 요약 없음
1번째 줄: 1번째 줄:
function addCopyLinkToCodeElements() {
function addCopyLinkToCodeElements() {
mw.util.addCSS('.copy-button { float:right; margin-left: -9999999px; margin-top: 0.7em; margin-right: 0.7em; }')
mw.util.addCSS('.copy-button { float:right; margin-left: -9999999px; margin-top: 0.7em; margin-right: 0.7em; }')
  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) {
    var copybutton = new OO.ui.ButtonWidget({
var copybutton = new OO.ui.ButtonWidget({
      label: 'copy',
label: 'copy',
      icon: 'code',
/*icon: 'code',*/
      title: 'copy',
title: 'copy',
      classes: ['copy-button']
classes: ['copy-button']
    });
});
    const code = element.firstChild.innerText;
const code = element.firstChild.innerText;
    copybutton.on('click', function() { copyCode(code); });
copybutton.on('click', function () { copyCode(code); });
    const endBlock = document.createElement('div');
const endBlock = document.createElement('div');
    endBlock.style = "clear:both";
endBlock.style = "clear:both";
    $(element).prepend(copybutton.$element);
$(element).prepend(copybutton.$element);
    element.appendChild(endBlock);
element.appendChild(endBlock);
  });
});
}
}


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


$(function () {
mw.loader.using(['oojs-ui-core'], function () {
  addCopyLinkToCodeElements()
addCopyLinkToCodeElements()
});
});

2022년 9월 4일 (일) 13:35 판

function addCopyLinkToCodeElements() {
	mw.util.addCSS('.copy-button { float:right; margin-left: -9999999px; margin-top: 0.7em; margin-right: 0.7em; }')
	const elements = document.querySelectorAll(".mw-parser-output .mw-highlight");
	elements.forEach(function (element) {
		var copybutton = new OO.ui.ButtonWidget({
			label: 'copy',
			/*icon: 'code',*/
			title: 'copy',
			classes: ['copy-button']
		});
		const code = element.firstChild.innerText;
		copybutton.on('click', function () { copyCode(code); });
		const endBlock = document.createElement('div');
		endBlock.style = "clear:both";
		$(element).prepend(copybutton.$element);
		element.appendChild(endBlock);
	});
}

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

mw.loader.using(['oojs-ui-core'], function () {
	addCopyLinkToCodeElements()
});