Greasy Fork is available in English.

Video Full Screen In Tab

让所有视频网页全屏

2014-09-06 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name        Video Full Screen In Tab
// @namespace   http://www.icycat.com
// @description 让所有视频网页全屏
// @author      冻猫
// @include     *
// @version     1.0
// @grant       none
// @run-at      document-end
// ==/UserScript==

(function() {
	var player;
	var btnDelay = null;
	var isOver = false;
	var parentArray = new Array(),
		fullStatus = false,
		backStyle = new Array(),
		childStyle = new Array(),
		playerStyle, parent, type, iframe;

	addStyle('#playerControlBtn {display:none;cursor: pointer;font: 12px "微软雅黑";padding: 2px 2px 3px 2px;margin:0;width:60px;text-align: center;transition: all 0.6s ease-out;position: absolute;z-index:2147483646;background-color: #27A9D8;color:#FFF;opacity:0.5;} #playerControlBtn:hover{opacity:1;}');
	addStyle('#leftFullStackButton{position:fixed;width:1px;height:100%;top:0;left:0;z-index:2147483646;}');
	addStyle('#rightFullStackButton{position:fixed;width:1px;height:100%;top:0;right:0;z-index:2147483646;}');
	createButton();

	var controlBtn = document.getElementById('playerControlBtn');

	document.addEventListener('mouseover', function(e) {
		if (fullStatus) {
			return;
		}
		var target = e.target;
		var nodeName = target.nodeName;
		switch (nodeName) {
			case 'OBJECT':
			case 'EMBED':
				if (target.type == 'application/x-shockwave-flash' && target.offsetWidth > 299 && target.offsetHeight > 199) {
					player = target;
					showButton();
				}
				break;
			case 'IFRAME':
			case 'VIDEO':
				if (target.offsetWidth > 299 && target.offsetHeight > 199) {
					player = target;
					showButton();
				}
				break;
			default:
				if (isOver) return;
				if (btnDelay) clearTimeout(btnDelay);
				btnDelay = setTimeout(function() {
					controlBtn.style.display = 'none';
				}, 1000);
				return;
		}
	});

	function createButton() {
		var btn = document.createElement('div');
		btn.id = 'playerControlBtn';
		btn.onclick = function() {
			playerControl();
		};
		btn.onmouseover = function() {
			isOver = true;
			if (btnDelay) clearTimeout(btnDelay);
		};
		btn.onmouseout = function() {
			isOver = false;
			if (btnDelay) clearTimeout(btnDelay);
			btnDelay = setTimeout(function() {
				controlBtn.style.display = 'none';
			}, 1000);
		}
		btn.appendChild(document.createTextNode('网页全屏'));
		document.body.appendChild(btn);
	}

	function showButton() {
		if (btnDelay) clearTimeout(btnDelay);
		var rect = player.getBoundingClientRect();
		var compStyle = getComputedStyle(player);
		var top = rect.top + parseFloat(compStyle.paddingTop) + parseFloat(compStyle.borderTopWidth) + document.documentElement.scrollTop;
		var left = rect.left + parseFloat(compStyle.paddingLeft) + parseFloat(compStyle.borderLeftWidth);
		controlBtn.style.display = 'inline';
		controlBtn.style.top = (top - 21) + 'px';
		controlBtn.style.left = (left + player.offsetWidth - 64) + 'px';
	}

	function createFullButton(id) {
		var fullButton = document.createElement('span');
		fullButton.id = id;
		fullButton.onclick = function() {
			playerControl();
		};
		document.body.appendChild(fullButton);
	}

	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 removeButton(id) {
		var button = document.getElementById(id);
		if (button) {
			button.parentNode.removeChild(button);
		}
	}

	function playerControl() {
		checkPlayer();
		if (!fullStatus) {
			fullWin();
		} else {
			smallWin();
		}
	}

	function checkPlayer() {
		parentArray = [];
		var full = player;
		while (full = full.parentNode) {
			if (full.getAttribute && full.nodeName != 'OBJECT') {
				parentArray.push(full);
			}
			if (full.nodeName == 'HTML') {
				break;
			}
		}
	}

	function fullWin() {
		if (!fullStatus) {
			window.addEventListener('resize', fullWin, false);
			playerStyle = player.style.cssText;
		}
		for (var i = 0; i < parentArray.length; i++) {
			if (!fullStatus) {
				backStyle[i] = parentArray[i].style.cssText;
			}
			parentArray[i].style.cssText = 'width:100% !important;height:100% !important;max-width:100% !important;margin:0px !important;padding:0px !important;top:0px !important;left:0px !important;z-index:2147483645 !important;overflow:hidden !important;position:fixed !important;background:#000 !important;border:none !important;';
		}

		player.style.cssText = 'left:' + (1 - player.offsetLeft) + 'px !important;top:' + (0 - player.offsetTop) + 'px !important;' + 'width:calc(100% - 2px) !important;height:100% !important;z-index:2147483645 !important;position:relative !important;visibility:visible !important;display:inline !important;';

		createFullButton('leftFullStackButton');
		createFullButton('rightFullStackButton');

		console.log('网页全屏完成');
		fullStatus = true;
	}

	function smallWin() {
		window.removeEventListener('resize', fullWin, false);
		for (var i = 0; i < parentArray.length; i++) {
			parentArray[i].style.cssText = backStyle[i];
		}
		player.style.cssText = playerStyle;
		removeButton('leftFullStackButton');
		removeButton('rightFullStackButton');
		console.log('恢复完成');
		fullStatus = false;
	}

})();