x0/modules/copy.js

24 lines
No EOL
962 B
JavaScript

// [x-copy-query="<query selector>"]
// [x-inner-text] - will use innerText
(() => {
// TODO: make copy work with checksums because not uuid will not always cut it.
function updateCopy() {
document.querySelectorAll("[x-copy-query]:not([x1-copy-uuid])")
.forEach(e => {
let uuid = new Date().getTime().toString(36);
e.setAttribute("x1-copy-uuid", uuid);
if (e.hasAttribute("x-inner-text")) {
e.innerText = document.querySelector(e.getAttribute("x-copy-query")).innerHTML
} else {
e.innerHTML = document.querySelector(e.getAttribute("x-copy-query")).innerHTML
}
})
}
updateCopy();
window.x0?.registerModuleUpdateHook("copy", updateCopy);
window.x0?.registerModuleRemoveHook("copy", () => {
document.querySelectorAll("[x-copy-query]").forEach(e => e.innerHTML = "");
});
})()