MediaWiki:Common.js
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.
/* Any JavaScript here will be loaded for all users on every page load. */
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll("syntaxhighlight").forEach((block) => {
// Create the copy button
const button = document.createElement("button");
button.className = "copy-button";
button.textContent = "Copy";
// Add click event to copy the code
button.addEventListener("click", () => {
const code = block.innerText;
navigator.clipboard.writeText(code).then(() => {
button.textContent = "Copied!";
setTimeout(() => (button.textContent = "Copy"), 2000);
});
});
// Style the button and append it to the <syntaxhighlight> block
button.style.position = "absolute";
button.style.top = "10px";
button.style.right = "10px";
button.style.backgroundColor = "#444";
button.style.color = "white";
button.style.border = "none";
button.style.borderRadius = "5px";
button.style.padding = "5px 10px";
button.style.cursor = "pointer";
button.style.fontSize = "12px";
// Ensure the <syntaxhighlight> block is positioned relatively
block.style.position = "relative";
block.style.paddingTop = "30px"; // Add space for the button
block.appendChild(button);
});
});