(Instagram)Enable standard video controls

enable standard video controls

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                (Instagram)Enable standard video controls
// @namespace           https://greasyfork.org/users/821661
// @match               https://www.instagram.com/*
// @grant               GM_getValue
// @grant               GM_setValue
// @run-at              document-start
// @version             1.3
// @author              hdyzen
// @description         enable standard video controls
// @license             GPL-3.0
// ==/UserScript==
'use strict';

const getVolume = () => GM_getValue('volume', 0);

const cantAddControls = () => !(window.location.pathname.startsWith('/stories/') || window.location.pathname.startsWith('/reels/'));

const videosHandler = videosEl => {
    for (const video of videosEl) {
        const videoNextSibling = video.nextElementSibling;
        const poster = videoNextSibling.querySelector('img[src]');
        const mButton = videoNextSibling.querySelector('button:has([d^="M1.5 13."])');

        video.setAttribute('controls', '');
        mButton?.click();
        video.volume = getVolume();

        video.addEventListener('volumechange', e => {
            GM_setValue('volume', video.volume);
        });

        if (poster) video.setAttribute('poster', poster.src);
        videoNextSibling.style.display = 'none';
    }
};

const mutationsHandler = mutations => {
    const videosEl = document.querySelectorAll('video[src]:not([controls])');
    if (videosEl.length && cantAddControls()) {
        videosHandler(videosEl);
    }
};

const observer = new MutationObserver(mutationsHandler);

observer.observe(document.documentElement, { childList: true, subtree: true });