Skip to content

Instantly share code, notes, and snippets.

@Midtan
Last active September 18, 2023 14:33
Show Gist options
  • Save Midtan/0fed2d2f0d28ca59df41626026189eea to your computer and use it in GitHub Desktop.
Save Midtan/0fed2d2f0d28ca59df41626026189eea to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Set Default Video Volume
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Set default volume for raw HTML5 video files
// @author Me
// @match *://*/*
// @grant none
// @downloadURL https://gist.github.com/Midtan/0fed2d2f0d28ca59df41626026189eea/raw/default-html5-video-volume.user.js
// ==/UserScript==
(function() {
'use strict';
// Set the default volume here (0.5 = 50% volume)
const defaultVolume = 0.15;
// Check if the current page is a raw video file
function isRawVideoFile() {
const videoTags = document.getElementsByTagName('video');
// If there's only one <video> tag and it's a direct child of the <body>, it's likely a raw video file
if (videoTags.length === 1 && videoTags[0].parentNode === document.body) {
return true;
}
return false;
}
// Set the default volume for the video player
function setVideoVolume() {
const videoTags = document.getElementsByTagName('video');
if (videoTags.length > 0) {
videoTags[0].volume = defaultVolume;
}
}
// If it's a raw video file, set the default volume
if (isRawVideoFile()) {
// Use 'loadedmetadata' event to set the volume after the video metadata is loaded
document.querySelector('video').addEventListener('loadedmetadata', setVideoVolume);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment