Код:
<script> async function copyToReply(c) { let text = c.innerText || c.textContent; let textareaMess = document.querySelector("#main-reply"); if (textareaMess) { textareaMess.value += (textareaMess.value ? "\n\n" : "") + text; textareaMess.focus(); textareaMess.selectionStart = textareaMess.selectionEnd = textareaMess.value.length; setTimeout(() => { textareaMess.scrollIntoView({ behavior: "smooth", block: "center" }); }, 100); } } function initCopyToReplyButtons() { $(".code-box .legend").each(function () { let $legend = $(this); if ($legend.find(".copy-to-reply-button").length === 0) { let button = $('<a href="javascript://" class="copy-to-reply-button">Скопировать в форму ответа</a>'); $legend.append(" | ").append(button); } }); } $(document).on("click", ".copy-to-reply-button", function () { let codeBlock = $(this).closest(".code-box").find(".blockcode pre").get(0); if (codeBlock) { copyToReply(codeBlock); } }); $(document).on("pun_post pun_edit pun_preview messenger:post messenger:messages_ready messenger:messages_load WYSI_init WYSI_insert", initCopyToReplyButtons) .pun_mainReady(initCopyToReplyButtons); </script> <script> async function copyToReplyFromData(button) { let textCopyCode = button.dataset.copycode; let textareaCopyCode = document.querySelector("#main-reply"); if (textareaCopyCode) { textareaCopyCode.value += (textareaCopyCode.value ? "\n\n" : "") + textCopyCode; textareaCopyCode.focus(); textareaCopyCode.selectionStart = textareaCopyCode.selectionEnd = textareaCopyCode.value.length; setTimeout(() => { textareaCopyCode.scrollIntoView({ behavior: "smooth", block: "center" }); }, 100); } try { await navigator.clipboard.writeText(textCopyCode); } catch { fallbackCopy(textCopyCode); } } function fallbackCopy(textCopyCode) { let temp = document.createElement("textarea"); document.body.appendChild(temp); temp.value = textCopyCode; temp.select(); try { document.execCommand("copy"); } catch (e) {} document.body.removeChild(temp); } $(document).on("click", ".copy-to-reply-toform", function () { copyToReplyFromData(this); }); </script>