SGTools Giveaways Helper (Custom by Barokai)

Makes your life easier!

  1. // ==UserScript==
  2. // @name SGTools Giveaways Helper (Custom by Barokai)
  3. // @icon https://cdn.steamgifts.com/img/favicon.ico
  4. // @namespace *://www.sgtools.info/
  5. // @version 1.7.7
  6. // @description Makes your life easier!
  7. // @author Barokai | www.loigistal.at (Enhanced version of KnSYS which is based on a work from Mole & Archi. See below)
  8. // @description Enhanced create giveaway feature - added 3 buttons for 3 giveaway groups (BundleQuest, RPGTreasury, Unlucky-7) which will be chosen automatically on click.
  9. // @homepage https://github.com/Barokai/sgtools-giveaways-helper-custom/
  10. // @license https://github.com/Barokai/sgtools-giveaways-helper-custom/blob/master/LICENSE
  11. // @match *://www.steamgifts.com/*
  12. // @grant GM_addStyle
  13. // @run-at document-end
  14. // ==/UserScript==
  15. /* Based on SGTools Giveaways Helper by KnSYS
  16. * which is
  17. * Based on Touhou Giveaways Helper
  18. * https://github.com/Aareksio/touhou-giveaways-helper
  19. * authors Mole & Archi
  20. *
  21. * MIT License
  22. */
  23.  
  24. /* TODO Barokai:
  25. * set default duration per group
  26. * set default description per group (eg. unlucky-7: open for gifters or not, bundlequest: free/discount/premium,..)
  27. */
  28.  
  29. (function() {
  30. 'use strict';
  31.  
  32. var SGTOOLS_SITE = 'https://www.sgtools.info/';
  33. var SGTOOLS_TIME = 2 * 24 * 60 * 60 * 1000; // 2 days recommended
  34.  
  35. var DEFAULT_LEVEL = 0;
  36. var DEFAULT_DESCRIPTION = "**Good Luck!**";
  37. var DISCLAIMER = "\n\n**Please notice**:\nThere is a very small chance that the key was already redeemed (happens if i forget to note that) - if thats the case, write a comment/message and I’ll send you a new key/steamgift!";
  38. var GROUPS = {
  39. "none": {
  40. name: "",
  41. level: 1,
  42. description: DEFAULT_DESCRIPTION + DISCLAIMER
  43. },
  44. "boxofkittens": {
  45. name: "Box of Kittens!",
  46. level: DEFAULT_LEVEL,
  47. description: DEFAULT_DESCRIPTION + "\n\nComment with a funny gif ;)" + DISCLAIMER
  48. },
  49. "bundlequest": {
  50. name: "Bundle Quest",
  51. level: DEFAULT_LEVEL,
  52. description: DEFAULT_DESCRIPTION + "\n\n*This Giveaway is a normal/free/XX% discount giveaway.*" + DISCLAIMER
  53. },
  54. "rpgtreasury": {
  55. name: "RPG Treasury",
  56. level: DEFAULT_LEVEL,
  57. description: DEFAULT_DESCRIPTION + DISCLAIMER
  58. },
  59. "unlucky7": {
  60. name: "Unlucky-7",
  61. level: DEFAULT_LEVEL,
  62. description: DEFAULT_DESCRIPTION + "\n\nOpen for gifters, but please make sure that your [gift difference](http://i.imgur.com/69WNFSw.png) is above 7.0 or higher.\nYou can check yours [here](https://www.steamgifts.com/group/WWF2y/unlucky-7/users)." + DISCLAIMER
  63. }
  64. };
  65.  
  66. var current_path;
  67. if (/steamgifts\.com/.exec(window.location.href)) {
  68. current_path = window.location.pathname.split('/');
  69. removeFromArray(current_path, "");
  70. }
  71.  
  72. if (current_path) {
  73. if (current_path.length !== 0 && current_path[0] === 'giveaways' && current_path[1] === 'new') {
  74. addStyles();
  75. giveawayNew();
  76. } else if (current_path.length !== 0 && current_path[0] === 'giveaway') {
  77. var id = current_path[1];
  78. console.log(id);
  79. getProtectedStatus(id);
  80. }
  81. }
  82.  
  83. function getProtectedStatus(id) {
  84. var isInviteOnly = $('.featured__column--invite-only').length;
  85. if (isInviteOnly) {
  86. var url = SGTOOLS_SITE + 'api/isSGTProtected/' + id;
  87. $.getJSON(url + "?callback=?", function(result) {
  88. var isProtected = result["protected"];
  89. var toAppendValid = $(".sidebar__entry-insert");
  90. var toAppendError = $(".sidebar__error");
  91. if (isProtected) {
  92. toAppendValid.html('<i class="fa fa-lock"></i> Enter SGTools Protected Giveaway');
  93. toAppendError.html('<i class="fa fa-lock"></i> ' + toAppendError.html());
  94. toAppendValid.css({
  95. 'border-color': "#93BBD3 #699DBC #427BA4 #70ACC8",
  96. 'background-image': 'linear-gradient(#A7D1EE 0%, #8AC4DF 50%, #6AA2C9 100%)',
  97. 'color': 'inherit'
  98. });
  99. } else {
  100. toAppendValid.html('<i class="fa fa-unlock"></i> ' + toAppendValid.html());
  101. }
  102. });
  103. }
  104. }
  105.  
  106. function giveawayNew() {
  107. let getGroupButtons = function() {
  108. var buttonMarkup = "";
  109. $.each(GROUPS, function(key, g) {
  110. //var groupname = g.name !== "" ? " for " + g.name : "";
  111. var groupname = g.name || "default";
  112. var icon = groupname !== "default" ? "group" : "gift";
  113. var id = "sgToolsBtn_" + key;
  114. //buttonMarkup += ' <div class="form__row__indent"><div class="form__submit-button" id="' + id + '"><i class="fa fa-fast-forward"></i>&nbsp;Fill default settings' + groupname + '</div>&nbsp;</div>';
  115. buttonMarkup += ' <div class="form__submit-button" id="' + id + '"><i class="fa fa-'+ icon +'"></i>&nbsp;' + groupname + '</div>&nbsp;';
  116. });
  117. return buttonMarkup;
  118. };
  119.  
  120. let bindOnClick = function() {
  121. $.each(GROUPS, function(key, group) {
  122. var id = "sgToolsBtn_" + key;
  123. $("#" + id).click(function() {
  124. applySettings(group);
  125. console.log("clicked " + (group.name || "default" ) + " button");
  126. });
  127. });
  128. };
  129.  
  130. $(".form__rows").prepend(
  131. '<div class="form__row form__row--sgtools-giveaway-helper">' +
  132. ' <div class="form__heading">' +
  133. ' <div class="form__heading__number">0.</div>' +
  134. ' <div class="form__heading__text">SGTools Giveaway</div>' +
  135. ' </div>' +
  136. getGroupButtons() +
  137. '</div>');
  138. bindOnClick();
  139.  
  140. let applyGiveawayTypeKey = function() {
  141. var privateButton = $("div[data-checkbox-value='key']");
  142. if (!privateButton.hasClass('is-selected')) {
  143. privateButton.trigger("click");
  144. }
  145. };
  146.  
  147. let applyDates = function() {
  148. let startingDate = new Date();
  149. let endingDate = new Date(startingDate.getTime() + SGTOOLS_TIME + (60 * 60 * 1000)); // Extra 1 hour
  150. $("input[name='start_time']").val(formatDate(startingDate));
  151. $("input[name='end_time']").val(formatDate(endingDate));
  152. };
  153.  
  154. let applyRegionRestrictions = function() {
  155. $("div[data-checkbox-value='0']").trigger("click");
  156. };
  157.  
  158. var applyPrivate = function(group) {
  159. if (group.name) {
  160. var privateButton = $("div[data-checkbox-value='invite_only']");
  161. if (!privateButton.hasClass('is-selected')) {
  162. privateButton.trigger("click");
  163. }
  164. }
  165. };
  166.  
  167. let scrollToGroup = function(group) {
  168. var container = $('.form__groups'),
  169. scrollTo = group;
  170.  
  171. container.scrollTop(
  172. scrollTo.offset().top - container.offset().top + container.scrollTop()
  173. );
  174. };
  175.  
  176. let applyGroup = function(group) {
  177. if (group.name) {
  178. var privateButton = $("div[data-checkbox-value='groups']");
  179. if (!privateButton.hasClass('is-selected')) {
  180. privateButton.trigger("click");
  181. }
  182.  
  183. var group_form = $('div.form__group__details > div.form__group__name:contains(' + group.name + ')');
  184. group_form.click();
  185. scrollToGroup(group_form);
  186. }
  187. };
  188.  
  189. let applyDescription = function(group) {
  190. let descarea = $("textarea[name='description']");
  191. descarea.val(group.description);
  192. };
  193.  
  194. let applyLevel = function(group) {
  195. let levelValue = $("input[name='contributor_level']");
  196. levelValue.val(group.level);
  197. $(".ui-slider-range").css('width', group.level * 10 + "%");
  198. $(".ui-slider-handle").css('left', group.level * 10 + "%");
  199. if (group.level > 0) {
  200. $(".form__input-description--level").removeClass("is-hidden");
  201. $(".form__input-description--no-level").addClass("is-hidden");
  202. $("span.form__level").text("level " + group.level);
  203. } else {
  204. $(".form__input-description--no-level").removeClass("is-hidden");
  205. $(".form__input-description--level").addClass("is-hidden");
  206. }
  207.  
  208. };
  209.  
  210. let reset = function() {
  211. $(".form__checkbox.is-disabled").show();
  212. $('div.form__group.form__group--steam.is-selected').click(); // deselect all previously selected groups
  213. $("div[data-checkbox-value='everyone']").click(); // set "who can enter" to everyone
  214. var group = {
  215. level: 0
  216. };
  217. applyLevel(group); // reset level to 0
  218. console.log("resetted checkboxes/groups/level");
  219. };
  220.  
  221. let applySettings = function(group) {
  222. reset();
  223. applyGiveawayTypeKey();
  224. applyDates();
  225. applyRegionRestrictions();
  226. applyPrivate(group);
  227. applyDescription(group);
  228. applyLevel(group);
  229. applyGroup(group);
  230. // hides not used checkboxes
  231. $(".form__checkbox.is-disabled").hide();
  232. // focus input to enter game name
  233. $(".js__autocomplete-name").focus();
  234. };
  235. }
  236.  
  237. /* Helpers */
  238. function removeFromArray(arr, item) {
  239. for (let i = arr.length; i--;) {
  240. if (arr[i] === item) {
  241. arr.splice(i, 1);
  242. }
  243. }
  244. }
  245.  
  246. function formatDate(date) {
  247. // Fixed by Archi for all SG weird dates, do not touch
  248.  
  249. // Fix hours
  250. let hours = date.getHours();
  251. let ampm = '';
  252. if (hours < 12) {
  253. ampm = 'am';
  254. if (hours === 0) {
  255. hours = 12;
  256. }
  257. } else {
  258. ampm = 'pm';
  259. if (hours !== 12) {
  260. hours = hours % 12;
  261. }
  262. }
  263.  
  264. // Fix minutes
  265. let minutes = date.getMinutes();
  266. if (minutes < 10) {
  267. minutes = '0' + minutes;
  268. }
  269.  
  270. // Return result
  271. return $.datepicker.formatDate('M d, yy', date) + " " + hours + ":" + minutes + " " + ampm;
  272. }
  273. })();
  274.  
  275. /*jshint multistr: true */
  276. function addStyles() {
  277. GM_addStyle("\
  278. input[type=text], textarea{\
  279. -webkit-transition: all 0.30s ease-in-out;\
  280. -moz-transition: all 0.30s ease-in-out;\
  281. -ms-transition: all 0.30s ease-in-out;\
  282. -o-transition: all 0.30s ease-in-out;\
  283. outline: none;\
  284. padding: 3px 0px 3px 3px;\
  285. margin: 5px 1px 3px 0px;\
  286. border: 1px solid #DDDDDD;\
  287. } \
  288. input[type=text]:focus, textarea:focus {\
  289. box-shadow: 0 0 5px rgba(81, 203, 238, 1);\
  290. padding: 3px 0px 3px 3px;\
  291. margin: 5px 1px 3px 0px;\
  292. border: 1px solid rgba(81, 203, 238, 1) !important; \
  293. }");
  294. }