Created
June 20, 2018 21:48
-
-
Save ChadBailey/06fef8bf9c75d304d2a77abdfa35eb15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//YouTube Enhanced | |
//Author Chad Bailey www.chadbailey.me | |
//Written 10-6-17 | |
//Initialize global variables | |
var video_element | |
var current_speed | |
//Amount to adjust video playback | |
var speed_step_fine = 0.5 | |
//Maximim amount to slow down video playback | |
var min_speed_fine = 0.1 | |
var min_speed_coarse = 0 | |
//Maximim amount to speed up video playback | |
var max_speed_fine = 4.0 | |
var max_speed_coarse = 128 | |
//Hotkey setup | |
var hotkey_decrease_fine = '<' | |
var hotkey_increase_fine = '>' | |
var hotkey_decrease_coarse = '[' | |
var hotkey_increase_coarse = ']' | |
function initialize() { | |
//Store the object to a variable for easier/cleaner code | |
video_element = document.getElementsByTagName("video")[0] | |
//Get current playback rate and store to a variable | |
current_speed = video_element.playbackRate | |
//Hotkey events | |
$(document).keydown(function(k) { | |
if (k.key == hotkey_decrease_fine) { | |
decrease_speed_fine() | |
} | |
if (k.key == hotkey_increase_fine) { | |
increase_speed_fine() | |
} | |
if (k.key == hotkey_decrease_coarse) { | |
decrease_speed_coarse() | |
} | |
if (k.key == hotkey_increase_coarse) { | |
increase_speed_coarse() | |
} | |
}); | |
} | |
function adjust_playback_rate(element,rate) { | |
element.playbackRate = rate | |
console.log("current speed: " + current_speed + " new speed: " + video_element.playbackRate) | |
current_speed = rate | |
} | |
function increase_speed_fine() { | |
//Only increase speed if we don't go above max_speed | |
if (current_speed + speed_step_fine <= max_speed_fine) { | |
//Adjust the playback rate to equal the sum of current_speed + speed_step | |
adjust_playback_rate(video_element,current_speed + speed_step_fine) | |
} | |
//If we are hitting our maximum, set our playback to the maximum speed | |
else { | |
adjust_playback_rate(video_element,max_speed_fine) | |
} | |
} | |
function decrease_speed_fine() { | |
//Only decrease speed if we don't go below min_speed | |
if (current_speed - speed_step_fine >= min_speed_fine) { | |
//Adjust the playback rate to equal the sum of current_speed + speed_step | |
adjust_playback_rate(video_element,current_speed - speed_step_fine) | |
} | |
//If we are hitting our maximum, set our playback to the maximum speed | |
else { | |
adjust_playback_rate(video_element,min_speed_fine) | |
} | |
} | |
function increase_speed_coarse() { | |
if (current_speed <= 0.5) { | |
adjust_playback_rate(video_element,1) | |
} | |
//Only increase speed if we don't go above max_speed | |
else if (current_speed *2 <= max_speed_coarse) { | |
//Adjust the playback rate to equal the sum of current_speed + speed_step | |
adjust_playback_rate(video_element,current_speed *2) | |
} | |
//If we are hitting our maximum, set our playback to the maximum speed | |
else { | |
adjust_playback_rate(video_element,max_speed_coarse) | |
} | |
} | |
function decrease_speed_coarse() { | |
//Only decrease speed if we don't go below min_speed | |
if (current_speed <= min_speed_coarse + 1) { | |
adjust_playback_rate(video_element,min_speed_coarse) | |
} | |
else { | |
//Adjust the playback rate to equal the sum of current_speed + speed_step | |
adjust_playback_rate(video_element,current_speed /2) | |
} | |
} | |
//Timer to add hotkeys once jquery is available | |
//Note: This is necessary due to the cjs extension | |
//loading this script and jquery asymmetrically | |
var checkforjq = setInterval(function(){ | |
if ($ && document.getElementsByTagName("video")[0].playbackRate) { | |
initialize() | |
clearInterval(checkforjq); // clear interval | |
} | |
},100); // check every 100ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment