IndieGala Create Giveaway Helper

Creating a giveaway is now a lot easier!!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         IndieGala Create Giveaway Helper
// @namespace    https://github.com/MrMarble/IndieGalaCreateGiveawayHelper
// @version      0.6
// @description  Creating a giveaway is now  a lot easier!!
// @author       MrMarble
// @license MIT
// @match        https://www.indiegala.com/profile?user_id=*
// @match        https://www.indiegala.com/gift?gift_id=*
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @grant        none
// ==/UserScript==

(function() {
  'use strict';
  run();

  function run() {
    if (!'URLSearchParams' in window) {
      throw "Web Browser not supported!";
    }

    let searchParams = new URLSearchParams(window.location.search);
    if (searchParams.has('user_id')) {
      fillGiveaway();
    } else if (searchParams.has('gift_id')) {
      waitForKeyElements("#steam-key-games", addButton, true);
    }
  }

  function addButton() {
    add_style();
    jQuery('div[id^="serial_"]').each((i, element) => {
      jQuery(element).append('<div class="entry-elem align-c create-giveaway-helper"><i aria-hidden="true" class="fa fa-gift"></i></div>');
    });
    jQuery('.create-giveaway-helper').on('click', function() {
      let game_url = jQuery(this).parents('.game-key-string').find('a.game-steam-url').attr('href');
      let game_serial = jQuery(this).prevAll('input[id^=serial_n_]').val();

      let w = window.open('https://www.indiegala.com/profile', '_blank', 'top=10,height=500,menubar=0,status=0,toolbar=0');
      w.opener = null; //Bypass IndieGala autoclose  of new windows
      w.game_url = game_url;
      w.game_serial = game_serial;
    });
  }

  function fillGiveaway() {
    jQuery(document).ready(function() {
      if (window.game_url !== undefined && window.game_serial !== undefined) {
        jQuery('.giveaways-new-cont').slideToggle();
        jQuery('#collapseGiveaways').toggleClass('in').css('height', 'auto');

        jQuery('textarea.giveaway-description').val('IndieGala Giveaway');
        jQuery('.form-not-guaranteed input').val(window.game_url);
        jQuery('.form-not-guaranteed button.form-button-1').trigger('click');
        waitForKeyElements('ul.form-not-guaranteed input[rel="steamSerialKey"]', function() {
          jQuery('ul.form-not-guaranteed input[rel="steamSerialKey"]').val(window.game_serial);
          jQuery('button.btn-add-rel-game').trigger('click');
          waitForKeyElements('span.points-result', function() {
            jQuery('button.btn-calculate-giv-value').trigger('click');
          }, true);
        }, true);
      }
    });
  }

  function add_style() {
    let css = `div.create-giveaway-helper {
      position: absolute;
      height: 24px;
      top: 1px;
      line-height: 10px;
      padding: 4px 10px;
      margin-left: 1px;
      color: #8c2222;
    }
    div.create-giveaway-helper:hover{
      cursor: pointer;
    }
    `;
    if (jQuery('.donate_indiegala_helper').length == 0) {
      css += ' .span-key {position: relative;}div.create-giveaway-helper {right:1px}'
    }
    jQuery('head').append('<style>' + css + '</style>');
  }

  function waitForKeyElements(selectorTxt, actionFunction, bWaitOnce, iframeSelector) {
    var targetNodes,
      btargetsFound;

    if (typeof iframeSelector == "undefined")
      targetNodes = jQuery(selectorTxt);
    else
      targetNodes = jQuery(iframeSelector).contents().find(selectorTxt);

    if (targetNodes && targetNodes.length > 0) {
      btargetsFound = true;
      /*--- Found target node(s).  Go through each and act if they
              are new.
          */
      targetNodes.each(function() {
        var jThis = jQuery(this);
        var alreadyFound = jThis.data('alreadyFound') || false;

        if (!alreadyFound) {
          //--- Call the payload function.
          var cancelFound = actionFunction(jThis);
          if (cancelFound)
            btargetsFound = false;
          else
            jThis.data('alreadyFound', true);
          }
        });
    } else {
      btargetsFound = false;
    }

    //--- Get the timer-control variable for this selector.
    var controlObj = waitForKeyElements.controlObj || {};
    var controlKey = selectorTxt.replace(/[^\w]/g, "_");
    var timeControl = controlObj[controlKey];

    //--- Now set or clear the timer as appropriate.
    if (btargetsFound && bWaitOnce && timeControl) {
      //--- The only condition where we need to clear the timer.
      clearInterval(timeControl);
      delete controlObj[controlKey]
    } else {
      //--- Set a timer, if needed.
      if (!timeControl) {
        timeControl = setInterval(function() {
          waitForKeyElements(selectorTxt, actionFunction, bWaitOnce, iframeSelector);
        }, 300);
        controlObj[controlKey] = timeControl;
      }
    }
    waitForKeyElements.controlObj = controlObj;
  }
})();