Back to feed
ForumSpark Support Themes & Extensions Join

Copy Code

Cory · 7 months ago · 0 replies · 27

Preview

When the "Copy Code" link is clicked, it will copy all code in between the code BBCode tags.

Admin CP → Themes → Board Templates → Below the board

<script>
const code = document.querySelectorAll('code');

if (code) {
    code.forEach(element => {
        element.parentElement.insertAdjacentHTML('beforebegin', '<a href="javascript: void(0)" class="copy-code" style="float: right">Copy Code</a><br>');
    });

const copyCode = document.querySelectorAll('a.copy-code');

copyCode.forEach(element => {
    element.addEventListener('click', function() {
        element.insertAdjacentHTML('beforeend', '<div id="code-copied" class="panel" style="position: absolute; border-width: 1px; border-radius: 5px; padding: 3px; color: initial">Code Copied</div>');
        const text = element.nextElementSibling.nextElementSibling.querySelector('code').innerText;
        navigator.clipboard.writeText(text);

        setTimeout(function() {
            document.querySelector('#code-copied').remove();
        }, 1000);
    });
});
}
</script>
Create a free account to join the conversation.
Sign up free
ForumSpark © 2023 - 2026 · ForumSpark.net · Terms