Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function () {
function fallbackCopy(text) {
var textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', 'readonly');
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
} finally {
document.body.removeChild(textarea);
}
}
function copyText(text) {
if (navigator.clipboard && window.isSecureContext) {
return navigator.clipboard.writeText(text);
}
return new Promise(function (resolve) {
fallbackCopy(text);
resolve();
});
}
function flashButton(button, label) {
var original = button.getAttribute('data-original-label') || 'Copy';
button.textContent = label;
window.setTimeout(function () {
button.textContent = original;
}, 1400);
}
function enhanceCodeBlock(container) {
var pre = container.matches('pre') ? container : container.querySelector('pre');
if (!pre || pre.dataset.instantCopyReady === '1') {
return;
}
pre.dataset.instantCopyReady = '1';
var host = container.classList.contains('mw-highlight') ? container : pre;
var wrapper = document.createElement('div');
wrapper.className = 'oc-copy-wrap';
host.parentNode.insertBefore(wrapper, host);
wrapper.appendChild(host);
var button = document.createElement('button');
button.type = 'button';
button.className = 'oc-copy-button';
button.textContent = 'Copy';
button.setAttribute('data-original-label', 'Copy');
button.setAttribute('aria-label', 'Copy code block');
button.addEventListener('click', function () {
copyText(pre.innerText || pre.textContent || '')
.then(function () {
flashButton(button, 'Copied');
})
.catch(function () {
flashButton(button, 'Failed');
});
});
wrapper.appendChild(button);
}
function initCopyButtons($content) {
var root = $content && $content[0] ? $content[0] : document;
var nodes = root.querySelectorAll('.mw-highlight, pre');
nodes.forEach(function (node) {
if (node.closest('.oc-copy-wrap')) {
return;
}
enhanceCodeBlock(node);
});
}
if (window.mw && mw.hook) {
mw.hook('wikipage.content').add(initCopyButtons);
} else {
document.addEventListener('DOMContentLoaded', function () {
initCopyButtons(document);
});
}
}());