MediaWiki:Vector.js: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "All JavaScript here will be loaded for users of the Vector skin: // Add copy buttons to all <syntaxhighlight> blocks document.addEventListener("DOMContentLoaded", () => { document.querySelectorAll("div.mw-highlight").forEach((block) => { const button = document.createElement("button"); button.className = "copy-button"; button.textContent = "Copy"; button.addEventListener("click", () => { const code = block.innerText;..."
 
No edit summary
 
Line 1: Line 1:
/* All JavaScript here will be loaded for users of the Vector skin */
// Function to add copy buttons to <syntaxhighlight> or <pre> blocks
function addCopyButtons() {
    document.querySelectorAll("div.mw-highlight").forEach((block) => {
        // Avoid adding duplicate buttons
        if (block.querySelector(".copy-button")) return;


// Add copy buttons to all <syntaxhighlight> blocks
        // Create the copy button
document.addEventListener("DOMContentLoaded", () => {
    document.querySelectorAll("div.mw-highlight").forEach((block) => {
         const button = document.createElement("button");
         const button = document.createElement("button");
         button.className = "copy-button";
         button.className = "copy-button";
         button.textContent = "Copy";
         button.textContent = "Copy";


        // Add click event to copy the code
         button.addEventListener("click", () => {
         button.addEventListener("click", () => {
             const code = block.innerText;
             const code = block.innerText.trim(); // Get the text inside the block
             navigator.clipboard.writeText(code).then(() => {
             navigator.clipboard.writeText(code).then(() => {
                 button.textContent = "Copied!";
                 button.textContent = "Copied!";
Line 16: Line 19:
         });
         });


         block.style.position = "relative";
         // Style the button and append it to the block
        block.style.paddingTop = "30px"; // Add space for the button
         button.style.position = "absolute";
         button.style.position = "absolute";
         button.style.top = "10px";
         button.style.top = "10px";
Line 29: Line 31:
         button.style.fontSize = "12px";
         button.style.fontSize = "12px";


        block.style.position = "relative"; // Ensure the block is positioned relatively
        block.style.paddingTop = "30px"; // Add space for the button
         block.appendChild(button);
         block.appendChild(button);
     });
     });
});
}
 
// Run the function on initial page load
document.addEventListener("DOMContentLoaded", addCopyButtons);
 
// Run the function whenever new content is added to the page
mw.hook("wikipage.content").add(addCopyButtons);

Latest revision as of 23:55, 22 March 2025

// Function to add copy buttons to <syntaxhighlight> or <pre> blocks
function addCopyButtons() {
    document.querySelectorAll("div.mw-highlight").forEach((block) => {
        // Avoid adding duplicate buttons
        if (block.querySelector(".copy-button")) return;

        // 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.trim(); // Get the text inside the block
            navigator.clipboard.writeText(code).then(() => {
                button.textContent = "Copied!";
                setTimeout(() => (button.textContent = "Copy"), 2000);
            });
        });

        // Style the button and append it to the 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";

        block.style.position = "relative"; // Ensure the block is positioned relatively
        block.style.paddingTop = "30px"; // Add space for the button
        block.appendChild(button);
    });
}

// Run the function on initial page load
document.addEventListener("DOMContentLoaded", addCopyButtons);

// Run the function whenever new content is added to the page
mw.hook("wikipage.content").add(addCopyButtons);