IG-Add2Lib

indiegala 快速领取免费游戏

As of 19. 02. 2021. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

"use strict";

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

// ==UserScript==
// @name               IG-Add2Lib
// @namespace          IG-Add2Lib
// @version            1.0.2
// @description        indiegala 快速领取免费游戏
// @author             HCLonely
// @license            MIT
// @iconURL            https://auto-task-test.hclonely.com/img/favicon.ico
// @homepage           https://github.com/HCLonely/IG-Add2Lib/
// @supportURL         https://github.com/HCLonely/IG-Add2Lib/issues/
// @include            *://keylol.com/*
// @grant              GM_addStyle
// @grant              GM_xmlhttpRequest
// @grant              GM_registerMenuCommand
// @grant              unsafeWindow
// @require            https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js
// @require            https://cdn.jsdelivr.net/npm/sweetalert2@9
// @require            https://cdn.jsdelivr.net/npm/[email protected]/dist/polyfill.min.js
// @require            https://greasyfork.org/scripts/418102-tm-request/code/TM_request.js?version=902218
// @connect            indiegala.com
// @run-at             document-end
// ==/UserScript==

/* global addToIndiegalaLibrary */
(function () {
  addButton();
  var observer = new MutationObserver(addButton);
  observer.observe(document.documentElement, {
    attributes: true,
    characterData: true,
    childList: true,
    subtree: true
  });

  function addButton() {
    var _iterator = _createForOfIteratorHelper($('a[href*=".indiegala.com/"]:not(".id-add2lib")')),
        _step;

    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var el = _step.value;
        var $this = $(el).addClass('id-add2lib');
        var href = $this.attr('href');

        if (/^https?:\/\/[\w\d]+?\.indiegala\.com\/.+$/.test(href)) {
          $this.after("<a class=\"add-to-library\" href=\"javascript:void(0)\" onclick=\"addToIndiegalaLibrary(this)\" data-href=\"".concat(href, "\" target=\"_self\">\u5165\u5E93</a>"));
        }
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  }

  unsafeWindow.addToIndiegalaLibrary = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(el) {
      var href, url;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              href = typeof el === 'string' ? el : $(el).attr('data-href');
              Swal.fire({
                title: '正在获取入库链接...',
                text: href,
                icon: 'info'
              });
              _context.next = 4;
              return TM_request({
                url: href,
                method: 'GET',
                timeout: 30000,
                retry: 3,
                cookie: 'incap_ses_896_255598='
              }).then(function (response) {
                var _response$responseTex;

                if (!response.responseText) {
                  console.error(response);
                  return null;
                }

                var pageId = (_response$responseTex = response.responseText.match(/dataToSend\.gala_page_id[\s]*?=[\s]*?'(.*?)';/)) === null || _response$responseTex === void 0 ? void 0 : _response$responseTex[1];

                if (!pageId) {
                  console.error(response);
                  return null;
                }

                return new URL("/ajax/add-to-library/".concat(pageId, "/").concat(new URL(href).pathname.replace('/', ''), "/").concat(new URL(href).hostname.replace('.indiegala.com', '')), href).href;
              })["catch"](function (error) {
                console.error(error);
                return null;
              });

            case 4:
              url = _context.sent;

              if (url) {
                _context.next = 8;
                break;
              }

              Swal.update({
                title: '获取入库链接失败!',
                text: href,
                icon: 'error'
              });
              return _context.abrupt("return", null);

            case 8:
              Swal.update({
                title: '正在入库...',
                text: href,
                icon: 'info'
              });
              return _context.abrupt("return", TM_request({
                url: url,
                method: 'POST',
                responseType: 'json',
                nocache: true,
                headers: {
                  'content-type': 'application/json'
                },
                timeout: 30000,
                retry: 3
              }).then(function (response) {
                var _response$response, _response$response2, _response$response3;

                if (((_response$response = response.response) === null || _response$response === void 0 ? void 0 : _response$response.status) === 'ok') {
                  Swal.update({
                    title: '入库成功!',
                    text: href,
                    icon: 'success'
                  });
                  return true;
                } else if (((_response$response2 = response.response) === null || _response$response2 === void 0 ? void 0 : _response$response2.status) === 'added') {
                  Swal.update({
                    title: '已在库中!',
                    text: href,
                    icon: 'warning'
                  });
                  return true;
                } else if (((_response$response3 = response.response) === null || _response$response3 === void 0 ? void 0 : _response$response3.status) === 'login') {
                  Swal.fire({
                    title: '请先登录!',
                    icon: 'error',
                    html: '<a href="https://www.indiegala.com/login" target="_blank">登录</a>'
                  });
                  return false;
                } else {
                  console.error(response);
                  Swal.update({
                    title: '入库失败!',
                    text: href,
                    icon: 'error'
                  });
                  return null;
                }
              }));

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }();

  GM_registerMenuCommand('入库所有', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
    var links, newLinks, failedLinks, _iterator2, _step2, link, result;

    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            links = $.makeArray($('a.add-to-library')).map(function (e, i) {
              return $(e).attr('data-href');
            });
            newLinks = _toConsumableArray(new Set(links));
            failedLinks = [];
            _iterator2 = _createForOfIteratorHelper(newLinks);
            _context2.prev = 4;

            _iterator2.s();

          case 6:
            if ((_step2 = _iterator2.n()).done) {
              _context2.next = 18;
              break;
            }

            link = _step2.value;
            _context2.next = 10;
            return addToIndiegalaLibrary(link);

          case 10:
            result = _context2.sent;

            if (!(result === false)) {
              _context2.next = 15;
              break;
            }

            return _context2.abrupt("break", 18);

          case 15:
            if (!result) {
              failedLinks.push("<a href=\"".concat(link, "\" target=_blank\">").concat(link, "</a>"));
            }

          case 16:
            _context2.next = 6;
            break;

          case 18:
            _context2.next = 23;
            break;

          case 20:
            _context2.prev = 20;
            _context2.t0 = _context2["catch"](4);

            _iterator2.e(_context2.t0);

          case 23:
            _context2.prev = 23;

            _iterator2.f();

            return _context2.finish(23);

          case 26:
            if (failedLinks.length === 0) {
              Swal.fire({
                title: '全部任务完成!',
                icon: 'success'
              });
            } else {
              Swal.fire({
                title: '以下任务未完成!',
                icon: 'warning',
                html: failedLinks.join('<br/>')
              });
            }

          case 27:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, null, [[4, 20, 23, 26]]);
  })));
  GM_addStyle('.add-to-library{margin-left:10px;}');
})();