User:P.dewater/common.js
Jump to navigation
Jump to search
Note: After saving, 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)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
document.addEventListener('DOMContentLoaded', function () {
// Zoek alle codeblokken
document.querySelectorAll('.mw-highlight pre').forEach(function (preBlock) {
// Maak een knop
var button = document.createElement('button');
button.innerText = 'Copy Code';
button.style.marginBottom = '5px';
button.style.padding = '4px 8px';
button.style.fontSize = '12px';
button.style.cursor = 'pointer';
// Functie om code zonder regelnummers te kopiëren
button.addEventListener('click', function () {
var codeText = '';
preBlock.childNodes.forEach(function (node) {
if (node.nodeType === Node.TEXT_NODE) {
codeText += node.textContent;
} else if (node.nodeType === Node.ELEMENT_NODE) {
if (!node.classList.contains('linenos')) {
codeText += node.textContent;
}
}
});
navigator.clipboard.writeText(codeText).then(function () {
button.innerText = 'Copied!';
setTimeout(function () {
button.innerText = 'Copy Code';
}, 2000);
}, function (err) {
console.error('Failed to copy: ', err);
});
});
// Voeg de knop toe vóór het codeblok
preBlock.parentNode.insertBefore(button, preBlock);
});
});