// ==UserScript==
// @name         Hanime Red Title and BBCode Copier
// @namespace    http://tampermonkey.net/
// @version      3.3
// @description  Capture title and video link to clipboard on hanime.red with enhanced BBCode functionality
// @author
// @match        https://hanime.red/*
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Wait for the DOM and additional content to be fully loaded
    window.addEventListener('load', function() {

        // Adding custom button styles
        GM_addStyle(`
            .custom-button {
                height: 15px;
                padding: 1px 5px;
                font-size: 10px;
                line-height: 10px;
                color: #c11b68 !important;
                background-color: #d4d4d4 !important;
                border: 1px solid #c11b68 !important;
                border-radius: 4px !important;
                cursor: pointer !important;
                margin-right: 0px !important;
                margin-left: 10px !important;
                display: inline-block !important;
                visibility: visible !important;
                opacity: 1 !important;
            }
            .custom-button:focus {
                background-color: #c11b68 !important;
                color: #d4d4d4 !important;
                border: 1px solid #d4d4d4 !important;
            }
        `);

        // Create buttons
        const titleButton = document.createElement('button');
        titleButton.innerText = 'TITLE';
        titleButton.className = 'custom-button';

        const bbcodeButton = document.createElement('button');
        bbcodeButton.innerText = 'BBCode';
        bbcodeButton.className = 'custom-button';

        // Find the button container
        const buttonContainer = document.querySelector('h4.text-whitelite.text-sm');
        if (buttonContainer) {
            console.log('Button container found:', buttonContainer);
            buttonContainer.appendChild(titleButton);
            buttonContainer.appendChild(bbcodeButton);
            console.log('Buttons added to the container.');
        } else {
            console.error('Button container not found!');
        }

        // Function to copy title
        titleButton.onclick = function() {
            const title = document.querySelector('h1.text-whitegray.text-lg');
            if (title) {
                GM_setClipboard(title.innerText);
                console.log('Title copied:', title.innerText);
            } else {
                console.error('Title not found!');
            }
        };

        // Function to handle BBCode button click with additional functionality
        bbcodeButton.onclick = function() {
            const imgElement = document.querySelector('img.attachment-full.size-full.wp-post-image');
            if (!imgElement) {
                console.error('Image not found!');
                return;
            }

            // Use the src of the image for line1
            const line1 = imgElement.getAttribute('src');
            console.log('Image URL captured:', line1);

            // Wait until the jw-media div is present
            const checkForJwMediaDiv = setInterval(function() {
                const jwMediaDiv = document.querySelector('div.jw-media.jw-reset');
                if (jwMediaDiv) {
                    clearInterval(checkForJwMediaDiv);
                    console.log('jw-media jw-reset div found:', jwMediaDiv);

                    // Locate the mp4 URL link
                    const videoElement = jwMediaDiv.querySelector('video');
                    if (videoElement) {
                        console.log('Video element found:', videoElement);
                        const videoUrl = videoElement.getAttribute('src');
                        if (videoUrl) {
                            console.log('MP4 video URL found:', videoUrl);

                            // Generate BBCode with the new format
                            const bbcode = `[img]${line1}[/img]\n\n[hide][mp4]${videoUrl}[/mp4][/hide]`;
                            GM_setClipboard(bbcode);
                            console.log('BBCode copied:', bbcode);
                        } else {
                            console.error('Video src attribute is empty!');
                        }
                    } else {
                        console.error('Video element not found inside jwMediaDiv!');
                    }
                } else {
                    console.log('jw-media jw-reset div not found yet, waiting...');
                }
            }, 500); // Check every 500 milliseconds

            // Set a timeout to stop checking after 10 seconds
            setTimeout(function() {
                clearInterval(checkForJwMediaDiv);
                console.error('jw-media jw-reset div not found after waiting.');
                // Proceed with the original function

                // Extract the video URL from the iframe's src attribute
                const iframe = document.querySelector('iframe');
                if (iframe) {
                    console.log('Iframe found:', iframe);
                    const videoUrl = iframe.src;
                    if (videoUrl) {
                        console.log('Video URL captured:', videoUrl);

                        // Generate BBCode with the original format
                        const bbcode = `[img]${line1}[/img]\n\n[hide][all]${videoUrl}[/all][/hide]`;
                        GM_setClipboard(bbcode);
                        console.log('BBCode copied:', bbcode);
                    } else {
                        console.error('Iframe src attribute is empty!');
                    }
                } else {
                    console.error('Iframe not found!');
                }
            }, 10000); // 10 seconds timeout
        };
    });
})();

it cannot locate div.jw-media.jw-reset.  Can you use other method to help me capture the mp4 video link?