Auto Loop and Autoplay for All Video Players

Enables loop and autoplay on all video players online

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Auto Loop and Autoplay for All Video Players
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Enables loop and autoplay on all video players online
// @author       Dj Dragkan
// @match        *://*/*
// @icon         https://upload.wikimedia.org/wikipedia/commons/4/42/YouTube_icon_%282013-2017%29.png
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function enableLoopAndAutoplay(video) {
        if (video && !video.dataset.loopEnabled) {
            // Enable loop
            video.loop = true;
            video.dataset.loopEnabled = "true";
            console.log("Loop enabled on:", video);
        }

        // Enable autoplay after the first playback finishes
        video.addEventListener('ended', () => {
            video.autoplay = true;
            video.play();
            console.log("Autoplay enabled on:", video);
        });
    }

    function checkVideos() {
        let videos = document.querySelectorAll("video");
        videos.forEach(enableLoopAndAutoplay);
    }

    // Observer to detect new videos on the page
    const observer = new MutationObserver(checkVideos);
    observer.observe(document.body, { childList: true, subtree: true });

    // Enable loop and autoplay on videos already present
    checkVideos();
})();