Advertisement
EyEBURNeR

steamlove v 3.01

Apr 29th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 8.02 KB | None | 0 0
  1. // ==UserScript==
  2. // @id             steamlove
  3. // @name           steamlove
  4. // @version        3.01
  5. // @namespace
  6. // @author         http://www.steamgifts.com/user/EyEBURNeR
  7. // @description    giveaway management helper. Rewritten and optimized, tested on FF but now should also works in Chrome.
  8. // @include        https://www.steamgifts.com/*
  9. // @require        https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
  10. // @grant          GM_xmlhttpRequest
  11. // @grant          GM_addStyle
  12. // @run-at         document-end
  13. // ==/UserScript==
  14.  
  15. // http://pastebin.com/1GgePS99
  16.  
  17. // static parameters
  18. var eleg_galist="http://steamcommunity.com/groups/EverybodyLEG/discussions/0/864972399765669380/";
  19.  
  20. //prepare globals
  21. var bundle_list = [];
  22. var ga_list = [];
  23. var ga = [];
  24. var ga_count = 0;
  25. var ga_progress = 0;
  26.  
  27. // add eleg button to SG menu
  28. $('.nav__left-container:first').append('<div class="nav__button-container"><a id="eleg_ftw" class="nav__button" href="#">ELEG</a></div>');
  29.  
  30. // init ELEG GA list screen, automatically load the list from ELEG forum
  31. $('nav:first').on('click', '#eleg_ftw', function () {
  32.     $('.featured__container').remove();
  33.     var box = $('.page__inner-wrap:first');
  34.     $(box).html('').attr('id', 'eleg');
  35.     $('.notification', '#eleg').remove();
  36.     document.title = 'Steam Gifts Eleg Management';
  37.     $('#eleg').html('<div style="height:100px !important;overflow:hidden">Source code from <a href="'+eleg_galist+'" target="_blank">'+eleg_galist+'</a><textarea id="source" style="width:100%">Please wait, trying to automatically load the GA list from steam…</textarea></div>');
  38.     GM_xmlhttpRequest({
  39.         method: "GET",
  40.         url: eleg_galist ,
  41.         onload: function(response) {
  42.             var source=optimise_raw_html(response.responseText);
  43.             if(source.indexOf('An error was encountered while')==-1){
  44.                 $('#source').val(source);
  45.             }else{
  46.                 $('#source').val('Could not retreive the source automatically, please manually paste it from:\n\n '+eleg_galist);
  47.             }
  48.             $('#eleg').append($('<a class="featured__action-button" href="#"><i class="fa fa-arrow-circle-right"></i> Check</a>').click(elegCheckGaList));
  49.         }
  50.  
  51.     });
  52. });
  53.  
  54. // optimise string to be parsed
  55. function optimise_raw_html(page){
  56.     return page.replace(/<img[^>]+>/ig,'')
  57. }
  58.  
  59. // parse source code to extract sg ga urls, will call populateGa() for each GA found
  60. function elegCheckGaList() {
  61.     $(this).after('<progress id="progress"></progress>');
  62.     $(this).remove();
  63.     var list=optimise_raw_html($('#source').val());
  64.     if( $('.forum_op .content',    list).length>0){
  65.         list=$('.forum_op .content',    list); // the whole is available, keep only the op content
  66.     }else{
  67.         list='<b>'+list+'</b>'; // make it a valid DOM object
  68.     }
  69.     // find all GA urls
  70.     $('a[href*="/giveaway/"]', list).each(function(){
  71.         ga_list.push($(this).attr('href'));
  72.     });
  73.     for(i in ga_list){
  74.        ga[i]={'href':ga_list[i]}
  75.     }
  76.     // keep track of the # of GA to be loaded and parsed
  77.     ga_count=ga.length;
  78.     $('#progress').attr('max',ga_count);
  79.     //getBundleList(); still no easy way to get the list in SG v2.
  80.     //begin the loading and parsing
  81.     for(i=0;i<ga_count;i++){
  82.         populateGa(i);
  83.     }
  84. }
  85.  
  86. // background loading of a GA page, will call parseGa() and parseGADone()
  87. function populateGa(gaIdx) {
  88.     var url = ga[gaIdx].href;
  89.     // get only the sg GA code and build the short url
  90.     url = '/giveaway/'+url.substring(url.indexOf('url=') + 4).split('//www.steamgifts.com/giveaway/')[1].split('/')[0]+'/';
  91.     $.ajax({
  92.         url: url,
  93.         contentType: 'text/plain',
  94.         success: function (page) {
  95.             ga[gaIdx]= $.extend(ga[gaIdx], parseGa(page, url));
  96.             parseGaDone();
  97.         },
  98.         dataType: 'html'
  99.     });
  100. }
  101.  
  102. // parse a loaded GA page
  103. function parseGa(page, url) {
  104.     var g = {};
  105.     try {
  106.         var winners = [];
  107.         page=optimise_raw_html(page);
  108.         g.title = $.trim($('.featured__heading__medium:first', page).text());
  109.         g.giver = $('.featured__column a[href*="/user/"]:first', page).text();
  110.         g.winner = '';
  111.         g.winners = [];
  112.         g.status = '';
  113.         g.is_failed = false;
  114.         g.is_open = false;
  115.         g.is_closed = false;
  116.         g.is_pending = false;
  117.         g.date = $('.featured__column .fa-clock-o', page).parent().find('span:last').text();
  118.         g.entries = parseInt($('.live__entry-count', page).text());
  119.         g.id = url.split('/giveaway/')[1].split('/')[0];
  120.         g.cv = $('.featured__heading>div.featured__heading__small:last', page).text().split('(')[1].split('P')[0];
  121.         if (g.date.indexOf('Ended ') > -1) {
  122.             urlwinners = $('.sidebar__navigation__item__link[href*="/winners"]', page).attr('href');
  123.             var wdiv;
  124.             $.ajax({
  125.                 url: urlwinners,
  126.                 async: false,
  127.                 success: function (wpage) {
  128.                 wpage=optimise_raw_html(wpage);
  129.                 wdiv = $('.page__inner-wrap', wpage);
  130.             },
  131.                    dataType: 'html'
  132.                    });
  133.  
  134.             g.is_closed = true;
  135.             g.status = 'CLOSED';
  136.             var received = $('.table__row-inner-wrap', wdiv);
  137.             if (received.length) {
  138.                 $(received).each(function () {
  139.                     if ($(this).closest('.table__row-outer-wrap').find('.fa-check-circle').length) {
  140.                         winners.push($('a[href*="/user/"]',this).text());
  141.                     } else if($(this).closest('.table__row-outer-wrap').find('.fa-question-circle').length)  {
  142.                         winners.push('PENDING');
  143.                         g.is_pending = true;
  144.                         g.status = 'PENDING';
  145.                     }else {
  146.                         winners.push('NOT RECEIVED');
  147.                     }
  148.                 });
  149.             } else {
  150.                 winners.push('FAILED');
  151.                 g.is_failed = true;
  152.                 g.status = 'FAILED';
  153.             }
  154.             g.winners = winners;
  155.             g.winner = winners.join(',');
  156.         } else {
  157.             g.is_open = true;
  158.             g.status = 'OPEN';
  159.         }
  160.     } catch (e) {
  161.        console.log(e);
  162.        g.title='ERROR - please check if GA is valid';
  163.     }
  164.     return g;
  165. }
  166.  
  167. // check if all GA were parsed, and if yes, call elegCheckDone().
  168. function parseGaDone() {
  169.     ga_progress++;
  170.     $('#progress').val(ga_progress);
  171.     if (ga_progress == ga_count) {
  172.         $('#progress').remove();
  173.         elegCheckDone();
  174.     }
  175. }
  176.  
  177. // display the parse results
  178. function elegCheckDone() {
  179.     $('#eleg').append('<h4>Result : </h4>');
  180.     var obj = $('<table/>').css('width','1200px');
  181.     var tr = $('<tr/>');
  182.     $(tr)
  183.         .append($('<th/>').text('link'))
  184.         .append($('<th/>').text('title'))
  185.         .append($('<th/>').text('giver'))
  186.         .append($('<th/>').text('cv'))
  187.         .append($('<th/>').text('status/winner'))
  188.         .append($('<th/>').text('B'))
  189.         .append($('<th/>').text('NB'))
  190.         .append($('<th/>').text('entries'))
  191.         .append($('<th/>').text('date'))
  192.     ;
  193.     $(obj).append(tr);
  194.     for (i in ga) {
  195.         var g = ga[i];
  196.         var tr = $('<tr/>');
  197.         var ga_url='https://www.steamgifts.com/giveaway/' + g.id +'/';
  198.         $(tr)
  199.             .append($('<td/>').html('<a href="'+ga_url+'">'+ga_url+'</a>'))
  200.             .append($('<td/>').text(g.title + ','))
  201.             .append($('<td/>').text(g.giver))
  202.             .append($('<td/>').text(g.is_bundle ? 0 : g.cv))
  203.             .append($('<td/>').text(g.status == 'CLOSED' ? g.winner : g.status))
  204.             .append($('<td/>').text('?'))  // unknow in SG v2
  205.             .append($('<td/>').text('?'))  // unknow in SG v2
  206.             .append($('<td/>').text(g.entries))
  207.             .append($('<td/>').text(g.date))
  208.         ;
  209.         $(obj).append(tr);
  210.     }
  211.     GM_addStyle("#eleg table td{border:1px solid #333}");
  212.     $('#eleg').append(obj);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement