Youtube Space=Pause

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

目前為 2015-03-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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     1
// @grant       none
// ==/UserScript==

/* - - - - - - - - - *\ 
 * Utility Functions *
\* - - - - - - - - - */
function contentEval(source) {
  // 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);
}


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

// Start on load
window.onload = main();