Greasy Fork

Greasy Fork is available in English.

Download Sibnet Video as MP4

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

当前为 2017-07-27 提交的版本,查看 最新版本

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

var POSTMessage = true;
var CANCELPostvideo = 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()
{
	removeAds();
	if( CANCELPostvideo )
		setCancelPostvideo();
	var pladform = detectPladformIFrames();
	if( pladform )
	{
		console.log('embeded video from pladform.ru');
		return;
	}
	var video_path = getVideoPatch();// get video path name '/v/{numbers}/{videoid}.mpd' from current html source page
	try{
		if( !video_path )
			throw new Error("can't find video path");
		var xhr = createXMLHttpObject();
		xhr.onreadystatechange = makeVideoLink(video_path);
		clog('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(error){
		console.error("[start] ", error);
	}
}
function getVideoPatch()
{
	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("[getVideoPatch] ", e);
		return null;
	}
}
function makeVideoLink( video_path )
{
	return function(){
		if( this.readyState == 4 && this.status == 200 )
		{
			var video_link = getVideoLink( this.responseText );// get downloadable video link
			if( !video_link )
			{
				console.error("[makeVideoLink] can't find video source link");
				return;
			}
			clog('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
			}
		}
	};
}
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 getVideoLink( 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.error("[getVideoLink] ", 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 setCancelPostvideo()
{
	try{
		document.getElementById('player_container').addEventListener('ended', function(event){
			try{
				var postvideo_cancel = document.getElementsByClassName('vjs-postvideo-cancel')[0];
				setTimeout( function(){postvideo_cancel.click();}, 2000 );
			}catch(err){
				console.error("[setCancelPostvideo] ", err);
			}
		}, true );
	}catch(error){
		console.error("[setCancelPostvideo] ", error);
	}
}
if( !clog )
{
	if( window.consoleLogger && window.consoleLogger.clog )
		var clog = window.consoleLogger.clog;
	else
		var clog = console.log;
}
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 video_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);
	}
}