// ==UserScript==
// @name         Similar Artists from WCD missing on RED
// @version      0.8
// @description  Add a box to the sidebar with the missing Similar Artists from the WCD metadata
// @author       Chameleon
// @include      http*://*redacted.ch/artist.php?id=*
// @include      http*://*redacted.ch/torrents.php?id=*
// @grant        GM_xmlhttpRequest
// @namespace https://greasyfork.org/users/87476
// @downloadURL https://update.greasyfork.icu/scripts/34926/Similar%20Artists%20from%20WCD%20missing%20on%20RED.user.js
// @updateURL https://update.greasyfork.icu/scripts/34926/Similar%20Artists%20from%20WCD%20missing%20on%20RED.meta.js
// ==/UserScript==
(function() {
  'use strict';
  if(window.location.href.indexOf('artist.php?id=') !== -1)
  {
    artistpage();
  }
  else if(window.location.href.indexOf('torrents.php?id=') !== -1)
  {
    torrentspage();
  }
})();
function torrentspage()
{
  var h2=document.getElementsByTagName('h2')[0];
  var artist=h2.getElementsByTagName('a')[0].textContent;
  var album=h2.getElementsByTagName('span')[0].textContent;
  //var wcd_collage_table=document.createElement('table');
  GM_xmlhttpRequest({method: "GET",
                     url: "http://159.89.252.33/torrents.php?artistname="+encodeURIComponent(artist)+"&groupname="+encodeURIComponent(album),
                     onload: gotSearch
                    });
}
function gotSearch(response)
{
  var div=document.createElement('div');
  div.innerHTML=response.responseText;
  var results=div.getElementsByClassName('group_info');
  if(results.length === 0)
    return;
  var link=results[0].getElementsByTagName('a')[1].href.split('torrents.php?id=')[1];
  GM_xmlhttpRequest({method: "GET",
                     url: "http://159.89.252.33/torrents.php?id="+link,
                     onload: gotAlbum
                    });
}
function gotAlbum(response)
{
  var div=document.implementation.createHTMLDocument();
  div.body.innerHTML=response.responseText;
  //document.body.innerHTML=response.responseText;
  var table=div.getElementsByClassName('collage_table')[0];
  var as=table.getElementsByTagName('a');
  for(var i=0; iLink';
      return;
    }
    r=backup_similar;
  }
  else
  {
    var r=JSON.parse(response.responseText);
    if(r===null)
    {
      box.innerHTML='None found';
      return;
    }
  }
  box.innerHTML='';
  var final_artists=[];
  for(var i=0; i 20)
  {
    var p=box.previousElementSibling;
    var a=document.createElement('a');
    p.appendChild(a);
    a.setAttribute('style', 'float: right;');
    a.innerHTML='(Show more)';
    a.href='javascript:void(0);';
    a.addEventListener('click', showMore.bind(undefined, box, final_artists, a));
  }
  showSimilar(box, final_artists, false);
}
function convertName(name)
{
  return name.replace('&', 'and').toLowerCase();
}
function showMore(box, final_artists, a)
{
  if(a.textContent==='(Show more)')
  {
    a.innerHTML='(Show less)';
    showSimilar(box, final_artists, true);
  }
  else
  {
    a.innerHTML='(Show more)';
    showSimilar(box, final_artists, false);
  }
}
function showSimilar(box, final_artists, all)
{
  box.innerHTML='';
  var links=[];
  for(var i=0; i'+f+'';
    var a=document.createElement('a');
    li.appendChild(a);
    a.setAttribute('style', 'float: right;');
    a.innerHTML='(Add)';
    a.href='javascript:void(0);';
    a.addEventListener('click', addArtist.bind(undefined, a, f, false, [], 0));
    links.push({a:a, f:f});
    box.appendChild(li);
    if(i >= 19 && !all)
      break;
  }
  var li=document.createElement('li');
  box.appendChild(li);
  li.setAttribute('style', 'text-align: center;');
  var a=document.createElement('a');
  li.appendChild(a);
  a.href='javascript:void(0);';
  a.innerHTML='(Add all to similar artists)';
  a.addEventListener('click', addAll.bind(undefined, links));
}
function addAll(links)
{
  addArtist(links[0].a, links[0].f, true, links, 0);
}
function addArtist(a, f, addAll, links, index)
{
  if(a.innerHTML==='(Added)')
  {
    if(addAll)
    {
      index++;
      if(index >= links.length)
        return;
      addArtist(links[index].a, links[index].f, addAll, links, index);
    }
    return;
  }
  a.innerHTML='(Adding)';
  var inputs=document.getElementsByClassName('add_form')[0].getElementsByTagName('input');
  GM_xmlhttpRequest({method: "POST",
                     url: "/artist.php",
                     headers: {
                       "Content-Type": "application/x-www-form-urlencoded"
                     },
                     data: "action=add_similar&auth="+inputs[1].value+"&artistid="+inputs[2].value+"&artistname="+encodeURIComponent(f),
                     onload: addedArtist.bind(undefined, a, addAll, links, index)
                    });
}
function addedArtist(a, addAll, links, index, response)
{
  a.innerHTML='(Added)';
  if(!addAll)
    return;
  index++;
  if(index >= links.length)
    return;
  window.setTimeout(addArtist.bind(undefined, links[index].a, links[index].f, addAll, links, index), 1000);
}