Video Player Toothbrush

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

От 12.07.2014. Виж последната версия.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==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.5
// @grant       none
// @run-at      document-start
// ==/UserScript==

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

var parentArray = new Array(),
	player = null,
	fullStatus = false,
	backStyle = new Array(),
	childStyle = new Array(),
	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 objectArray = document.getElementsByTagName('object');
	console.log('object数量' + objectArray.length);
	checkObject(objectArray);
	if (!player) {
		console.log('未找到object播放器');
		var embedArray = document.getElementsByTagName('embed');
		console.log('embed数量' + embedArray.length);
		checkEmbed(embedArray);
	}
	if (!player) {
		console.log('未找到embed播放器');
		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 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 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 checkIframe(iframeArray) {
	if (iframeArray.length > 0) {
		for (var i=0;i<iframeArray.length;i++) {
			if (iframeArray[i].offsetWidth > 299 && iframeArray[i].offsetHeight > 199) {
				try {
					var objectArray = iframeArray[i].contentWindow.document.getElementsByTagName('object');
					console.log('iframe'+i+'中object数量' + objectArray.length);
					checkObject(objectArray);
					if (!player) {
						console.log('iframe'+i+'中未找到object播放器');
						var embedArray = iframeArray[i].contentWindow.document.getElementsByTagName('embed');
						console.log('iframe'+i+'中embed数量' + embedArray.length);
						checkEmbed(embedArray);
					}
					if (player) {
						iframe = iframeArray[i];
						type = 'innerIframe';
						break;
					} else {
						console.log('未找到iframe'+i+'中的播放器');
					}
				} catch(e) {
					player = iframeArray[i];
					type = 'iframe';
					console.log('找到可能通过iframe跨域引用的播放器');
					break;
				}
			}
		}
	}
}

function playerControl() {
	if (!player) {
		checkPlayer();
	} else {
		if (!fullStatus) {
			switch (type) {
				case 'object':
					var objectArray = parent.getElementsByTagName('object');
					checkObject(objectArray);
					break;
				case 'embed':
					var embedArray = parent.getElementsByTagName('embed');
					checkEmbed(embedArray);
					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,
		left: player.style.left
	};
	for (var 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%';
		//chrome下flash父级若是object切换position会引发重载入
		if (i==0 && parentArray[i].nodeName == 'OBJECT' && parentArray[i].style.position != 'fixed' && parentArray[i].style.position != 'absolute') {
			parentArray[i].style.position = 'relative';
		} else {
			parentArray[i].style.position = 'fixed';
		}
		parentArray[i].style.margin = '0';
		parentArray[i].style.padding = '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 = 'calc(100% - 2px)';
	player.style.height = '100%';
	player.style.margin = '0';
	player.style.padding = '0';
	// chrome下flash不能在常规流和绝对定位切换position,否则会引发重载入
	if (player.style.position != 'fixed' && player.style.position != 'absolute') {
		player.style.position = 'relative';
	} else {
		player.style.position = 'fixed';
	}
	player.style.zIndex = '555555';
	player.style.left = '1px';
	console.log('网页全屏完成');
}

function smallWin() {
	fullStatus = false;
	for (var 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.left = playerStyle.left;
	console.log('恢复完成');
}