Video Player Toothbrush

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

目前为 2014-07-06 提交的版本。查看 最新版本

// ==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/*
// @version     1.9
// @grant       none
// @run-at      document-start
// ==/UserScript==

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

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

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播放器');
		return false;
	} else {
		parent = player.parentNode;
		var full = player;
		while (full = full.parentNode) {
			if (full.getAttribute) {
				full.setAttribute('full_stack', true);
				divArray.push(full);
			}
		}
		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:66666;}');
	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:66666;}');
}

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].getAttribute('allowfullscreen') && embedArray[i].offsetWidth > 500) {
				player = embedArray[i];
				type = 'embed';
				console.log('找到embed播放器');
				break;
			}
		}
	}
}

function checkObject(objectArray) {
	if (objectArray.length > 0) {
		objectloop: 
		for (i = 0; i < objectArray.length; i++) {
			console.log('object播放器检测' + i);
			if (objectArray[i].offsetWidth > 500) {
				var paramArray = objectArray[i].getElementsByTagName('param');
				var j;
				for (j = 0; j < paramArray.length; j++) {
					if (paramArray[j].name.toLowerCase() == 'allowfullscreen') {
						player = objectArray[i];
						type = 'object';
						console.log('找到object播放器');
						break objectloop;
					}
				}
			}
		}
	}
}

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;
			}
			fullWin();
		} else {
			smallWin();
		}
	}
}

function fullWin() {
	fullStatus = true;
	playerStyle = {
		width: player.style.width,
		height: player.style.height,
		margin: player.style.margin,
		padding: player.style.padding,
		bodyOverflow: document.body.style.overflow,
		htmlOverflow: document.body.parentNode.style.overflow
	};
	for (i = 0; i < divArray.length; i++) {
		backStyle[i] = {
			width: divArray[i].style.width,
			height: divArray[i].style.height,
			position: divArray[i].style.position,
			margin: divArray[i].style.margin,
			padding: divArray[i].style.padding,
			background: divArray[i].style.background,
			top: divArray[i].style.top,
			left: divArray[i].style.left,
			zIndex: divArray[i].style.zIndex
		};
		divArray[i].style.width = '100%';
		divArray[i].style.height = '100%';
		divArray[i].style.position = 'fixed';
		divArray[i].style.margin = '0 0';
		divArray[i].style.padding = '0 0';
		divArray[i].style.background = '#000';
		divArray[i].style.top = '0';
		divArray[i].style.left = '0';
		divArray[i].style.zIndex = '55555';
	}
	for (i=0; i < parent.childNodes.length; i++) {
		console.log(parent.childNodes[i].nodeName.toLowerCase());
		if (parent.childNodes[i].nodeName.toLowerCase()!=type && parent.childNodes[i].nodeName.substr(0,1)!='#') {
			childStyle[i] = parent.childNodes[i].style.display;
			parent.childNodes[i].style.display = 'none';
			console.log('隐藏子元素');
		}
	}
	document.body.style.overflow = 'hidden';
	document.body.parentNode.style.overflow = 'hidden';
	player.style.width = '100%';
	player.style.height = '100%';
	player.style.margin = '0 0';
	player.style.padding = '0 1px';
	console.log('网页全屏完成');
}

function smallWin() {
	fullStatus = false;
	for (i = 0; i < divArray.length; i++) {
		divArray[i].style.width = backStyle[i].width;
		divArray[i].style.height = backStyle[i].height;
		divArray[i].style.position = backStyle[i].position;
		divArray[i].style.margin = backStyle[i].margin;
		divArray[i].style.padding = backStyle[i].padding;
		divArray[i].style.background = backStyle[i].background;
		divArray[i].style.top = backStyle[i].top;
		divArray[i].style.left = backStyle[i].left;
		divArray[i].style.zIndex = backStyle[i].zIndex;
	}
	for (i=0; i < parent.childNodes.length; i++) {
		if (parent.childNodes[i].nodeName.toLowerCase()!=type && parent.childNodes[i].nodeName.substr(0,1)!='#') {
			parent.childNodes[i].style.display = childStyle[i] ;
		}
	}
	document.body.style.overflow = playerStyle.bodyOverflow;
	document.body.parentNode.style.overflow = playerStyle.htmlOverflow;
	player.style.width = playerStyle.width;
	player.style.height = playerStyle.height;
	player.style.margin = playerStyle.margin;
	player.style.padding = playerStyle.padding;
	console.log('恢复完成');
}