Coursera EXT - Play/Pause with [space bar] or click

Coursera Extension -- Enables space bar to play/pause the video. Based on http://userscripts.org/scripts/show/139512 by loopkid

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Coursera EXT - Play/Pause with [space bar] or click
// @description     Coursera Extension -- Enables space bar to play/pause the video. Based on http://userscripts.org/scripts/show/139512 by loopkid
// @namespace       http://sepczuk.com/
// @version         0.03
// @include         https://*.coursera.org/*/lecture/view*
// @include         https://*.coursera.org/*/lecture/preview_view*
// @match           https://*.coursera.org/*/lecture/view*
// @match           https://*.coursera.org/*/lecture/preview_view*
// @copyright       2012-2013, Damian Sepczuk, damian at sepczuk period delme com; loopkid
// ==/UserScript==

function contentEval(source) {
	if ('function' == typeof source) {
		source = '(' + source + ')();';
	}
	var script = document.createElement('script');
	script.setAttribute("type", "application/javascript");
	script.textContent = source;
	document.body.appendChild(script);
	document.body.removeChild(script);
}


function fixVideoPlayerShortcuts() {

	// Check every 300ms if video player has finished loading
	var qlchecker = window.setInterval(function () { checkForQL_player(); }, 300);

	function checkForQL_player() {
		if (typeof QL_player == "undefined") return;

        // Disable checking of video player load status
        window.clearInterval(qlchecker);
		var playOrPause = function(player, media) {
            if (media.paused || media.ended) {
                media.play();
            } else {
                media.pause();
            };
        };
        SPACEBAR_KEY = 32;

        QL_player.mediaelement_handle.options.keyActions.push({keys: [SPACEBAR_KEY], action: playOrPause});
        QL_player.mediaelement_media.addEventListener('click', function(){playOrPause(undefined, QL_player.mediaelement_media)});
	}
}

contentEval(fixVideoPlayerShortcuts);