Bashtube HTML5 Player and Downloader

Добавляет ссылку на видео файл и если браузер поддерживает воспроизведение mp4, то добавляет видеоплеер.

Versione datata 02/06/2014. Vedi la nuova versione l'ultima versione.

// ==UserScript==
// @name        Bashtube HTML5 Player and Downloader
// @description Добавляет ссылку на видео файл и если браузер поддерживает воспроизведение mp4, то добавляет видеоплеер.
// @namespace   2k1dmg@userscript
// @include     http://bashtube.ru/video/*
// @version     17
// @author      2k1dmg
// @grant       none
// ==/UserScript==

// 2014-06-02

(function(window) {
	'use strict';
	if (!window || (window && window.self !== window.top)) {
		return;
	}
	var document = window.document;

	var styleSheetLoader = function(canPlay) {
		var cssStyle = document.createElement('style');
		cssStyle.type = 'text/css';
		cssStyle.textContent = '#BThtml5videoDivForButton {\n' +
			'	width: 640px;\n' +
			'	height: 25px;\n' +
			'	padding: 15px 0 10px 0;\n' +
			'}\n' +
			'#BThtml5videoDivForButton > a {\n' +
			'	margin-right: 20px;\n' +
			'	float: right;\n' +
			'}\n';
		if (canPlay)
			cssStyle.textContent += '#BThtml5videoDivForButton > button {\n' +
			'	margin-left: 20px;\n' +
			'}\n';
		document.head.appendChild(cssStyle);
	};

	var createLink = function(file, poster) {
		var linkDiv = document.createElement('a');
		linkDiv.href = file;
		linkDiv.textContent = ' Скачать';

		var videoInfos = document.getElementsByClassName('video-info');
		var videoInfo = videoInfos[0];

		var canPlay = false;
		var v = document.createElement('video');
		if(v.canPlayType && v.canPlayType('video/mp4').replace(/no/, '')) {
			canPlay = true;
		}

		if (canPlay) {
			linkDiv.onclick = function(event) {
				if (event && event.button === 0) {
					event.preventDefault();
					event.stopPropagation();
				}
			};
			linkDiv.title = 'Скачивать правой кнопкой мыши!\nИ выбрать: "Сохранить объект как…" или т.п';
		}

		var divForButton = document.createElement('div');
		divForButton.id = 'BThtml5videoDivForButton';

		var canPlayAddBotton = function() {
			var html5Video = document.createElement('div');
			html5Video.id = 'BThtml5video';
			html5Video.innerHTML = '<video id="BThtml5videoPlayer" poster="' + poster + '" width="640" height="360" volume="0.5" controls="controls">' +
					'<source src="' + file + '" type="video/mp4">' +
				'</video>';

			videoInfo.parentNode.insertBefore(html5Video, videoInfo);

			var html5VideoPlayer = document.getElementById('BThtml5videoPlayer');
			html5VideoPlayer.volume = 0.5;

			var uppod = document.getElementById('uppod');
			uppod.style.display = canPlay ? 'none' : '';
			html5Video.style.display = canPlay ? '' : 'none';
			var toggleButton = document.createElement('button');
			divForButton.appendChild(toggleButton);
			divForButton.appendChild(linkDiv);

			toggleButton.textContent = canPlay ? 'FLASH' : 'HTML5';

			toggleButton.onclick = function() {
				var uppod = document.getElementById('uppod');
				var html5Video = document.getElementById('BThtml5video');
				if (uppod.style.display == 'none') {
					uppod.style.removeProperty('display');
					html5Video.style.display = 'none';
					this.textContent = 'HTML5';
				}
				else {
					html5Video.style.removeProperty('display');
					uppod.style.display = 'none';
					this.textContent = 'FLASH';
				}

			};
		};

		if (canPlay) {
			canPlayAddBotton();
		}
		else {
			divForButton.appendChild(linkDiv);
		}

		styleSheetLoader(canPlay);
		videoInfo.parentNode.insertBefore(divForButton, videoInfo);
	};

	var getParams = function() {
		var scriptContent = function() {
			var lastSibling = uppod.nextSibling;
			var targetContent;
			for (var i = 0; i < 15; i++) {
				if (lastSibling.nodeType === 1 &&
					/.*file:\s'(http:[^']*).*/m.test(lastSibling.textContent)) {
				   targetContent = lastSibling;
				   break;
				}
				lastSibling = lastSibling.nextSibling;
			}
			if (targetContent) {
				return targetContent.textContent;
			}
			else {
				return null;
			}
		};

		var pageBody = function() {
			var bodyContent = scriptContent() || document.body.innerHTML;
			var matchBodyContent = bodyContent.match(/.*file:\s'(http:[^']*).*/m);
			var matchBodyContentPoster = bodyContent.match(/.*poster:\s'(http:[^']*).*/m);
			if (matchBodyContent[1]) {
				file = matchBodyContent[1];
				poster = matchBodyContentPoster[1] ? matchBodyContentPoster[1] : '';
				createLink(file, poster);
			}
		};

		var uppod = document.getElementById('uppod');
		var params = uppod.getElementsByTagName('param');
		var flashvars;
		var file;
		var poster;
		if ('flashvars' in params && params['flashvars'].value) {
			flashvars = params['flashvars'].value;
		}
		else {
			pageBody();
			return;
		}
		var matchFlashvars = flashvars.match(/.*file=(http:[^&]*).*/m);
		var matchPoster = flashvars.match(/.*poster=(http:[^&]*).*/m);
		if (matchFlashvars[1]) {
			file = matchFlashvars[1];
			poster = matchPoster[1] ? matchPoster[1] : '';
			createLink(file, poster);
		}
	};

	if (document.readyState === 'complete') {
		getParams();
	}
	else {
		window.addEventListener('load', function pageLoaded(e) {
			window.removeEventListener('load', pageLoaded, false);
			getParams();
		}, false);
	}
})(window);