🏠 Home 

Set HTML5 media player volume

Script to set the volume of <video> and <audio> elements to reduced value (defaults to 50%), with a menu item to change for the current page.


Installer dette script?
  1. // ==UserScript==
  2. // @name Set HTML5 media player volume
  3. // @description Script to set the volume of <video> and <audio> elements to reduced value (defaults to 50%), with a menu item to change for the current page.
  4. // @namespace JeffersonScher
  5. // @author Jefferson "jscher2000" Scher
  6. // @copyright Copyright 2017 Jefferson Scher
  7. // @license BSD-3-clause
  8. // @include *
  9. // @version 0.6
  10. // @grant GM_registerMenuCommand
  11. // ==/UserScript==
  12. var setvol_volumepct = 0.5; // Set volume to 50%
  13. // == == == Detect added nodes / attach MutationObserver == == ==
  14. if (document.body){
  15. // Check existing videos
  16. setvol_checkNode(document.body);
  17. // Watch for changes that could be new videos
  18. var setvol_MutOb = (window.MutationObserver) ? window.MutationObserver : window.WebKitMutationObserver;
  19. if (setvol_MutOb){
  20. var setvol_chgMon = new setvol_MutOb(function(mutationSet){
  21. mutationSet.forEach(function(mutation){
  22. for (var setvol_node_count=0; setvol_node_count<mutation.addedNodes.length; setvol_node_count++){
  23. if (mutation.addedNodes[setvol_node_count].nodeType == 1){
  24. setvol_checkNode(mutation.addedNodes[setvol_node_count]);
  25. }
  26. }
  27. });
  28. });
  29. // attach setvol_chgMon to document.body
  30. var setvol_opts = {childList: true, subtree: true};
  31. setvol_chgMon.observe(document.body, setvol_opts);
  32. }
  33. }
  34. function setvol_checkNode(el){
  35. if (el.nodeName == "video" || el.nodeName == "audio") var vids = [el];
  36. else var vids = el.querySelectorAll('video, audio');
  37. if (vids.length > 0){
  38. for (var j=0; j<vids.length; j++){
  39. vids[j].volume = setvol_volumepct;
  40. }
  41. }
  42. }
  43. // This is not compatible with Greasemonkey 4, but should work in Tampermonkey and Violentmonkey
  44. function chgVol(e){
  45. var newvol = prompt('Enter value between 0.0 and 1.0 for 0% to 100%', setvol_volumepct);
  46. if (!isNaN(parseFloat(newvol))){
  47. var newnum = parseFloat(newvol);
  48. if (newnum < 0) newnum = 0;
  49. if (newnum > 1) newnum = 1;
  50. setvol_volumepct = newnum;
  51. setvol_checkNode(document.body);
  52. }
  53. }
  54. GM_registerMenuCommand('Change volume for this page', chgVol);