// ==UserScript==
// @name         Gnarly pasword filler by AZXAD✨
// @namespace    <http://tampermonkey.net/>
// @version      1.2
// @description  Auto-fill "gnarly", submit form, and show large watermark on bin.0xfc.de after password entry.
// @author       axad
// @match        <https://bin.0xfc.de/*>
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const PASSWORD = 'gnarly';

    function autoFillAndSubmit() {
        const input = document.getElementById('passworddecrypt');
        const button = document.querySelector('#passwordform button[type="submit"]');

        if (input && button) {
            input.value = PASSWORD;

            input.dispatchEvent(new Event('input', { bubbles: true }));
            input.dispatchEvent(new Event('change', { bubbles: true }));

            console.log('[Tampermonkey] Password filled. Submitting...');

            button.click();

            // Add the watermark after the modal is gone
            setTimeout(addWatermark, 1500);
        }
    }

    function addWatermark() {
        if (document.getElementById('axad-watermark')) return;

        const link = document.createElement('a');
        link.href = '<https://www.reddit.com/user/azxad>';
        link.target = '_blank';
        link.textContent = 'made with 💖 by AZXAD';
        link.id = 'axad-watermark';

        Object.assign(link.style, {
            position: 'fixed',
            top: '50%',
            right: '12px',
            transform: 'translateY(-50%)',
            fontSize: '18px',
            fontFamily: 'monospace',
            color: '#ffffffcc',
            textDecoration: 'none',
            backgroundColor: '#00000066',
            padding: '6px 12px',
            borderRadius: '8px',
            zIndex: 99999,
            pointerEvents: 'auto',
            userSelect: 'none',
            boxShadow: '0 0 8px #00000055'
        });

        document.body.appendChild(link);
    }

    const observer = new MutationObserver((mutations) => {
        for (let mutation of mutations) {
            mutation.addedNodes.forEach((node) => {
                if (node.nodeType === 1 && node.querySelector?.('#passwordform')) {
                    setTimeout(autoFillAndSubmit, 100);
                }
            });
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

    window.addEventListener('load', () => {
        setTimeout(autoFillAndSubmit, 500);
    });
})();