편집 요약 없음 |
편집 요약 없음 |
||
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 | |||
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', | |||
title: 'copy', | title: 'copy', | ||
classes: ['copy-button'] | classes: ['copy-button'] | ||
}); | }); | ||
const | const codeText = element.firstChild.innerText; | ||
copybutton.on('click', function () { copy( | 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'], | mw.loader.using(['oojs-ui-core'], 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);