<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Clipboard Applier</title>
        <script>
            // raw params without leading '?'
            const raw = window.location.search.substring(1);
            var toApply;
            if (raw.startsWith('u=')) toApply = decodeURI(raw.substring(2));
            if (raw.startsWith('b=')) toApply = atob(raw.substring(2));
            if (raw.startsWith('r=')) toApply = raw.substring(2);
            window.onload = async () => {
                document.getElementById('fallback').textContent = toApply;
                if (!navigator.clipboard) {
                    console.error('Clipboard API is not available');
                    document.getElementById('failure').style.display = 'block';
                    document.getElementById('failapiavailable').style.display = 'block';
                } else {
                    try {
                        await navigator.clipboard.writeText(toApply);
                        console.log('Applied value to clipboard sucessfully!');
                        document.getElementById('success').style.display = 'block';
                        window.top.close();
                        window.close();
                    } catch (e) {
                        console.error('Clipboard API permissions denied');
                        console.error(e);
                        document.getElementById('failure').style.display = 'block';
                        document.getElementById('failpermission').style.display = 'block';
                    }
                }
            }
        </script>
    </head>
    <body>
        <noscript style="text-align: center;">
            <h1>Oh no!</h1>
            <p>This page requires javascript in order to apply a value to your clipboard.</p>
            <p>Please enable Javascript to continue.</p>
        </noscript>
        <div id="success" style="display: none; text-align: center;">
            <h1>Success!</h1>
            <p>You can now close this page or view the copied text below:</p>
        </div>
        <div id="failure" style="display: none; text-align: center;">
            <h1>Oh no!</h1>
            <p id="failapiavailable" style="display: none;">
                Your browser does not support the JavaScript Clipboard API, this means that we are unable to apply the requested text to your clipboard.
            </p>
            <p id="failpermission" style="display: none;">
                To apply the value to your clipboard we need access to the JavaScript Clipboard API, please provide the adequate permissions or copy from the textbox below.
            </p>
            <p>You can still copy the text below:</p>
        </div>
        <p id="fallback" style="text-align: center; border: 1px solid black; margin: 5px; padding: 2.5px 0;"></p>
    </body>
</html>