IndieGala: Check giveaways if won

Check won giveaways the fast & convenient way

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
    });
  }
})();