2024-10-03 14:38:56 +02:00
|
|
|
export const copyCodeToClipboard = () => {
|
2024-10-01 16:44:34 +02:00
|
|
|
const codeblocks = document.querySelectorAll("pre>code");
|
|
|
|
|
for (const b of codeblocks) {
|
|
|
|
|
const button = document.createElement("button");
|
|
|
|
|
button.innerHTML = "Kopiera";
|
2024-10-03 14:38:56 +02:00
|
|
|
button.addEventListener("click", function ({ target }) {
|
2024-10-01 16:44:34 +02:00
|
|
|
const text = target.previousSibling.innerHTML;
|
|
|
|
|
navigator.clipboard.writeText(text);
|
2024-10-03 14:38:56 +02:00
|
|
|
});
|
2024-10-01 16:44:34 +02:00
|
|
|
b.parentNode.appendChild(button);
|
|
|
|
|
}
|
2024-10-03 14:38:56 +02:00
|
|
|
};
|
2024-10-01 16:44:34 +02:00
|
|
|
|
2024-10-03 14:38:56 +02:00
|
|
|
export const copyUrlToClipboard = () => {
|
|
|
|
|
const permalinks = document.querySelectorAll(".permalink");
|
|
|
|
|
for (const pl of permalinks) {
|
|
|
|
|
pl.setAttribute(
|
|
|
|
|
"title",
|
|
|
|
|
pl.getAttribute("title") + ", klicka för att kopiera",
|
|
|
|
|
);
|
|
|
|
|
pl.addEventListener("click", function (evt) {
|
|
|
|
|
const { target, shiftKey, ctrlKey, altKey } = evt;
|
|
|
|
|
if (!shiftKey && !ctrlKey && !altKey) {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
const text = target.href;
|
|
|
|
|
navigator.clipboard.writeText(text);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|