juunykx / CSGORage Bot

// ==UserScript==
// @name         CSGORage Bot
// @description  Join automatically free raffles on csgorage.com! Inspired from mik13ST script. You need 200+ hours playing CS:GO and spend $0.50+ with raffles before running this script! Is highly reccomended that you use CSGORage.com in your Steam nickname.
// @author       JunnyKx
// @include      /^http?://.*csgorage\.com/.*$/
// @version      1.7
// ==/UserScript==

// Disable alerts on Firefox
window.alert = function() {};
addJS_Node (null, null, overrideSelectNativeJS_Functions);

// Check page errors
var time = 3000;

(function () 
{
	if (document.title == '502 Bad Gateway' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == '504 Gateway Time-out' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == 'Problem loading page' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == '503 Service Temporarily Unavailable' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == 'Service Unavailable' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == '500 Internal Server Error' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == 'Database error' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == 'FastCGI Error' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == 'The connection has timed out' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == 'Problemas al cargar la página' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.title == 'Error 502 (Server Error)!!1' )
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.getElementsByTagName('h1')[0].innerHTML == '502 Bad Gateway')
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.getElementsByTagName('h1')[0].innerHTML == 'Service Unavailable')
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.getElementsByTagName('h1')[0].innerHTML == 'Error 503 Service Unavailable')
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.getElementsByTagName('h1')[0].innerHTML == '404 Not Found')
		{setTimeout(function() {window.location.reload(true);}, time);}
	else if (document.getElementsByTagName('h1')[0].innerHTML == '504 Gateway Time-out')
		{setTimeout(function() {window.location.reload(true);}, time);}
})();

// Disable alerts on Chrome and Opera
function overrideSelectNativeJS_Functions () {
    window.alert = function alert (message) {
        console.log (message);
    };
}

function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

// Check if is logged in
if ($(".logged_in").length === 0) {
  
  // Display countdown info
  document.body.innerHTML+='<div id="info" style="opacity:.90;font-family: Verdana, Geneva, sans-serif;z-index: 10000;position: fixed;left: 20px;bottom: 10px;background-color: red;width: 160px;text-align: center;border-radius: 5px; padding:5px; display: flex;align-items: center;justify-content: center; font-size: 12px;">Please log in first!</div>';
  
} else {
  
// Verify if is a raffle page
var p = window.location.pathname;

if ( p.includes('/raffles') || p.includes('/free-raffles') || p.includes('/raffle') || p.includes('/special-raffles') ){

// Vars
var raffles = document.getElementsByClassName("raffle_box_lg"), btns = document.getElementsByClassName("btn btn-warning orange_gradient shadow1");
var cRaffles = 0, cPaid = 0, cFree = 0, cIn = 0;

// Jquery Ready Function
jQuery(document).ready(function () {
  
  // Check path
  if (window.location.pathname.includes("/free-raffles")){
    
    // Start coutdown
    var t = 30;
    (function countDown(){
      if (t-->0) {
        $('#time').text('Refresh in ' + t + ' s.');
        setTimeout(countDown, 1000);
       } else {
         $('#time').text('Gone!');
         setTimeout(function(){window.location.assign(window.location.href);}, 1);
       }
    })();
    
    // Display countdown info
    document.body.innerHTML+='<div id="time" style="opacity:.90;font-family: Verdana, Geneva, sans-serif;z-index: 10000;position: fixed;left: 20px;bottom: 10px;background-color: red;width: 160px;text-align: center;border-radius: 5px; padding:5px; display: flex;align-items: center;justify-content: center; font-size: 12px;">Refresh in...</div>';
    
  }
  
});

// Check if is a raffle page
if (window.location.pathname.includes("/raffle")){
  
  var raffle = document.getElementsByClassName("skin_info");
  
  // Count Raffles
  for(var z = 0; z < raffle.length; z++){
    
    // Check if you're in
    if (raffle[z].innerHTML.indexOf("You are in!") == -1){

      setTimeout(function () {
        document.getElementById("randomize").click();
      }, 2000);
      setTimeout(function () {
        document.getElementById("getrandomslot").click();
      }, 3000);
      setTimeout(function () {
        window.location.assign("http://csgorage.com/free-raffles/current");
      }, 4000);
      
    } else {
      
      // Countdown (((AGAIN)))
      jQuery(document).ready(function () {
      var t2 = 60;
      (function countDown(){
        if (t2-->0) {
            $('#time2').text('You are already participating in this raffle! Returning in ' + t2 + ' s.');
            setTimeout(countDown, 1000);
           } else {
             $('#time2').text('Gone!');
             setTimeout(function(){window.location.assign("http://csgorage.com/free-raffles/current");}, 1);
           }
        })();
       document.body.innerHTML+='<div id="time2" style="opacity:.90;font-family: Verdana, Geneva, sans-serif;z-index: 10000;position: fixed;right: 20px;top: 10px;background-color: rgb(74,161,226);width: 160px;text-align: center;border-radius: 5px; padding:5px; display: flex;align-items: center;justify-content: center; font-size: 12px;">You are already participating in this raffle!</div>';
        });
    }
  }
  
}

// Check raffles
for(var i = 0; i < raffles.length; i++){
  
  cRaffles++;
  
  if (raffles[i].innerHTML.indexOf("You're in!") > -1){
    cIn++;
  }
  
}

// Paid buttons count
for(var x = 0; x < btns.length; x++){
  if (btns[x].innerHTML.indexOf('Buy') > -1){
    
    cPaid++;
    
  } else {
    
    if (raffles[x].innerHTML.indexOf("You're in!") == -1) {
        btns[x].click();
    }
    
    cFree++;
    
  }
}

// Log in alert
document.body.innerHTML+='<div id="info" style="opacity:.90;font-family: Verdana, Geneva, sans-serif;z-index: 10000;position: fixed;left: 20px;top: 10px;background-color: green;width: 160px;text-align: left;border-radius: 5px; padding:15px; display: flex;align-items: center; font-size: 12px;"><br>RAFFLES<br>Total: ' + cRaffles + '<br>Paid: ' + cPaid + '<br>Free: ' + cFree + '<br>In: ' + cIn + '<br></div>';

} else {
  // Start coutdown
    var t = 60;
    (function countDown(){
      if (t-->0) {
        $('#timeHome').text("I've nothing to do here... Getting out in " + t + " s.");
        setTimeout(countDown, 1000);
       } else {
         $('#timeHome').text('Gone!');
         setTimeout(function(){window.location.assign('http://csgorage.com/free-raffles/current');}, 1);
       }
    })();
    
    // Display countdown info
    document.body.innerHTML+='<div id="timeHome" style="opacity:.90;font-family: Verdana, Geneva, sans-serif;z-index: 10000;position: fixed;left: 20px;top: 10px;background-color: rgb(74,161,226);width: 160px;text-align: center;border-radius: 5px; padding:5px; display: flex;align-items: center;justify-content: center; font-size: 12px;">I\'ve nothing to do here...</div>';
}
}