Greasy Fork

Greasy Fork is available in English.

Download Sibnet Video as MP4

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

当前为 2017-10-08 提交的版本,查看 最新版本

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

window.addEventListener('message', recieveMessage, false );
document.addEventListener('DOMContentLoaded', start, false );

var postMessage = true;
var clickCancel = true;
function start()
{
	removeAds();
	if( clickCancel )
		setCancelPostvideo();
	var pladform = detectPladformIFrames();
	if( pladform )
	{
		console.log('embeded video from pladform.ru');
		return;
	}
	var video_path = getVideoPath();// get video path name '/v/{numbers}/{videoid}.mpd' from current html source page
	console.log("video path: " + video_path);
	if( video_path )
	{
		GM_xmlhttpRequest({
			url: video_path,
			method: 'HEAD',
			onload: makeVideoLink,
		});
	}
}
function getVideoPath()
{
	var video, source_str, pos, end;
	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 )
		return null;
	pos = source_str.indexOf("/v/", pos);
	end = source_str.indexOf(".mpd", pos);
	if( end == -1 )
		return null;
	return source_str.substring(pos, end+4);
}
function makeVideoLink( xhr )
{
	var video_link = xhr.finalUrl.replace('/manifest.mpd', '.mp4');// get downloadable video link
	console.log("video file: ", video_link);
	if( !video_link )
	{
		console.error("[makeVideoLink] can't find video source link");
		return;
	}
	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=' + this.url.match(/\d+\.mpd/)[0].slice(0,-4), '*');
		window.location.href = video_link;// open it in current page
	}
}
function insertLink( source_link, size_mb ) // insert hyper reference into video_size element  
{
	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;
}
function removeAds()
{
	var tbody = document.querySelector('.main tbody');
	if( tbody && tbody.children && tbody.children[0] )
		tbody.children[0].innerHTML = '<td style="height:0px"></td>';
}
function setCancelPostvideo()
{
	var player_container = document.getElementById('player_container');
	if( player_container )
	{
		player_container.addEventListener('ended', function(event){
			var postvideo_cancel = document.getElementsByClassName('vjs-postvideo-cancel')[0];
			if( postvideo_cancel )
				setTimeout( function(){postvideo_cancel.click();}, 1000 );
		}, true );
	}
}
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)
{
	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.href = event.data.url;
		}
	}else{
		//console.info('[recieveMessage] message from ' + event.origin + ' is ignored');
	}
}