Video Player Toothbrush

牙刷科技! 让所有视频播放器网页全屏!默认快捷键ALT+1

Fra 11.07.2014. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Video Player Toothbrush
// @namespace   http://www.icycat.com
// @description 牙刷科技! 让所有视频播放器网页全屏!默认快捷键ALT+1
// @include     *www.bilibili.com/video/av*
// @include     *www.iqiyi.com/*
// @include     *v.youku.com/*
// @include     *www.youtube.com/*
// @include     *v.17173.com/*
// @include     *www.tudou.com/*
// @include     *.letv.com/*
// @include     *v.pptv.com/*
// @include     *tv.sohu.com/*
// @include     *v.ku6.com/*
// @include     *vod.kankan.com/*
// @include     *v.qq.com/*
// @include     *www.56.com/*
// @include     *live.yy.com/*
// @include     *www.acfun.com/v/ac*
// @include     *donghua.dmzj.com/*
// @include     *video.sina.com.cn/*
// @include     *.cntv.cn/*
// @include     *.douyutv.com/*
// @include     *music.163.com/*
// @version     2.3
// @grant       none
// @run-at      document-start
// ==/UserScript==

//若有需要可以自行添加域名,按照include的格式添加即可。
//自行修改快捷键,请参考下面代码中的注释。注意焦点在flash上时快捷键会失效。

var parentArray = new Array(),
	player = null,
	fullStatus = false,
	backStyle = new Array(),
	childStyle = new Array(),
	i, playerStyle, parent, type, iframe;

document.addEventListener('DOMContentLoaded', init, false);

function init() {
	createButton();
	window.addEventListener("keydown", function(e) {
		//默认快捷键为alt + 1, 全屏/恢复。altkey是按键ALT,keyCode=49是按键1,需要修改为其他快捷键的请搜索"keycode",修改为按键对应的数字。
		if (e.altKey && e.keyCode == 49) {
			playerControl();
		}
	}, false);
	console.log('初始化完成');
}

function checkPlayer() {
	var embedArray = document.getElementsByTagName('embed');
	console.log('embed数量' + embedArray.length);
	checkEmbed(embedArray);
	if (!player) {
		console.log('未找到embed播放器');
		var objectArray = document.getElementsByTagName('object');
		console.log('object数量' + objectArray.length);
		checkObject(objectArray);
	}
	if (!player) {
		console.log('未找到object播放器');
		var iframeArray = document.getElementsByTagName('iframe');
		console.log('iframe数量' + iframeArray.length);
		checkIframe(iframeArray);
	} 
	if (!player) {
		console.log('未找到iframe引用的播放器');
		return;
	}
	parent = player.parentNode;
	var full = player;
	while (full = full.parentNode) {
		if (full.getAttribute) {
			full.setAttribute('full_stack', true);
			parentArray.push(full);
		}
		if (full.nodeName == 'HTML') {
			break;
		}
	}
	if (type=='innerIframe') {
		full = iframe;
		do {
			if (full.getAttribute) {
				full.setAttribute('full_stack', true);
				parentArray.push(full);
			}
			if (full.nodeName == 'HTML') {
				break;
			}
		} while (full = full.parentNode);
	}
	fullWin();
}

function createButton() {
	var leftButton = document.createElement('span');
	leftButton.id = 'leftFullStackButton';
	leftButton.onclick = function() {
		playerControl();
	};
	document.body.appendChild(leftButton);
	addStyle('#leftFullStackButton{position:fixed;width:2px;height:100%;top:0;left:0;z-index:666666;}');
	var rightButton = document.createElement('span');
	rightButton.id = 'rightFullStackButton';
	rightButton.onclick = function() {
		playerControl();
	};
	document.body.appendChild(rightButton);
	addStyle('#rightFullStackButton{position:fixed;width:2px;height:100%;top:0;right:0;z-index:666666;}');
}

function addStyle(css) {
	var style = document.createElement('style');
	style.type = 'text/css';
	var node = document.createTextNode(css);
	style.appendChild(node);
	document.head.appendChild(style);
}

function checkEmbed(embedArray) {
	if (embedArray.length > 0) {
		for (i = 0; i < embedArray.length; i++) {
			console.log('embed播放器检测' + i);
			if (embedArray[i].type == 'application/x-shockwave-flash' && embedArray[i].offsetWidth > 299 && embedArray[i].offsetHeight > 199) {
				player = embedArray[i];
				type = 'embed';
				console.log('找到embed播放器');
				break;
			}
		}
	}
}

