Download Sibnet Video as MP4

Выдает ссылку на видео в правом нижнем углу плеера

As of 2017-07-27. See the latest version.

// ==UserScript==
// @name        Download Sibnet Video as MP4
// @namespace   http://video.sibnet.ru
// @description Выдает ссылку на видео в правом нижнем углу плеера
// @include     *://video.sibnet.ru/*
// @version     1.2.8.1
// @author      Iron_man
// @grant       none
// ==/UserScript==

var POSTMessage = true;
var XMLHTTPFactory = [
function(){return new XMLHttpRequest();},
function(){return new ActiveXObject('Msxml3.XMLHTTP');},
function(){return new ActiveXObject('Msxml2.XMLHTTP.6.0');},
function(){return new ActiveXObject('Msxml2.XMLHTTP.3.0');},
function(){return new ActiveXObject('Msxml2.XMLHTTP');},
function(){return new ActiveXObject('Microsoft.XMLHTTP');},
];
window.addEventListener('message', recieveMessage, false );
document.addEventListener('DOMContentLoaded', start, false );
function start()
{
	try{
		removeAds();
		var pladform = detectPladformIFrames();
		if( pladform )
		{
			console.log('embeded video from pladform.ru');
			return;
		}
		var video_path = findVideoPath();// get video path name '/v/{numbers}/{videoid}.mpd' from current html source page
		if( !video_path )
			throw new Error("can't find video path");
		var xhr = createXMLHttpObject();
		xhr.onreadystatechange = function()
		{
			if( xhr.readyState == 4 && xhr.status == 200 )
			{
				var video_link = findVideoLink( xhr.responseText );// get downloadable video link
				if( !video_link )
				{
					console.error("[start] can't find video source link");
					return;
				}
				console.log('video link: ', video_link);
				var stat = insertLink( video_link );// try to insert the link into 'video_size' element of html source page
				if( stat !== 0 )// if failed to insert the link then 
				{
					if( window.parent !== window.self && POSTMessage )// if not in parent window then post video_link to parent
						window.parent.postMessage( video_link + '&id=' + video_path.match(/\d+\.mpd/)[0].slice(0,-4), '*');
					window.location = video_link;// open it in current page
				}
			}
		};
		console.log('video path: ', video_path);
		xhr.open('GET', video_path, true);//  send 'GET' request to 'http://video.sibnet.ru/v/{numbers}/{videoid}.mpd'
		xhr.send(null);
	}catch(e){
		console.error("[start] ", e);
	}
}
function findVideoPath()
{
	var video, source_str, pos, end;
	try{
		video = document.getElementById('video');
		if( video )
			source_str = video.innerHTML;
		else
			source_str = document.body.innerHTML;
		pos = source_str.indexOf( "player.src([{src: \"" );
		if( pos == -1 )
			throw new Error("matching 'player.src([{src: \"' string not found");
		pos = source_str.indexOf("/v/", pos);
		end = source_str.indexOf(".mpd", pos);
		if( end == -1 )
			throw new Error("matching '.mpd' string not found");
		return source_str.substring(pos, end+4);
	}catch(e){
		console.error("[findVideoPath] ", e);
		return null;
	}
}
function createXMLHttpObject() //  create xhr object
{
	for( var i = 0; i < XMLHTTPFactory.length; ++i )
	{
		try{
			return XMLHTTPFactory[i]();
		}catch(e){}
	}
	console.error("[createXMLHttpObject] can't create xhr object");
	return null;
}
function removeAds()
{
	try{document.querySelector('.main tbody').children[0].innerHTML = '<td style="height:0px"></td>';}
	catch(e){console.error('[removeAds] ', e);}
}
function findVideoLink( source_str )
{
	try{
		var pos = source_str.indexOf("initialization=\"");
		if( pos == -1 )
			throw new Error( "matching string 'initialization=\"' not found");
		pos = source_str.indexOf("http", pos);
		var end = source_str.indexOf("noip=1", pos);
		if( end == -1 )
			throw new Error( "matching string 'noip=1' not found");
		source_str = source_str.substring(pos, end+6);
		var result_str = source_str.replace(/amp;/g, '');
		result_str = result_str.replace("/init-$RepresentationID$", "");
		return result_str;
	}catch(e){
		console.log("[findVideoLink] ", e);
		return null;
	}
}
function insertLink( source_link, size_mb ) // insert hyper reference into video_size element  
{
	try{
		//console.log('inserting link into video_size ...');
		var video_size = document.getElementsByClassName('video_size')[0];
		if( !video_size )
			return 1;
		video_size.innerHTML = '<a href="' + source_link + '" title="Скачать">' + ( size_mb ? size_mb : video_size.innerHTML ) + '</a>';
		return 0;
	}catch(e){
		console.error("[insertLink] ", e);
		return -1;
	}
}
function detectPladformIFrames()
{
	var iFrames = document.getElementsByTagName('iframe'), count = 0;
	for( var i = 0; i < iFrames.length; ++i )
	{
		if( iFrames[i].src.indexOf('pladform.ru') != -1 )
		{
			iFrames[i].setAttribute('name', 'pladform' + i);
			iFrames[i].setAttribute('id', 'pladform' + i);
			++count;
		}
	}
	return count;
}
function recieveMessage(event)
{
	try{
		if( event.origin.indexOf('out.pladform.ru') != -1 )
		{
			var stat = insertLink( event.data.url, '- Mb' );// try to insert link to vide_size
			if( stat !== 0 )// if failed then
			{
				if( window.parent !== window.self && POSTMessage )// if not in parent window then
					window.parent.postMessage(event.data.url, '*');// post message to parent
				window.location = event.data.url;
			}
		}else{
			//console.info('[recieveMessage] message from ' + event.origin + ' is ignored');
		}
	}catch(error){
		console.error("[recieveMessage] ", error);
	}
}