Greasy Fork is available in English.

Universal Video Recorder

Tracks videos > 5 mins with a dedicated red overlay. Pulses on play, downloads on pause.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Universal Video Recorder
// @namespace    universal_video_recorder
// @version      1.0
// @description  Tracks videos > 5 mins with a dedicated red overlay. Pulses on play, downloads on pause.
// @author       dev.ansung
// @match        *://*/*
// @match        *://*.youtube.com/*
// @match        *://*.vimeo.com/*
// @match        *://*.dailymotion.com/*
// @match        *://*.twitch.tv/*
// @match        *://*.facebook.com/watch*
// @match        *://*.facebook.com/*/videos/*
// @match        *://*.twitter.com/*
// @match        *://*.x.com/*
// @match        *://*.streamable.com/*
// @match        *://*.rumble.com/*
// @exclude      *://*.bilibili.com/*
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const MIN_DURATION_SEC = 300; // 5 minutes

    class VideoRecorder {
        #video;
        #overlay;
        #recorder = null;
        #chunks = [];
        #animationFrameId = null;

        constructor(videoElement) {
            this.#video = videoElement;
            this.#video.dataset.recorderAttached = "true";

            this.#initOverlay();
            this.#bindEvents();
            this.#syncOverlay();
        }

        static injectStyles() {
            if (document.getElementById('univ-video-styles')) return;

            const style = document.createElement('style');
            style.id = 'univ-video-styles';
            style.textContent = `
                .univ-video-overlay {
                    position: fixed;
                    pointer-events: none;
                    z-index: 2147483647;
                    outline: 4px solid red;
                    outline-offset: -4px;
                }
                .univ-video-overlay.univ-pulse-active {
                    animation: univ-pulse-anim 0.8s infinite alternate;
                }
                @keyframes univ-pulse-anim {
                    from {
                        outline-color: rgba(255, 0, 0, 1);
                        box-shadow: inset 0 0 40px rgba(255, 0, 0, 0.6);
                    }
                    to {
                        outline-color: rgba(255, 0, 0, 0.2);
                        box-shadow: inset 0 0 5px rgba(255, 0, 0, 0.1);
                    }
                }
            `;
            document.head.append(style);
        }

        #initOverlay() {
            this.#overlay = document.createElement('div');
            this.#overlay.className = 'univ-video-overlay';
            document.body.appendChild(this.#overlay);
        }

        // Arrow function preserves 'this' context for requestAnimationFrame
        #syncOverlay = () => {
            // Cleanup if the video is removed from the DOM
            if (!document.body.contains(this.#video)) {
                this.#overlay.remove();
                cancelAnimationFrame(this.#animationFrameId);
                return;
            }

            const rect = this.#video.getBoundingClientRect();

            // Only render if video is visible
            if (rect.width > 0 && rect.height > 0) {
                this.#overlay.style.display = 'block';
                this.#overlay.style.top = `${rect.top}px`;
                this.#overlay.style.left = `${rect.left}px`;
                this.#overlay.style.width = `${rect.width}px`;
                this.#overlay.style.height = `${rect.height}px`;
            } else {
                this.#overlay.style.display = 'none';
            }

            this.#animationFrameId = requestAnimationFrame(this.#syncOverlay);
        }

        #bindEvents() {
            this.#video.addEventListener('play', this.#startRecording);
            this.#video.addEventListener('pause', this.#stopRecording);
        }

        #startRecording = () => {
            try {
                const stream = this.#video.captureStream();
                const mimeTypes = ['video/webm;codecs=vp9', 'video/webm', 'video/mp4'];
                const mime = mimeTypes.find(t => MediaRecorder.isTypeSupported(t)) || '';

                this.#recorder = new MediaRecorder(stream, { mimeType: mime });
                this.#chunks = [];

                this.#recorder.ondataavailable = (e) => {
                    if (e.data && e.data.size > 0) this.#chunks.push(e.data);
                };

                this.#recorder.onstop = this.#downloadRecording;

                this.#recorder.start();
                this.#overlay.classList.add('univ-pulse-active');
                console.log("VideoRecorder: Recording started.");
            } catch (err) {
                console.error("VideoRecorder: Failed to start recording. (Possible CORS issue)", err);
            }
        }

        #stopRecording = () => {
            this.#overlay.classList.remove('univ-pulse-active');

            if (this.#recorder && this.#recorder.state === 'recording') {
                this.#recorder.stop();
                console.log("VideoRecorder: Recording stopped. Initiating download...");
            }
        }

        #downloadRecording = () => {
            if (this.#chunks.length === 0) return;

            const blob = new Blob(this.#chunks, { type: this.#recorder.mimeType });
            const url = URL.createObjectURL(blob);

            const a = document.createElement('a');
            a.href = url;
            a.download = `video_capture_${Date.now()}.mp4`;
            a.click();

            setTimeout(() => URL.revokeObjectURL(url), 1000);
        }
    }

    // --- Main Scanner ---
    function scanForVideos() {
        document.querySelectorAll('video').forEach(video => {
            if (!video.dataset.recorderAttached && video.duration > MIN_DURATION_SEC) {
                new VideoRecorder(video);
            }
        });
    }

    // Initialize
    VideoRecorder.injectStyles();
    setInterval(scanForVideos, 2000);
    scanForVideos();

})();