coyoteazul / CoAzSelector

// ==UserScript==
// @name        CoAzSelector
// @namespace   CoAzSelector
// @include     https://www.neobux.com/*
// @version     1.4
// @grant       none
// @copyright   2016
// @description Creates a writeable area on neobux's RR page. It autoselects the RRs you write there
// @updateURL   https://openuserjs.org/meta/coyoteazul/CoAzSelector.meta.js
// @license MIT
// ==/UserScript==

//Nota: este es un projecto abandonado. Cualquier persona que quiera heredarlo y/o modificarlo sientase libre de hacerlo

// 1.1: Scrolls down if a referral is found
// 1.2: Uses local storage to paste referrals on all open windows instead of making the user paste them window by window
// 1.3: Changed settings to it doesn't load the back up while you are editing the text area
// 1.4: Reduced system load. Now it only walks through the list when a change on textarea or localstorage is detected, instead of walking through the list on every localstorage check


function buscarRR (nombreRR){
  var tabla = document.getElementById('tblprp').getElementsByTagName('table') [2].childNodes[0];
  var fila = tabla.rows;

  for (i = 2; i <= fila.length - 3; i = i + 2){
    var nombreEnFila = fila[i].childNodes[2].innerHTML;
    var puntoDeCorte = nombreEnFila.indexOf('&nbsp');
    nombreEnFila = nombreEnFila.slice(0, puntoDeCorte);

    if (nombreEnFila === nombreRR){
      fila[i].getElementsByTagName("input")[0].click();
      window.scrollTo(0,document.body.scrollHeight);
      break;
    }
  }
}

function recorreLista(){
  console.log("Contador")
  var DOMReiniciador = document.getElementById("jjsa"); //reinicia las marcas
  DOMReiniciador.click();
  DOMReiniciador.click();
  
  if (!localStorage.getItem('selectRR') || localStorage.getItem('selectRR') != document.getElementById("CoAzTextArea").value){
    localStorage.setItem('selectRR', document.getElementById("CoAzTextArea").value)
    setTimeout(function(){
      localStorage.removeItem('selectRR');
    }, 550000);
  }
  var lineas = document.getElementById("CoAzTextArea").value.split("\n");
  var referido = lineas[0];
  var i = 0;

  while(referido !== undefined){
    var espacio = referido.indexOf(" ",0);
    if (espacio != -1){
      referido = referido.substring(0, espacio);
      console.log(referido);
    }

    buscarRR(referido);

    i++;
    referido = lineas[i];
  }
}

function checkStorage(){
  if (localStorage.getItem('selectRR')){
    if (document.getElementById("CoAzTextArea") === document.activeElement)
      {}
    else {
      var store = localStorage.getItem('selectRR');
      if (store != document.getElementById("CoAzTextArea").value){
          document.getElementById("CoAzTextArea").value = store;
          recorreLista();
      }
    }
  }
}

function crearTextArea(){
  var DOMParaText = document.getElementById('menu_w');

  var DOMDeLista = document.createElement('textarea');
  DOMDeLista.setAttribute('id', 'CoAzTextArea');
  DOMDeLista.addEventListener("change", recorreLista);
  DOMDeLista.style.width = '155px';

  if (document.URL.indexOf("ss3=2") != -1){
   DOMParaText.appendChild(DOMDeLista);
   setInterval(function () {checkStorage()}, 5000)
  }
}

crearTextArea();