PlayerCam

Bietet Auswahl aller angeschlossenen Webcams und aktiviert die gewählte als PlayerCam.

目前為 2024-12-31 提交的版本,檢視 最新版本

// ==UserScript==
// @name         PlayerCam
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Bietet Auswahl aller angeschlossenen Webcams und aktiviert die gewählte als PlayerCam.
// @author       YourName
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Erzeugt eine Dropdown-Auswahl für die Kameras
    const cameraSelect = document.createElement('select');
    cameraSelect.id = 'cameraSelect';
    cameraSelect.style.position = 'fixed';
    cameraSelect.style.top = '20px';
    cameraSelect.style.right = '20px';
    cameraSelect.style.zIndex = '1000';
    cameraSelect.style.padding = '10px';

    const activateButton = document.createElement('button');
    activateButton.textContent = 'Activate PlayerCam';
    activateButton.style.position = 'fixed';
    activateButton.style.top = '60px';
    activateButton.style.right = '20px';
    activateButton.style.zIndex = '1000';
    activateButton.style.padding = '10px 20px';
    activateButton.style.backgroundColor = '#007BFF';
    activateButton.style.color = '#FFFFFF';
    activateButton.style.border = 'none';
    activateButton.style.borderRadius = '5px';
    activateButton.style.cursor = 'pointer';

    document.body.appendChild(cameraSelect);
    document.body.appendChild(activateButton);

    // Videoelement für die Webcam-Ansicht erstellen
    const videoElement = document.createElement('video');
    videoElement.style.position = 'fixed';
    videoElement.style.bottom = '20px';
    videoElement.style.right = '20px';
    videoElement.style.width = '320px';
    videoElement.style.height = '240px';
    videoElement.style.border = '1px solid #000';
    videoElement.style.zIndex = '1000';
    videoElement.style.display = 'none'; // Standardmäßig ausgeblendet

    document.body.appendChild(videoElement);

    let selectedDeviceId = null;

    // Funktion, um verfügbare Kameras zu ermitteln
    async function getCameras() {
        try {
            const devices = await navigator.mediaDevices.enumerateDevices();
            const videoDevices = devices.filter(device => device.kind === 'videoinput');

            // Dropdown mit Kameras füllen
            cameraSelect.innerHTML = '';
            videoDevices.forEach((device, index) => {
                const option = document.createElement('option');
                option.value = device.deviceId;
                option.textContent = device.label || `Camera ${index + 1}`;
                cameraSelect.appendChild(option);
            });

            if (videoDevices.length > 0) {
                selectedDeviceId = videoDevices[0].deviceId; // Erste Kamera auswählen
            } else {
                alert('Keine Kameras gefunden.');
            }
        } catch (error) {
            console.error('Fehler beim Abrufen der Kameras:', error);
        }
    }

    // Ändern des ausgewählten Geräts
    cameraSelect.addEventListener('change', (event) => {
        selectedDeviceId = event.target.value;
    });

    // Funktion zur Aktivierung der ausgewählten Kamera
    async function activateWebcam() {
        if (!selectedDeviceId) {
            alert('Keine Kamera ausgewählt.');
            return;
        }

        try {
            const stream = await navigator.mediaDevices.getUserMedia({
                video: {
                    deviceId: { exact: selectedDeviceId },
                    width: { exact: 320 },
                    height: { exact: 240 },
                    frameRate: { max: 10 }
                },
                audio: false
            });

            // Stream auf das Videoelement legen
            videoElement.srcObject = stream;
            videoElement.play();
            videoElement.style.display = 'block';
        } catch (error) {
            console.error('Konnte die Webcam nicht aktivieren:', error);
            alert('Fehler beim Zugriff auf die ausgewählte Kamera.');
        }
    }

    // Klick-Event für den Aktivierungsbutton
    activateButton.addEventListener('click', activateWebcam);

    // Kameras bei Start laden
    getCameras();
})();