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

편집 요약 없음
편집 요약 없음
2번째 줄: 2번째 줄:
mw.util.addCSS('.copy-button { margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }\
mw.util.addCSS('.copy-button { margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }\
.mw-highlight { position: relative; }');
.mw-highlight { position: relative; }');
const elements = document.querySelectorAll(".mw-parser-output .mw-highlight");
elements.forEach(function (element) {
const highlightElements = document.querySelectorAll(".mw-parser-output .mw-highlight");
highlightElements.forEach(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 codeText = element.firstChild.innerText;
copybutton.on('click', function () { copy(code); });
copybutton.on('click', function () { copy(codeText); });
const endBlock = document.createElement('div');
const endBlock = document.createElement('div');
endBlock.style = "clear:both";
endBlock.style = "clear:both";
23번째 줄: 25번째 줄:
}
}


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

2023년 12월 30일 (토) 21:46 판

function addCopyLinkToCodeElements() {
	mw.util.addCSS('.copy-button { 	margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }\
	.mw-highlight { position: relative; }');
	
	const highlightElements = document.querySelectorAll(".mw-parser-output .mw-highlight");
	
	highlightElements.forEach(element => {
		var copybutton = new OO.ui.ButtonWidget({
			label: 'copy',
			icon: 'code',
			title: 'copy',
			classes: ['copy-button']
		});
		const codeText = element.firstChild.innerText;
		copybutton.on('click', function () { copy(codeText); });
		const endBlock = document.createElement('div');
		endBlock.style = "clear:both";
		$(element).prepend(copybutton.$element);
		element.appendChild(endBlock);
	});
}

function copy(string) {
	navigator.clipboard.writeText(string);
}

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