Kones / SnatchlistImproved

// ==UserScript==
// @name         SnatchlistImproved
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Improves snatchlist
// @author       BTN
// @contributor  ChillMeister
// @match        https://broadcasthe.net/user.php?action=edit&userid=*
// @match        https://broadcasthe.net/snatchlist.php*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js
// @icon         https://broadcasthe.net/favicon.ico
// @grant        unsafeWindow
// @grant        GM_log
// @connect      broadcasthe.net
// ==/UserScript==
'use strict';
$(document).ready(function () {
  var window = unsafeWindow;
  function setItem(key, value) {
    try {
      window.localStorage.setItem(key, value);
    } catch (e) {
      // error modifying local storage (out of space?)
      console.error(e);
    }
  }
  function getItem(key, defaultValue) {
    try {
      var result = window.localStorage.getItem(key);
      return (result ? result : defaultValue);
    } catch (e) {
      // error reading from local storage
      console.error(e);
      return defaultValue;
    }
  }
  function snatchListFilter() {
    // pulled from https://github.com/Userscriptz/BTN-Snatchlist/blob/master/main.user.js
    function changeTable() {
      var snatchView = getItem('selectedmode', 'all');
      var links = getItem('dllinks', 'no');
      if (links == 'yes') {
        $('.download-btn').show();
      } else {
        $('.download-btn').hide();
      }
      $('#SnatchData').find('table:nth-child(9) > tbody tr').each(function () {
        if ($(this).attr('class') != 'colhead_dark') {
          var id = '#' + $(this).attr('id');
          var status = $(id + ' > td:nth-child(7) > span').text();
          var seedTimeLeft = $(id + ' > td:nth-child(5)').text();
          var remove;
          
          if (snatchView == 'showcomplete') {
            remove = (seedTimeLeft != 'Complete' || status != 'Seeding');
          } 
          else if (snatchView == 'showcomplete2') {
            remove = (seedTimeLeft != 'Complete' || status != 'Complete');
          } 
          else if (snatchView == 'seedtimeleft') {
            remove = (seedTimeLeft == 'Complete' || status == 'Complete');
          } 
          else if (snatchView == 'inactive') {
            remove = (status != 'Inactive');
          } 
          else if (snatchView == 'leeching') {
            remove = (status != 'Leeching');
          } 
          else {
            remove = false;
          }
          if (remove) {
            $(id).hide();
          } else {
            $(id).show();
          }
        }
      });
    }
    var selectedMode = getItem('selectedmode', 'all');
    var dllinks = getItem('dllinks', 'no');
    var cssStyle = '.mode-active {color:limegreen; font-weight:bold;} ';
    cssStyle += '#div-1 {position:relative; padding-top:40px;} ';
    cssStyle += '#div-1a {position:absolute; top:0; right:0; width:170px;} ';
    cssStyle += '#div-1b {position:absolute; top:0; left:0; width:600px;} ';
    cssStyle += '.mode-hidden {display: none} ';
    $('<style>').prop('type', 'text/css').html(cssStyle).appendTo('head');
    var selectModeLink = function (text, key, mode, active) {
      return $('<a>', {
        text: text,
        href: '#',
        click: function (e) {
          setItem(key, mode);
          $('a').filter('.mode-active.' + key).removeClass('mode-active');
          $(this).addClass('mode-active');
          changeTable();
          e.preventDefault();
        },
        class: (active ? 'mode-active ' : '') + key
      });
    };
    var div1b = $('<div>', {
      id: 'div-1b',
      text: 'Torrents: '
    });
    var div1a = $('<div>', {
      id: 'div-1a',
      text: 'Downloads: '
    });
    div1b.append(selectModeLink('All', 'selectedmode', 'all', selectedMode == 'all'), ' | ', selectModeLink('Inactive', 'selectedmode', 'inactive', selectedMode == 'inactive'), ' | ', selectModeLink('Leeching', 'selectedmode', 'leeching', selectedMode == 'leeching'), ' | ', selectModeLink('Seed Time Left', 'selectedmode', 'seedtimeleft', selectedMode == 'seedtimeleft'), ' | ', selectModeLink('Complete (Seeding)', 'selectedmode', 'showcomplete', selectedMode == 'showcomplete'), ' | ', selectModeLink('Complete', 'selectedmode', 'showcomplete2', selectedMode == 'showcomplete2'));
    div1a.append(selectModeLink('Enable', 'dllinks', 'yes', dllinks == 'yes'), ' | ', selectModeLink('Disable', 'dllinks', 'no', dllinks == 'no'));
    var div1 = $('<div>', {
      id: 'div-1'
    });
    div1.append(div1b, div1a);
    div1.prependTo('#content');
    var modifySnatchlistElements = function () {
      var authkey = getItem('authkey');
      var torrent_pass = getItem('torrent_pass');
      if (dllinks == 'yes' && (!authkey || !torrent_pass)) {
        var torrentUrl = prompt('Please copy paste a torrent download link') || '';
        torrentUrl = torrentUrl.match(/authkey=([0-9a-f]+)&torrent_pass=([0-9a-z]+)/);
        if (!torrentUrl || !torrentUrl[1] || !torrentUrl[2]) {
          return;
        }
        authkey = torrentUrl[1];
        torrent_pass = torrentUrl[2];
        setItem('authkey', authkey);
        setItem('torrent_pass', torrent_pass);
        location.reload();
      } else {
        $('#SnatchData').find('table:nth-child(9) > tbody tr').each(function () {
          if ($(this).attr('class') != 'colhead_dark') {
            var id = '#' + $(this).attr('id');
            var torrentid = $(id + ' > td:nth-child(1) > span > a:nth-child(2)').attr('href').match(/torrentid=([0-9]+)/) [1];
            var link = $('<a>', {
              href: 'http://broadcasthe.net/torrents.php?action=download&id=' + torrentid + '&authkey=' + authkey + '&torrent_pass=' + torrent_pass,
              class: 'download-btn'
            });
            $('<img>', {
              src: 'https://cdn.broadcasthe.net/common/browsetags/download.png'
            }).appendTo(link);
            link.appendTo(id + ' > td:nth-child(1) > span');
          }
        });
      }
      changeTable();
    };
    window.SortSnatch = exportFunction(function (sort, uid, page) {
      var old = '';
      var hnr = '';
      if (window.location.href.search('old') !== - 1) {
        old = '&old=1';
      }
      if (window.location.href.search('hnr') !== - 1) {
        hnr = '&hnr=1';
      }
      console.log('Fetching page ' + page);
      $.get('https://broadcasthe.net/snatchlist.php?type=ajax&id=' + uid + '&sort=' + sort + '&page=' + page + old + hnr, function (data) {
        data = $(data) [0];
        console.log('Fetched page ' + page);
        $('#SnatchData').replaceWith(data);
        modifySnatchlistElements();
      });
      return false;
    }, window);
    modifySnatchlistElements();
  }
  function addTorrentPerPageOptions() {
    $('#torpersnatch').replaceWith('<input name="torpersnatch" id="torpersnatch" type="number" min="10" max="100000" step="10" value="1000" size="6">');
    $('#torperpage').replaceWith('<input name="torperpage" id="torperpage" type="number" min="25" max="100000" step="5" value="100" size="6">');
  }
  if (/user\.php\?action=edit&userid=[0-9]+/.test(location.href)) {
    addTorrentPerPageOptions();
  } else if (/snatchlist\.php(?:\?id=[0-9]+)?/.test(location.href)) {
    snatchListFilter();
  }
});