MediaWiki:Common.js

From RVM Wiki
Revision as of 09:54, 2 July 2026 by WikiSysop (talk | contribs)
Jump to navigation Jump to search

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. */

$(function () {

    console.log("Copy button script loaded");

    function addButton(pre) {
        if (pre.dataset.copyBtn) return;
        pre.dataset.copyBtn = "1";

        const btn = document.createElement("button");
        btn.innerText = "???? Copia";

        btn.style.position = "absolute";
        btn.style.top = "4px";
        btn.style.right = "4px";
        btn.style.zIndex = "9999";
        btn.style.fontSize = "12px";
        btn.style.cursor = "pointer";

        const wrapper = pre.parentElement;
        if (getComputedStyle(wrapper).position === "static") {
            wrapper.style.position = "relative";
        }

        btn.addEventListener("click", async () => {
            try {
                await navigator.clipboard.writeText(pre.innerText);
                btn.innerText = "✔ Copiato";
                setTimeout(() => btn.innerText = "???? Copia", 1200);
            } catch (e) {
                console.error(e);
                alert("Clipboard non disponibile");
            }
        });

        wrapper.appendChild(btn);
    }

    function scan() {
        document.querySelectorAll("pre").forEach(addButton);
    }

    scan();
});