nwolf / LegendasTV+

// ==UserScript==
// @name        LegendasTV+
// @namespace   http://www.legendas.tv
// @description	Melhora a navegação e adiciona recursos ao site Legendas.tv
// @include     http://legendas.tv/*
// @version     0.0.8
// @updateURL	https://openuserjs.org/meta/nwolf/LegendasTV+.meta.js
// @downloadURL	https://openuserjs.org/install/nwolf/LegendasTV+.user.js
// @grant		GM_getResourceURL
// @require		https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @require		https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js
// @resource	yt_logo	https://www.youtube.com/yt/brand/media/image/YouTube-logo-full_color.png
// ==/UserScript==

//inject JQuery UI
$("head").append (
	'<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" '
		+ 'rel="stylesheet" type="text/css">'
);
addGlobalStyle('.ui-tooltip {color: #58595B; font-size: 13px; font-weight: normal;}');

start();

function start() {

	var path = window.location.pathname;

	if(path === '/') {
		startMain();
	} else {
		var download = /\/download\/(\w*)+\/[\w\/]*/i;
		if(download.test(path)){
			startDownloadPage(download.exec(path)[1]);
		}         
	}
}

// -------------------------------------------------------------------- //

function startMain() {
	var target = document.querySelector('#pagination_target');
	var observer = new MutationObserver(function(mutations) {
			addPosterLinks();
			addNameDetails();
			forceDirectDownloadLink();
		});
	var config = { childList: true };
	observer.observe(target, config);
}

function addPosterLinks(){
	var items = $('div.film');
	var tooltip = getTooltip();
	items.each(function(index, element){
		var href = $(element).find('h3>a').attr('href');
		var poster = $(element).children('img');
		var link = document.createElement('a');
		$(link).attr('href', href);
		$(link).attr('title', '_');
		$(link).tooltip(tooltip);
		$(poster).wrapAll(link);
	});
}

function addNameDetails(){
	var links = $('div.film > h3 > a');
	$(links).attr('title', '_');
	$(links).tooltip(getTooltip());
}

function getTooltip() {
	var onOpened = function(event, ui) {
		ui.tooltip[0].textContent = 'Carregando detalhes...';		
		var href = $(event.target).attr('href');
		$.get(href, function(data){
			var description = $('.t1 > p:nth-child(1)', data).text();
			ui.tooltip[0].textContent = description;
		});
	};
	
	return { open: onOpened };
}

function forceDirectDownloadLink() {
	var links = $('span.bt_seta_download>a');
	links.each(function(index, element) {
		var href = $(element).attr('href').replace('download', 'downloadarquivo');
		$(element).attr('href', href);
	});
}

// -------------------------------------------------------------------- //

function startDownloadPage(subId) {

	addTitleSearch();
	addSearch();
	addYoutube();

	if(isSeriesDownload()) {
		//TODO;
	} else {
		//TODO;
	}
}

function addYoutube() {
	var title = $('div.middle.download>section.first h3').text();
	var search = 'https://www.youtube.com/results?search_query=' + title;

	var mainText = $('div.middle.download>section>div.t2>p');
	mainText.before(
		'<p>' +
			'<a href="' + search + '" target="_blank">' +
				'<img id="yt" src="' + GM_getResourceURL('yt_logo') + '" style="cursor:pointer;width:70px">' +
				'<span>Procurar no YouTube</span>' +
			'</a>' +
		'</p>'
	);
}

function addSearch() {
	var description = $('div.middle.download>section>div.t2>p');
	var textLines = document.evaluate(".//text()", 
									  description[0],
									  null,
									  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
									  null);
	var isRelease = /\b(?:\w+[\. ]){3,}\w+-\w+\b/i;
	for (var i = 0; i < textLines.snapshotLength; i++) {
		var line = textLines.snapshotItem(i);
		if(isRelease.test($(line).text())) {
			var release = isRelease.exec($(line).text())[0];
			var link = document.createElement('a');
			$(link).attr('href', 'https://thepiratebay.org/search/' + release);
			$(line).wrap(link);
		}
	}
}

function addTitleSearch() {
	var title = $('div.middle.download>section.first h3');
	var link = document.createElement('a');
	$(link).attr('href', '/busca/' + title.text().replace(':', ''));
	$(title).wrap(link);
}

function isSeriesDownload() {

	var title1 = $('div.middle.download>section.first h3').text();
	var title2 = $('div.middle.download>section.first h5').text();
	var release = $('div.middle.download>section h1').text();   

	var hasSeason = /.*\s*S\d+E\d+.*\s*/i;
	return title1.toLowerCase().indexOf('temporada') > -1 
		|| title2.toLowerCase().indexOf('temporada') > -1
		|| hasSeason.test(release);
}

// -------------------------------------------------------------------- //

function searchImdbId() {
	var imdb = null;
	var imdbLink = /(http)?s?(:\/\/)?(www.)?imdb.com\/title\/(tt\d+)\/?/i;
	var links = $('div.middle.download>section>div.t2>p>a');
	links.each(function(index, link){
		var href = $(link).attr('href');
		if(imdb === null && imdbLink.test(href)){
			var groups = imdbLink.exec(href);
			imdb = groups[groups.length -1];
		}
	});
	return imdb;
}

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}