2523a5ac创建于 2025年11月18日历史提交
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>go2rtc</title>
    <style>
        .controls {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            align-items: center;
        }

        .info {
            color: #888;
        }
    </style>
</head>
<body>

<script src="main.js"></script>

<main>
    <div class="controls">
        <button>stream</button>
        modes
        <label><input type="checkbox" name="webrtc" checked>webrtc</label>
        <label><input type="checkbox" name="mse" checked>mse</label>
        <label><input type="checkbox" name="hls" checked>hls</label>
        <label><input type="checkbox" name="mjpeg" checked>mjpeg</label>
    </div>
    <table>
        <thead>
        <tr>
            <th><label><input id="selectall" type="checkbox">name</label></th>
            <th>online</th>
            <th>commands</th>
        </tr>
        </thead>
        <tbody id="streams">
        </tbody>
    </table>
    <div class="info"></div>
</main>

<script>
    const templates = [
        '<a href="stream.html?src={name}">stream</a>',
        '<a href="links.html?src={name}">links</a>',
        '<a href="#" data-name="{name}">delete</a>',
    ];

    document.querySelector('.controls > button')
        .addEventListener('click', () => {
            const url = new URL('stream.html', location.href);

            const streams = document.querySelectorAll('#streams input');
            streams.forEach(i => {
                if (i.checked) url.searchParams.append('src', i.name);
            });

            if (!url.searchParams.has('src')) return;

            let mode = document.querySelectorAll('.controls input');
            mode = Array.from(mode).filter(i => i.checked).map(i => i.name).join(',');

            window.location.href = `${url}&mode=${mode}`;
        });

    const tbody = document.getElementById('streams');
    tbody.addEventListener('click', async ev => {
        if (ev.target.innerText !== 'delete') return;

        ev.preventDefault();

        const src = decodeURIComponent(ev.target.dataset.name);

        const message = `Please type the name of the stream "${src}" to confirm its deletion from the configuration. This action is irreversible.`;
        if (prompt(message) !== src) {
            alert('Stream name does not match. Deletion cancelled.');
            return;
        }

        const url = new URL('api/streams', location.href);
        url.searchParams.set('src', src);

        try {
            await fetch(url, {method: 'DELETE'});
            reload();
        } catch (error) {
            console.error('Failed to delete the stream:', error);
        }
    });

    document.getElementById('selectall').addEventListener('change', ev => {
        document.querySelectorAll('#streams input').forEach(el => {
            el.checked = ev.target.checked;
        });
    });

    function reload() {
        const url = new URL('api/streams', location.href);
        const checkboxStates = {};
        tbody.querySelectorAll('input[type="checkbox"][name]').forEach(checkbox => {
            checkboxStates[checkbox.name] = checkbox.checked;
        });
        fetch(url, {cache: 'no-cache'}).then(r => r.json()).then(data => {
            const existingIds = Array.from(tbody.querySelectorAll('tr')).map(tr => tr.dataset['id']);
            const fetchedIds = [];

            for (const [key, value] of Object.entries(data)) {
                const name = key.replace(/[<">]/g, ''); // sanitize
                fetchedIds.push(name);

                let tr = tbody.querySelector(`tr[data-id="${name}"]`);
                const online = value && value.consumers ? value.consumers.length : 0;
                const src = encodeURIComponent(name);
                const links = templates.map(link => link.replace('{name}', src)).join(' ');

                if (!tr) {
                    tr = document.createElement('tr');
                    tr.dataset['id'] = name;
                    tbody.appendChild(tr);
                }

                const isChecked = checkboxStates[name] ? 'checked' : '';
                tr.innerHTML =
                    `<td><label><input type="checkbox" name="${name}" ${isChecked}>${name}</label></td>` +
                    `<td><a href="api/streams?src=${src}">${online} / info</a> / <a href="api/streams?src=${src}&video=all&audio=all&microphone">probe</a> / <a href="net.html?src=${src}">net</a></td>` +
                    `<td>${links}</td>`;
            }

            // Remove old rows
            existingIds.forEach(id => {
                if (!fetchedIds.includes(id)) {
                    const trToRemove = tbody.querySelector(`tr[data-id="${id}"]`);
                    tbody.removeChild(trToRemove);
                }
            });
        });
    }

    // Auto-reload
    setInterval(reload, 1000);

    const url = new URL('api', location.href);
    fetch(url, {cache: 'no-cache'}).then(r => r.json()).then(data => {
        const info = document.querySelector('.info');
        info.innerText = `version: ${data.version} / config: ${data.config_path}`;
    });

    reload();
</script>

</body>
</html>