User:EstherLoer/media keyboard controls.js

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

var mediaType; // for compatibility

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

// Since the video or audio element only loads when starting playback on the legacy player (.mwPlayerContainer .k-player), the type needs to be checked on each click. document.getElementById("content").onclick = function { checkMediaType; };

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

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

// Time jumping (left/right) if (key == 37) { document.getElementsByTagName(mediaType)[0].currentTime-=10 ; console.log("Time: " + document.getElementsByTagName(mediaType)[0].currentTime); }  if (key == 39) { document.getElementsByTagName(mediaType)[0].currentTime+=10 ; console.log("Time: " + document.getElementsByTagName(mediaType)[0].currentTime); }

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

// Volume control (+/-) if (key == 171 || key == 107) { // set volume to 100% if 90% or above if (document.getElementsByTagName(mediaType)[0].volume >= 0.9) { document.getElementsByTagName(mediaType)[0].volume = 1; } else { // increase volume by 10 % document.getElementsByTagName(mediaType)[0].volume+=0.1 ; }      console.log("Volume: " + document.getElementsByTagName(mediaType)[0].volume*100 + " %"); }  if (key == 173 || key == 109) { // set volume to 0% if 10% or below if (document.getElementsByTagName(mediaType)[0].volume <= 0.1) { document.getElementsByTagName(mediaType)[0].volume = 0; } else { // decrease volume by 10 % document.getElementsByTagName(mediaType)[0].volume-=0.1 ; }      console.log("Volume: " + document.getElementsByTagName(mediaType)[0].volume*100 + " %"); }

};