Add Volume Control Slider

6/10/2024

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Add Volume Control Slider
// @namespace   Violentmonkey Scripts
// @match       https://www.redgifs.com/watch/*
// @grant       none
// @version     1.0
// @author      LovingObserver
// @license     GNU GPLv3
// @icon        https://icons.duckduckgo.com/ip2/redgifs.com.ico
// @description 6/10/2024
// ==/UserScript==

//create a MutationObserver instance to detect the video, since they are added dynamically
let observer = new MutationObserver((mutations, observer) => {
  //look through all mutations that just occured
  for(let mutation of mutations) {
    //if the addedNodes property has one or more nodes
    if(mutation.addedNodes.length) {
      let element = document.querySelector('.GifPreview_isActive');
      if(element) {
        //get the video element
        let video = document.querySelector('video');

        //set the video volume to the value stored in localStorage
        if (localStorage.getItem("videoVolume")) {
          video.volume = localStorage.getItem("videoVolume");
        } else {
          video.volume = 0.75;
        }

        //create a volume control slider
        let volumeControl = document.createElement('input');
        volumeControl.type = 'range';
        volumeControl.min = 0;
        volumeControl.max = 1;
        volumeControl.step = 0.01;
        volumeControl.value = video.volume;
        volumeControl.style.position = "absolute";
        volumeControl.style.top = "6px";
        volumeControl.style.right = "7.5px";
        volumeControl.style.zIndex = 3;
        volumeControl.style.cursor = "pointer";

        //update the video volume when the slider value changes
        volumeControl.addEventListener('input', (event) => {
          video.volume = volumeControl.value;
          localStorage.setItem("videoVolume", volumeControl.value);
        });

        //add the volume control to the page
        element.appendChild(volumeControl);

        //after the video element is found, stop observing
        observer.disconnect();
        return;
      }
    }
  }
});

//start observing the document with the configured parameters
observer.observe(document, { childList: true, subtree: true });