Disable autoplay

Block autoplay and require direct user interaction to play media

目前為 2024-08-26 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Disable autoplay
// @namespace    https://www.androidacy.com/
// @version      1.4
// @description  Block autoplay and require direct user interaction to play media
// @author       Androidacy
// @include      *
// @exclude      https://www.google.com/adsense/new/*
// @icon         https://www.androidacy.com/wp-content/uploads/cropped-cropped-cropped-cropped-New-Project-32-69C2A87-1-192x192.jpg
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // Function to override the play method to require direct interaction with the media element or its parent
    const overridePlayMethod = (media) => {
        const originalPlay = media.play;
        media.play = function() {
            if (media.hasUserInteracted) {
                return originalPlay.apply(media, arguments);
            } else {
                console.log('Autoplay prevented: Direct interaction with the media element or its parent is required to start playback.');
                return Promise.reject(new Error('Playback requires direct interaction with the media element or its parent.'));
            }
        };
    };

    // Function to pause all video and audio elements and disable autoplay
    const disableAutoplay = () => {
        const mediaElements = document.querySelectorAll('video, audio');
        mediaElements.forEach((media) => {
            media.autoplay = false;
            media.pause();
            media.removeAttribute('autoplay');
            overridePlayMethod(media);

            // Mark media as interacted with when it or its parent (or any children of the parent) is clicked
            const parent = media.parentElement;
            const clickHandler = (event) => {
                if (event.isTrusted) {
                    media.hasUserInteracted = true;
                }
            };

            media.addEventListener('click', clickHandler);
            if (parent) {
                parent.addEventListener('click', clickHandler);
            }
            parent.querySelectorAll('*').forEach(child => {
                child.addEventListener('click', clickHandler);
            });

            // Listen for the onplay event and pause if not interacted
            media.addEventListener('play', () => {
                if (!media.hasUserInteracted) {
                    media.pause();
                    console.log('Autoplay prevented: Media paused because no user interaction occurred.');
                }
            });
        });
    };

    // Initial run
    disableAutoplay();

    // Observe for dynamically added media elements
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.addedNodes.length > 0) {
                disableAutoplay();
            }
        });
    });

    // Observe the entire document
    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

})();