Fix Yify

Add content ratings to Yify listings and replace torrent urls with magnet links.

Versione datata 16/04/2015. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        Fix Yify
// @namespace   yts.to
// @description Add content ratings to Yify listings and replace torrent urls with magnet links.
//
// @include     https://yts.re/browse-movie*
// @include     http://yts.re/browse-movie*
// @include     https://yts.to/browse-movie*
// @include     http://yts.to/browse-movie*
// @require     http://code.jquery.com/jquery-1.11.0.min.js
// @version     0.1.2
// @grant       GM_xmlhttpRequest, GM_addStyle
// ==/UserScript==

function addMpaaRating(dom, div) {
  $(".icon-eye", $(dom)).each(function(i) {
    var rating = $(this).parent().text().trim();
    // console.log("mpaa: "+rating);
    var star = div.find('.icon-star');
    star.replaceWith('<h4 class="rating">'+rating+'</h4>');
  });
}

function is1080magnet(url) {
  var index = url.search("1080p");
  return (index > -1);
}

function replaceOriginalTorrentLinks(dom, div) {
  $(".magnet", $(dom)).each(function(i) {
    var url = $(this).attr('href');
    // console.log("mag: "+url);
    var orig1080tor = null;
    var orig720tor = null;
    var links = div.find('.browse-movie-tags a');
    $(links).each(function(i) {
      // console.log("test: "+$(this).text());
      if ($(this).text().indexOf("1080p") >= 0) {
        // 1080
        orig1080tor = $(this);
      } else {
        // 720
        orig720tor = $(this);
      }
    });

    if (is1080magnet(url)) {
      // replace 1080 link
      orig1080tor.replaceWith('<a href="'+url+'">1080p</a>');
      // console.log("replaced 1080");
    } else {
      // replace 720 link
      orig720tor.replaceWith('<a href="'+url+'">720p</a>');
      // console.log("replaced 720");
    }
  });
}

function imposeMyWill(url, div) {
  var data = null;
  // console.log("DIV: "+ div);
  GM_xmlhttpRequest({
    method: "GET",
    url: url,
    onload: function(response) {
      // We've received a response
      data = $.parseHTML(response.responseText);
      // console.log("DATA: "+ data);
      replaceOriginalTorrentLinks(data, div);
      addMpaaRating(data, div);
      return;
    },
    onerror: function(response) {
      data = JSON.parse(response.responseText);
      console.log('ERROR: '+data);
      // $('#yts-options').html('<p>ERROR! Failed to connect to the YTS website.</p>');
    }
  });
}

function removeAds() {
  var ads = $('a.hidden-xs');
  if (ads.length) {
    console.log('Removing ad box.');
    ads.remove();
  }
}

$(document).ready(function() {
  var divs = $(".col-lg-4");
  var link = null;
  var url = null;

  removeAds();

  $(divs).each(function(i) {
    link = $(this).find('.browse-movie-link');
    url = link.attr('href');
    // console.log("URL: "+url);
    imposeMyWill(url, $(this));
  });
});