YouTube - Always Theater Mode

Set the default viewing mode to Theater Mode.

当前为 2015-07-24 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        YouTube - Always Theater Mode
// @namespace   r-a-y/youtube/theater
// @description Set the default viewing mode to Theater Mode.
// @include     http://www.youtube.com/watch*
// @include     https://www.youtube.com/watch*
// @version     1.3.0
// @run-at      document-start
// @grant       none
// @license     GPL v3
// ==/UserScript==

// Ensure unsafeWindow object is available both in firefox and chrome
//
// Props Anon via YouTube Cinema Mode userscript
// https://greasyfork.org/en/scripts/10417-youtube-cinema-mode
function installUnsafewindowPolyfill()
{
	if (typeof unsafeWindow === 'undefined')
	{
		if (typeof XPCNativeWrapper === 'function' && typeof XPCNativeWrapper.unwrap === 'function')
			unsafeWindow = XPCNativeWrapper.unwrap(window);
		else if (window.wrappedJSObject)
			unsafeWindow = window.wrappedJSObject;
	}
}

// Disable SPF (Structured Page Fragments), which prevents properly attaching to page load events when navigation occurs 
// Will also disable the red loading bar.
//
// Props Anon via YouTube Cinema Mode userscript
// https://greasyfork.org/en/scripts/10417-youtube-cinema-mode
function disableSPF()
{
	if (unsafeWindow._spf_state && unsafeWindow._spf_state.config)
	{
		unsafeWindow._spf_state.config['navigate-limit'] = 0;
		unsafeWindow._spf_state.config['navigate-part-received-callback'] = function (targetUrl) { location.href = targetUrl; }
	}
	
	setTimeout(disableSPF, 50);
}

// Easy way to fire JS events.
//
// Props Kooilnc from Stackoverflow
// http://stackoverflow.com/a/2706236
function eventFire(el, etype){
	if (el.fireEvent) {
		(el.fireEvent('on' + etype));
	} else {
		var evObj = document.createEvent('Events');
		evObj.initEvent(etype, true, false);
		el.dispatchEvent(evObj);
	}
}

// flash + html5 requires manually resizing the stage
function resizeStage() {
	document.getElementById('page').className = "watch watch-stage-mode";
	document.getElementById('player').className = "content-alignment watch-medium";
	document.getElementById('watch7-container').className = "watch-wide";

	// handle playlists
	var playlist = document.getElementById('watch-appbar-playlist');
	if (playlist) {
		playlist.setAttribute('style', 'top:520px');
	}
}

// html5 requires other tomfoolery
function html5() {
	var video = document.querySelector('video');
	if (video) {
		document.querySelector('.html5-video-content').setAttribute('style', 'width:100% !important; height:100% !important;');
		document.querySelector('.html5-video-container').setAttribute('style', 'position:static');

		var button = document.querySelector('.ytp-size-toggle-large');
		// new html5 player
		if ( ! button ) {
			button = document.querySelector('.ytp-size-button');
		}

		// emulate clicking on the "Theater Mode" button for HTML5 progressbar to expand
		eventFire( button, 'click' );
	}
}

/** TIME FOR SOME ACTION! **/

// get rid of SPF
installUnsafewindowPolyfill();
disableSPF();

// resize after document is loaded
window.addEventListener ("load", function() {
    resizeStage();
    html5();
});