사용자:하늘/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 { margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }');
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) {
23번째 줄: 23번째 줄:


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

2022년 9월 4일 (일) 16:23 판

function addCopyLinkToCodeElements() {
	mw.util.addCSS('.copy-button { 	margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }');
	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();
});