Disable autoplay

Block autoplay and require direct user interaction to play media

当前为 2024-08-26 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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,
    });

})();