MediaWiki:Common.js: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "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", () => {..."
 
Tag: Replaced
 
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* 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);
    });
});

Latest revision as of 23:15, 22 March 2025

/* Any JavaScript here will be loaded for all users on every page load. */