function checkObject(objectArray) {
	if (objectArray.length > 0) {
		for (i = 0; i < objectArray.length; i++) {
			console.log('object播放器检测' + i);
			if (objectArray[i].type == 'application/x-shockwave-flash' && objectArray[i].offsetWidth > 299 && objectArray[i].offsetHeight > 199) {
				player = objectArray[i];
				type = 'object';
				console.log('找到object播放器');
				break;
			}
		}
	}
}

function checkIframe(iframeArray) {
	if (iframeArray.length > 0) {
		for (i=0;i<iframeArray.length;i++) {
			console.log(i);
			if (iframeArray[i].offsetWidth > 299 && iframeArray[i].offsetHeight > 199) {
				try {
					var embedArray = iframeArray[i].contentWindow.document.getElementsByTagName('embed');
					console.log('iframe'+i+'中embed数量' + embedArray.length);
					checkEmbed(embedArray);
					if (!player) {
						console.log('iframe'+i+'中未找到embed播放器');
						var objectArray = iframe.document.getElementsByTagName('object');
						console.log('iframe'+i+'中object数量' + objectArray.length);
						checkObject(objectArray);
					}
					if (player) {
						iframe = iframeArray[i];
						type = 'innerIframe';
						break;
					}
				} catch(e) {
					player = iframeArray[i];
					type = 'iframe';
					console.log('找到可能通过iframe跨域引用的播放器');
					break;
				}
			}
		}
	}
}

function playerControl() {
	if (!player) {
		checkPlayer();
	} else {
		if (!fullStatus) {
			switch (type) {
				case 'embed':
					var embedArray = parent.getElementsByTagName('embed');
					checkEmbed(embedArray);
					break;
				case 'object':
					var objectArray = parent.getElementsByTagName('object');
					checkObject(objectArray);
					break;
				case 'innerIframe':
				case 'iframe':
					var iframeArray = parent.getElementsByTagName('iframe');
					checkIframe(iframeArray);
					break;
			}
			fullWin();
		} else {
			smallWin();
		}
	}
}

function fullWin() {
	fullStatus = true;
	playerStyle = {
		width: player.style.width,
		height: player.style.height,
		margin: player.style.margin,
		padding: player.style.padding,
		position: player.style.position,
		zIndex: player.style.zIndex,
		boxSizing: player.style.boxSizing
	};
	for (i = 0; i < parentArray.length; i++) {
		backStyle[i] = {
			width: parentArray[i].style.width,
			height: parentArray[i].style.height,
			position: parentArray[i].style.position,
			margin: parentArray[i].style.margin,
			padding: parentArray[i].style.padding,
			background: parentArray[i].style.background,
			top: parentArray[i].style.top,
			left: parentArray[i].style.left,
			zIndex: parentArray[i].style.zIndex,
			overflow: parentArray[i].style.overflow
		};
		parentArray[i].style.width = '100%';
		parentArray[i].style.height = '100%';
		parentArray[i].style.position = 'fixed';
		parentArray[i].style.margin = '0 0';
		parentArray[i].style.padding = '0 0';
		parentArray[i].style.background = '#000';
		parentArray[i].style.top = '0';
		parentArray[i].style.left = '0';
		parentArray[i].style.zIndex = '555555';
		parentArray[i].style.overflow = 'hidden';
	}
	player.style.width = '100%';
	player.style.height = '100%';
	player.style.margin = '0 0';
	player.style.padding = '0 1px';
	// chrome下flash不能在常规流和绝对定位切换,否则会引发重载入
	if (player.style.position != 'fixed' && player.style.position != 'absolute') {
		player.style.position = 'relative';
	} else {
		player.style.position = 'fixed';
	}
	player.style.zIndex = '555555';
	player.style.boxSizing = 'border-box';
	console.log('网页全屏完成');
}

function smallWin() {
	fullStatus = false;
	for (i = 0; i < parentArray.length; i++) {
		parentArray[i].style.width = backStyle[i].width;
		parentArray[i].style.height = backStyle[i].height;
		parentArray[i].style.position = backStyle[i].position;
		parentArray[i].style.margin = backStyle[i].margin;
		parentArray[i].style.padding = backStyle[i].padding;
		parentArray[i].style.background = backStyle[i].background;
		parentArray[i].style.top = backStyle[i].top;
		parentArray[i].style.left = backStyle[i].left;
		parentArray[i].style.zIndex = backStyle[i].zIndex;
		parentArray[i].style.overflow = backStyle[i].overflow;
	}
	player.style.width = playerStyle.width;
	player.style.height = playerStyle.height;
	player.style.margin = playerStyle.margin;
	player.style.padding = playerStyle.padding;
	player.style.position = playerStyle.position;
	player.style.zIndex = playerStyle.zIndex;
	player.style.boxSizing = playerStyle.boxSizing;
	console.log('恢复完成');
}