MediaWiki:Gadget-PreCopy.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes.
- Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
- Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
- Opera, Konqueror and Safari users can just click the Reload button.
- Chrome: press Ctrl+F5 or Shift+F5
// Adds copy button to all <pre> elements in page, good for documentation
mw.hook('wikipage.content').add(function ($content) {
$content.find('.mw-parser-output pre').each(function () {
var pre = this;
if (pre.closest('.mw-pre-wrapper')) return;
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.className = 'mw-copy-btn';
btn.addEventListener('click', function () {
navigator.clipboard.writeText(pre.textContent).then(function () {
btn.textContent = 'Copied!';
setTimeout(function () { btn.textContent = 'Copy'; }, 1200);
}, function () {
btn.textContent = 'Error';
});
});
var wrapper = document.createElement('div');
wrapper.className = 'mw-pre-wrapper';
pre.parentNode.insertBefore(wrapper, pre);
wrapper.appendChild(pre);
wrapper.appendChild(btn);
});
});