// Extension for Chrome by Prohorov Yuri e-mail: star716@mail.ru  resume : http://cosnat.com.ru/resume 
// var fnar = [ 22, 941, 1666, 187, 2090, 2221, 2091, 2092, 2093, 2200, 1950, 2540, 905, 101, 100, 877, 2199, 313, 312, 1247, 2343, 930, 1900, 521, 2258, 208, 539, 209, 484, 9, 235, 1102, 489, 387, 2366, 1803, 266, 193, 1690, 1459, 1463, 825, 1248, 1288 ];


var fnar = [ 22, 313, 9, 489, 2540, 930, 539, 209, 1950, 2200, 2093, 2092, 2091, 2221 ];

var un1="";
var un2="";

var fn = 22;
var fs =   0;
var fnai = 0;

var sendi = 0;
var pnum = 0;
var oseed = 0;
var vstop = 0;

var vsf = [];
var rta = [];

var cods;

chrome.runtime.onMessage.addListener(function(request, sender) {
  if (request.action == "getSource") {


     if( vstop > 0 ) return;


     mo.ars(request.source);

     pnum++;

     log("mess getSource oseed = " + oseed + " pnum = " + pnum + "  fnai = " + fnai + "  mo.tvc = " + mo.tvc + "  mo.hnext  " + mo.hnext + "  mo.hnext len  " + mo.hnext.length );


 let st = "fs-" + fnar[fnai] + "\".{1,170}nb";

 let reg = new RegExp(st, "g");

 let hx = mo.sth.match(reg);

 let fsv = hx.toString().indexOf(">");

 st = "Не определено";

 if( fsv > 0 )  st = hx.toString().substring(fsv+1, hx.toString().length-3);

 prtRes(st, pnum, mo.tvc);


    if(  mo.tvc > 350 )
      {

         onBt1();

         return;

      }



//  alert(hx+"   st  "+st);                                                                    

//    log("mess getSource topic = " + st + " fsv = " + fsv + "   hx = " + hx.toString()  );

    if( ( pnum < 3 ) && ( mo.hnext.length > 2 ) )
     {

        chUrl("https://rutracker.org/forum/"+mo.hnext);

     }  
    else
     {

      if( fnai < 13 )
       {


       pnum=0;
       oseed=0;
       fnai++;

       un1 = "https://rutracker.org/forum/tracker.php?f="+fnar[fnai];

       chUrl(un1);

        }

     }


  }


  if (request.action == "loadPage") {


//         alert("loadPage ? apage.html");


    if(  request.source.indexOf("firt.html") > 0 )
      {



         onBt3();

         return;

      }


//     alert(request.source);

//     return;

     log("mess loadPage" );

     if( oseed == 1 ) 
      {  
//        sendi = 1;

  chrome.tabs.executeScript(null, {
    file: "getPagesSource.js"
  }, function() {
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error
    if (chrome.runtime.lastError) {
       log("ERROR getPagesSource.js");
    }
  });
     
        return;
      }
     


     if( oseed == 0 ) 
      {  

        selPg();

        oseed = 1;
      }


 
  }




  if (request.action == "sendHref") {

      log("mess sendRef");
      
      return;


     if( sendi == 1 ) sendRef();  

/*
     fn = fnar[fnai];

    message.innerText = "\nhc = " + hc  + "  fn = " + fn  + "  dn = " + dn + "\n"  + message.innerText;

    if( dn.length > 1 )
     {

       un1 = "https://rutracker.org/forum/tracker.php?f="+fn+"&start="+dn;

//       setTimeout(function(){ chUrl( un1 ); }, 2000);
      }
     else
      {
        if( fnai < 44 )
         {
           fnai++;
           un1 = "https://rutracker.org/forum/tracker.php?f="+fnar[fnai];

         }

      }

*/

  }



});

function onBt1() {

//  chUrl("apage.html");
  chUrl("http://cosnat.com.ru/resume/chrome/firt.html");



 vstop = 1;

}





function log(st) {

   message.innerText = st;
//   message.innerText = st + "\n" + message.innerText;

}

function prtRes(st, pn, fn) {

   rsout.innerHTML = "<font size=\"+1\">Раздел</font>  " + "<font size=\"+1\" color=\"0000FF\">" + st + "</font>" 
               + "    <font size=\"+1\">Страниц</font>  " + "<font size=\"+1\" color=\"0000FF\">" + pn + "</font>"
               + "    <font size=\"+1\">Фильмов</font>  " + "<font size=\"+1\" color=\"0000FF\">" + fn + "</font>";

}


function cN(a, b) {
  return a.seeds - b.seeds;
}



function onBt3() {


   cods = 'document.getElementById("tbc").innerHTML = "<table border=\'1\'><tr><td>№</td><td>Название</td><td>Seeds</td></tr>';


  let s1, i, k=0;

  mo.tv.sort( function(x,y) { if(x.seeds>y.seeds) return -1; if(x.seeds<y.seeds) return 1; return 0; });


 mo.tv.forEach( function(item) {

       k++;

       if( cods.length < 16000 )
        {

       try {
        s1 = "<tr><td class='n'>" + k + '</td><td>'+item.naim.replace('"','')+"</td><td class='s'>"+item.seeds+'</td></tr>';
        cods = cods + s1;
           }
        catch(e){

           }

        }

});



  cods = cods + '</table>";';

  
//  alert(cods);
//  alert('Pause');



  chrome.tabs.executeScript(null, {
   code: cods

  }, function() {
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error
    if (chrome.runtime.lastError) {
       alert("Error");
    }
  });





}





function onBt2() {


   oseed = 0;

   pnum = 0;


  un1 = "https://rutracker.org/forum/tracker.php?f="+fnar[fnai];

  chUrl(un1);


}








function chUrl(stURL)
{

var murl = new Object();

murl.url =  stURL;

chrome.tabs.query({currentWindow: true, active: true}, function (tab) {
      chrome.tabs.update(tab.id, murl);
});

}

function tmProc()
{
    chUrl("https://rutracker.org/forum/viewtopic.php?t="+tar[tari]);

    message.innerText = message.innerText + "\ntari = " + tari + "   tar =  " + tar[tari] + "\n";

    tari++;

    if( tari < 3 ) 
     {
     setTimeout(tmProc, 5000);
     }
    else
     {
    chUrl("https://rutracker.org/forum/tracker.php?search_id=5WYA5Kvcsuk7&start=150");
     }

}



function memo(sn,st)
{

   message.innerText = sn + "  " + st + '\n' + message.innerText;


}

function selPg()
{

  var cods = 'document.getElementById("o").value = "10"; document.getElementById("fs-main").value = "' + fnar[fnai] + '"; document.getElementById("tr-form").submit();';

  chrome.tabs.executeScript(null, {
//   file: "pageinj.js"
   code: cods

  }, function() {
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error
    if (chrome.runtime.lastError) {
//       alert("pageinj.js");
    }
  });

}


function onWindowLoad() {

  var message = document.querySelector('#message');

  var rsout = document.querySelector('#rsout');


/*

  chrome.tabs.executeScript(null, {
    file: "getPagesSource.js"
  }, function() {
    // If you try and inject into an extensions page or the webstore/NTP you'll get an error
    if (chrome.runtime.lastError) {
      message.innerText = 'There was an error injecting script : \n' + chrome.runtime.lastError.message;
    }
  });

*/
}

 window.onload = onWindowLoad;

 document.getElementById("bt1").addEventListener("click", onBt1);
 document.getElementById("bt2").addEventListener("click", onBt2);
//  document.getElementById("bt3").addEventListener("click", onBt3);
