<!-- shared/_copy_text.js.erb begins -->
<script>
"use strict";

// *deep sigh*
function copyTextToClipboard(text)
{
    if (!navigator.clipboard) {
        const textArea = document.createElement("textarea");

        textArea.value = text;
        textArea.style.top = "0";
        textArea.style.left = "0";
        textArea.style.position = "fixed";

        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();

        try {
            if (!document.execCommand("copy"))
                window.alert("Can't copy the text. Sorry.");
        } catch (e) {
            console.error(e);
            window.alert("Can't copy the text. See the console for details. Sorry.");
        }

        document.body.removeChild(textArea);
        return;
    }

    try {
        navigator.clipboard.writeText(text).then(
            () => {},
            (e) => {
                console.error(e);
                window.alert(e);
            }
        );
    } catch (e) {
        console.error(e);
        window.alert("Can't copy the text. See the console for details. Sorry.");
    }
}
</script>
<!-- shared/_copy_text.js.erb ends -->
