User:EstherLoer/vector-2022.js

// Playback keyboard controls script // (currently only works on dedicated media pages)

// Configuration jump_distance_forward = 10    // Jump this many seconds forward with right arrow key. jump_distance_backward = 5    // Jump this many seconds backward with left arrow key. speed_step   = 1/4   // Speed incresaed or decreased by this value. volume_step  = 1/10  // Volume increased or decreased by this value.

var mediaType; // for compatibility

function checkMediaType { // checks whether it is a video or audio tag mediaTypeBeforeCheck = mediaType; if (document.getElementsByTagName("video")[0]) mediaType = "video"; if (document.getElementsByTagName("audio")[0]) mediaType = "audio"; mediaTypeAfterCheck = mediaType; if (mediaTypeBeforeCheck != mediaTypeAfterCheck) // Only show media type in console if it has changed. console.log("Detected media type: " + mediaType); }

// Additional checks to ensure the player is detected window.onclick = function { checkMediaType; }; window.addEventListener("keydown", function { checkMediaType; } );

// shortcuts for current time and duration function getCurrentTime { return Math.round( document.getElementsByTagName(mediaType)[0].currentTime ); } function getDuration   { return Math.round( document.getElementsByTagName(mediaType)[0].duration ); }

window.onkeydown = function(e) { var key = e.keyCode ? e.keyCode : e.which;

// Keys 0 to 9 of main row and NUM pad, and also Home and End. if (key == 48 || key == 96 || key == 36) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.0 ; console.log("Position: 0% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 49 || key == 97) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.1 ; console.log("Position: 10% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 50 || key == 98) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.2 ; console.log("Position: 20% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 51 || key == 99) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.3 ; console.log("Position: 30% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 52 || key == 100) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.4 ; console.log("Position: 40% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 53 || key == 101) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.5 ; console.log("Position: 50% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 54 || key == 102) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.6 ; console.log("Position: 60% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 55 || key == 103) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.7 ; console.log("Position: 70% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 56 || key == 104) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.8 ; console.log("Position: 80% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 57 || key == 105) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*0.9 ; console.log("Position: 90% Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 35) { document.getElementsByTagName(mediaType)[0].currentTime=document.getElementsByTagName(mediaType)[0].duration*1.0 ; console.log("Position: 100% Time: " + getCurrentTime + " / " + getDuration + " s" ); }

// Time skipping with left and right arrow keys. if (key == 37) { document.getElementsByTagName(mediaType)[0].currentTime-=jump_distance_backward ; console.log("Time: " + getCurrentTime + " / " + getDuration + " s" ); }  if (key == 39) { document.getElementsByTagName(mediaType)[0].currentTime+=jump_distance_forward ; console.log("Time: " + getCurrentTime + " / " + getDuration + " s" ); }

// Speed control with up and down arrow keys. if (key == 38) { document.getElementsByTagName(mediaType)[0].playbackRate+=speed_step ; console.log("Speed: " + document.getElementsByTagName(mediaType)[0].playbackRate); }  if (key == 40 && document.getElementsByTagName(mediaType)[0].playbackRate >= speed_step) { // speed below 0 causes DOMException: Operation is not supported document.getElementsByTagName(mediaType)[0].playbackRate-=speed_step ; console.log("Speed: " + document.getElementsByTagName(mediaType)[0].playbackRate); }

// Volume control (+ -) if (key == 171 || key == 107) { // set volume to 100% if closer to it than the value in volume_step to prevent errors if (document.getElementsByTagName(mediaType)[0].volume >= (1-volume_step) ) { document.getElementsByTagName(mediaType)[0].volume = 1; } else { // increase volume by the value in volume_step document.getElementsByTagName(mediaType)[0].volume+=volume_step ; }      console.log("Volume: " + Math.round(document.getElementsByTagName(mediaType)[0].volume*100 ) + " %"); }  if (key == 173 || key == 109) { // set volume to 0% if below volume_step if (document.getElementsByTagName(mediaType)[0].volume <= volume_step) { document.getElementsByTagName(mediaType)[0].volume = 0; } else { // decrease volume by the value in volume_step document.getElementsByTagName(mediaType)[0].volume-=volume_step ; }      console.log("Volume: " + Math.round(document.getElementsByTagName(mediaType)[0].volume*100 ) + " %"); }

};