Fourth Webcam Viewer

Aktiviert die vierte angeschlossene Webcam und öffnet ein eigenes Browserfenster, das das Bild der Kamera anzeigt.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Fourth Webcam Viewer
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Aktiviert die vierte angeschlossene Webcam und öffnet ein eigenes Browserfenster, das das Bild der Kamera anzeigt.
// @author       YourName
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let selectedDeviceId = null;

    // Funktion zur Ermittlung der Kameras
    async function getCameras() {
        try {
            const devices = await navigator.mediaDevices.enumerateDevices();
            const videoDevices = devices.filter(device => device.kind === 'videoinput');

            if (videoDevices.length > 3) {
                selectedDeviceId = videoDevices[3].deviceId; // Vierte Kamera auswählen
                activateWebcam();
            } else {
                alert('Nicht genug Kameras angeschlossen.');
            }
        } catch (error) {
            console.error('Fehler beim Abrufen der Kameras:', error);
        }
    }

    // Webcam aktivieren und in eigenem Fenster anzeigen
    async function activateWebcam() {
        if (!selectedDeviceId) {
            console.log('Keine vierte Kamera verfügbar.');
            return;
        }

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

            // Neues Fenster öffnen und Stream anzeigen
            const cameraWindow = window.open('', '_blank', 'width=340,height=260');
            if (cameraWindow) {
                const cameraVideo = cameraWindow.document.createElement('video');
                cameraVideo.style.width = '320px';
                cameraVideo.style.height = '240px';
                cameraVideo.autoplay = true;
                cameraVideo.srcObject = stream;
                cameraWindow.document.body.appendChild(cameraVideo);

                cameraWindow.onbeforeunload = () => {
                    stream.getTracks().forEach(track => track.stop());
                };
            } else {
                alert('Pop-up Blocker deaktivieren, um das Kamerafenster zu öffnen.');
            }
        } catch (error) {
            console.error('Konnte die Webcam nicht aktivieren:', error);
        }
    }

    // Kameras ermitteln und aktivieren
    getCameras();
})();