Skip to content

Instantly share code, notes, and snippets.

@beminee
Last active June 27, 2017 03:54
Show Gist options
  • Save beminee/7181e5313285305b881500bda7d4eb8a to your computer and use it in GitHub Desktop.
Save beminee/7181e5313285305b881500bda7d4eb8a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Volume to speed
// @author Satan
// @namespace youtube.com
// @description YouTube videos change playback speed based on the current volume. Fun.
// @include https://www.youtube.com/watch?v=*
// @version 1.0
// @grant none
// ==/UserScript==
'use strict'
function getVolume() {
let slider = document.querySelector('.ytp-volume-panel')
let val = parseInt(slider.getAttribute('aria-valuenow'))
let isMuted = slider.getAttribute('aria-valuetext').match(/muted/) != null
return isMuted ? 0 : val
}
function setSpeed(speed) {
let video = document.querySelector('.video-stream')
video.playbackRate = speed
}
// observe the volume slider
let lastVolume = getVolume()
setSpeed((100 - lastVolume) / 25)
let observer = new MutationObserver(mutations => {
let volume = getVolume()
if (volume == lastVolume)
return
else
lastVolume = volume
// console.log('Volume -> Speed userscript: volume changed to ' + volume + '%')
setSpeed((100 - volume) / 25)
})
observer.observe(document.querySelector('.ytp-volume-panel'), {
attributes: true,
childList: false,
characterData: false,
})
// console.log('Volume -> Speed userscript: observing volume')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment