MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
Created page with "→Any JavaScript here will be loaded for all users on every page load.: $(function () { →Adds copy button Chatgp: function addCopyButton(pre) { if ($(pre).data('copyButtonAdded')) return; $(pre).data('copyButtonAdded', true); var $pre = $(pre).closest('.mw-highlight, pre').first(); var target = $(pre).text(); var $wrapper = $pre.parent(); $wrapper.css('position', 'relative'); var $btn = $('<button>')..." |
No edit summary |
||
| Line 2: | Line 2: | ||
$(function () { | $(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(); | |||
}); | }); | ||
Revision as of 09:54, 2 July 2026
/* 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();
});