IndieGala: Check giveaways if won

Check won giveaways the fast & convenient way

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         IndieGala: Check giveaways if won
// @version      1.1.0
// @description  Check won giveaways the fast & convenient way
// @author       Hafas (https://github.com/Hafas/)
// @match        https://www.indiegala.com/profile*
// @grant        none
// @namespace https://greasyfork.org/users/56087
// ==/UserScript==
(function () {
  var $completedTab = $(".giveaway-completed").parent();
  $completedTab.prepend("<button/>");
  var $myButton = $("button", $completedTab).first();
  activateButton();
  $myButton.on("click", function () {
    deactivateButton();
    checkAllIfWon().then(function (html) {
      printSummary(html);
      activateButton();
    });
  });

  function activateButton () {
    $myButton.html("Check all if won");
    $myButton.css({
      background: "#CC001D",
      color: "white",
      float: "right",
      marginLeft: "2px",
      padding: "9px"
    });
    $myButton.attr("disabled", false);
  }

  function deactivateButton () {
    $myButton.html("Checking ...");
    $myButton.css({background: "#999"});
    $myButton.attr("disabled", true);
  }

  function getCompleted () {
    return $.ajax({
      url: "/giveaways/library_completed",
      dataType: "json"
    }).then(null, function (error) {
      //retry in 10s
      return delay(getCompleted, 10000);
    });
  }

  function checkAllIfWon (entryIds) {
    return $.ajax({
      url: "/giveaways/check_if_won_all"
    }).then(null, function () {
      //retry in 10s
      return delay(checkAllIfWon, 10000);
    });
  }

  function printSummary (html) {
    alert($(html).text().trim());
  }

  function delay (fn, timeout) {
    return $.Deferred(function (d) {
      setTimeout(function () {
        fn().then(function (value) {
          d.resolve(value);
        });
      }, timeout);
    });
  }
})();