aScHEABL / Extract <li> Text Content

// ==UserScript==
// @name         Extract <li> Text Content
// @namespace    Violentmonkey Script
// @version      1.0
// @description  Extracts all <li> tag text content from current page
// @author       You
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    let button = null;

    function createCopyButton() {
        if (button) return;

        button = document.createElement('button');
        button.innerText = '📋 Copy Panel <li>';
        Object.assign(button.style, {
            position: 'fixed',
            top: '20px',
            right: '20px',
            zIndex: '9999',
            padding: '8px 12px',
            fontSize: '14px',
            backgroundColor: '#4CAF50',
            color: 'white',
            border: 'none',
            borderRadius: '6px',
            cursor: 'pointer',
            boxShadow: '0 2px 6px rgba(0,0,0,0.3)'
        });
        button.addEventListener('click', copyPanelListItems);
        document.body.appendChild(button);
    }

    function removeCopyButton() {
        if (button) {
            button.remove();
            button = null;
        }
    }

    function showToast(message) {
        const toast = document.createElement('div');
        toast.innerText = message;
        Object.assign(toast.style, {
            position: 'fixed',
            bottom: '20px',
            right: '20px',
            backgroundColor: '#333',
            color: '#fff',
            padding: '10px 15px',
            borderRadius: '5px',
            fontSize: '14px',
            zIndex: '10000',
            opacity: '0.95'
        });
        document.body.appendChild(toast);
        setTimeout(() => toast.remove(), 2000);
    }

    function copyPanelListItems() {
        const panel = document.getElementById('displayPanel');
        if (!panel) {
            showToast('❌ #displayPanel not found.');
            return;
        }

        const liElements = panel.querySelectorAll('li');
        const texts = [];

        liElements.forEach(li => {
            const text = li.textContent.trim();
            if (text) texts.push(text);
        });

        const output = texts.join('\n');
        if (!output) {
            showToast('⚠️ No <li> content found.');
            return;
        }

        navigator.clipboard.writeText(output).then(() => {
            showToast('✅ Copied!');
        }).catch(err => {
            console.error('Copy failed:', err);
            showToast('❌ Copy failed.');
        });
    }

    // Mutation observer to detect displayPanel availability and contents
    const observer = new MutationObserver(() => {
        const panel = document.getElementById('displayPanel');
        const liCount = panel ? panel.querySelectorAll('li').length : 0;

        if (panel && liCount > 0) {
            createCopyButton();
        } else {
            removeCopyButton();
        }
    });

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

    // Initial check in case content already loaded
    setTimeout(() => {
        const panel = document.getElementById('displayPanel');
        const liCount = panel ? panel.querySelectorAll('li').length : 0;
        if (panel && liCount > 0) {
            createCopyButton();
        }
    }, 500);
})();