Advertisement
PunasaurusRex

elescript

Jul 19th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. // ==UserScript==
  2. // @id steamlove
  3. // @name steamlove
  4. // @version 4
  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.  
  16. // static parameters
  17. var eleg_galist="http://steamcommunity.com/groups/EverybodyLEG/discussions/0/864972399765669380/";
  18.  
  19. //prepare globals
  20. var bundle_list = [];
  21. var ga_list = [];
  22. var ga = [];
  23. var ga_count = 0;
  24. var ga_progress = 0;
  25. var currentDate = new Date().getTime();
  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('test').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 = parseInt($('.featured__column .fa-clock-o', page).parent().find('span:last').attr('data-timestamp'))*1000;
  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 < currentDate) {
  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. alert(e);
  162. }
  163. return g;
  164. }
  165.  
  166. // check if all GA were parsed, and if yes, call elegCheckDone().
  167. function parseGaDone() {
  168. ga_progress++;
  169. $('#progress').val(ga_progress);
  170. if (ga_progress == ga_count) {
  171. $('#progress').remove();
  172. elegCheckDone();
  173. }
  174. }
  175.  
  176. // display the parse results
  177. function elegCheckDone() {
  178. $('#eleg').append('<h4>Result : </h4>');
  179. var obj = $('<table/>');
  180. var tr = $('<tr/>');
  181. $(tr)
  182. .append($('<th/>').text('link'))
  183. .append($('<th/>').text('title'))
  184. .append($('<th/>').text('giver'))
  185. .append($('<th/>').text('cv'))
  186. .append($('<th/>').text('status/winner'))
  187. .append($('<th/>').text('B'))
  188. .append($('<th/>').text('NB'))
  189. .append($('<th/>').text('entries'))
  190. .append($('<th/>').text('date'))
  191. ;
  192. $(obj).append(tr);
  193. for (i in ga) {
  194. var g = ga[i];
  195. var tr = $('<tr/>');
  196. var ga_url='https://www.steamgifts.com/giveaway/' + g.id +'/';
  197. $(tr)
  198. .append($('<td/>').html('<a href="'+ga_url+'">'+ga_url+'</a>'))
  199. .append($('<td/>').text(g.title + ','))
  200. .append($('<td/>').text(g.giver))
  201. .append($('<td/>').text(g.is_bundle ? 0 : g.cv))
  202. .append($('<td/>').text(g.status == 'CLOSED' ? g.winner : g.status))
  203. .append($('<td/>').text('?')) // unknow in SG v2
  204. .append($('<td/>').text('?')) // unknow in SG v2
  205. .append($('<td/>').text(g.entries))
  206. .append($('<td/>').text(formatDate(new Date(g.date))))
  207. ;
  208. $(obj).append(tr);
  209. }
  210. GM_addStyle("#eleg table td{border:1px solid #333}");
  211. $('#eleg').append(obj);
  212. }
  213.  
  214. // format date
  215. function formatDate(d) {
  216. if(null !== d && typeof d !== 'undefined') {
  217. return d.getUTCFullYear() + "/" + (d.getUTCMonth()+1) + "/" + d.getUTCDate() + " " + d.getUTCHours() + ":" + d.getUTCMinutes();
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement