Youtube Space=Pause

Pressing space when watching a video on Youtube will always pause the video instead of functioning like Page Down key.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        Youtube Space=Pause
// @namespace   s4nji
// @author      s4nji
// @description Pressing space when watching a video on Youtube will always pause the video instead of functioning like Page Down key.
// @license     CC0
// @include     https://www.youtube.com/watch*
// @version     2
// @grant       none
// ==/UserScript==


/* - - - - - *\
 * Variables *
\* - - - - - */
var inputFocus = false;
var debug = false;


/* - - - - - - - - - *\ 
 * Utility Functions *
\* - - - - - - - - - */
// Debug Console Logging
function clog(msg) {
	if (debug) {
		console.log("YtS=P | "+msg);
	}
}

// Run codes "unsafely"
function contentEval(source) {
	"use strict";
	
	// Check for function input.
	if ('function' === typeof source) {
		// Execute this function with no arguments, by adding parentheses.
		// One set around the function, required for valid syntax, and a
		// second empty set calls the surrounded function.
		source = '(' + source + ')();';
	}

	// Create a script node holding this  source code.

	var script = document.createElement('script');
	script.setAttribute("type", "application/javascript");
	script.textContent = source;

	// Insert the script node into the page, so it will run, and immediately
	// remove it to clean up.
	document.body.appendChild(script);
	document.body.removeChild(script);
}

// Set inputFocus to true if an input box is on focus
function setInputFocus(bool) {
	"use strict";
	
	return function() {
		if ( typeof bool === "boolean" ) {
			inputFocus = bool;
			clog("inputFocus = " + inputFocus);
		}
	};
}

// Add Event Listeners
function hookInputFocus() {
	"use strict";
	
	var inputs = document.querySelectorAll("input"), i;
	for (i=0; i<inputs.length; i+=1) {
		inputs[i].addEventListener('focus', setInputFocus(true));
		inputs[i].addEventListener('blur', setInputFocus(false));
		clog("hooked "+i+"!");
	}
}

/* - - - - - - - *\ 
 * Main Function *
\* - - - - - - - */
function main() {
	"use strict";
	
	document.body.addEventListener('keydown', function(event) {
		clog("inputFocus == " + inputFocus);
		
		if (event.keyCode === 32 && !inputFocus) {
			event.preventDefault();
			
			var status = document.querySelector("#movie_player").getPlayerState();
			if ( status === 1 || status === 3 ) {
				contentEval('document.querySelector("#movie_player").pauseVideo();');
			} else if ( status === 2 || status === 0 ) {
				contentEval('document.querySelector("#movie_player").playVideo();');
			}
			
			// N/A (-4), unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5). 
			
		}
	});
	
	hookInputFocus();
}

// Start on load
document.addEventListener('DOMContentLoaded', main() );