IG-Add2Lib

indiegala 快速领取免费游戏

当前为 2021-02-19 提交的版本,查看 最新版本

  1. "use strict";
  2.  
  3. 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); } }
  4.  
  5. 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); }); }; }
  6.  
  7. 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; } } }; }
  8.  
  9. 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); }
  10.  
  11. 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; }
  12.  
  13. // ==UserScript==
  14. // @name IG-Add2Lib
  15. // @namespace IG-Add2Lib
  16. // @version 1.0.0
  17. // @description indiegala 快速领取免费游戏
  18. // @author HCLonely
  19. // @license MIT
  20. // @iconURL https://auto-task-test.hclonely.com/img/favicon.ico
  21. // @homepage https://github.com/HCLonely/IG-Add2Lib/
  22. // @supportURL https://github.com/HCLonely/IG-Add2Lib/issues/
  23. // @include *://keylol.com/*
  24. // @grant GM_addStyle
  25. // @grant GM_xmlhttpRequest
  26. // @grant unsafeWindow
  27. // @require https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js
  28. // @require https://cdn.jsdelivr.net/npm/sweetalert2@9
  29. // @require https://cdn.jsdelivr.net/npm/promise-polyfill@8.1.3/dist/polyfill.min.js
  30. // @require https://greasyfork.org/scripts/418102-tm-request/code/TM_request.js?version=902218
  31. // @connect freebies.indiegala.com
  32. // @run-at document-end
  33. // ==/UserScript==
  34. (function () {
  35. addButton();
  36. var observer = new MutationObserver(addButton);
  37. observer.observe(document.documentElement, {
  38. attributes: true,
  39. characterData: true,
  40. childList: true,
  41. subtree: true
  42. });
  43.  
  44. function addButton() {
  45. var _iterator = _createForOfIteratorHelper($('a[href^="https://freebies.indiegala.com/"]:not(".id-add2lib")')),
  46. _step;
  47.  
  48. try {
  49. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  50. var el = _step.value;
  51. var $this = $(el);
  52. $this.after("<a class=\"add-to-library\" href=\"javascript:void(0)\" onclick=\"addToIndiegalaLibrary(this)\" data-href=\"".concat($this.attr('href'), "\" target=\"_self\">\u5165\u5E93</a>")).addClass('id-add2lib');
  53. }
  54. } catch (err) {
  55. _iterator.e(err);
  56. } finally {
  57. _iterator.f();
  58. }
  59. }
  60.  
  61. unsafeWindow.addToIndiegalaLibrary = /*#__PURE__*/function () {
  62. var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(el) {
  63. var href, url;
  64. return regeneratorRuntime.wrap(function _callee$(_context) {
  65. while (1) {
  66. switch (_context.prev = _context.next) {
  67. case 0:
  68. href = $(el).attr('data-href');
  69. Swal.fire({
  70. title: '正在获取入库链接...',
  71. icon: 'info'
  72. });
  73. _context.next = 4;
  74. return TM_request({
  75. url: href,
  76. method: 'GET',
  77. timeout: 30000,
  78. retry: 3
  79. }).then(function (response) {
  80. var _response$responseTex;
  81.  
  82. if (!response.responseText) {
  83. console.error(response);
  84. return null;
  85. }
  86.  
  87. if (response.responseText.includes('https://www.indiegala.com/login')) {
  88. return 0;
  89. }
  90.  
  91. var matchUrl = (_response$responseTex = response.responseText.match(/var[\s]*?url[\s]*?=[\s]*?'(\/ajax\/add-to-library\/.*?\/.*?\/freebies)';/)) === null || _response$responseTex === void 0 ? void 0 : _response$responseTex[1];
  92.  
  93. if (!matchUrl) {
  94. console.error(response);
  95. return null;
  96. }
  97.  
  98. return new URL(matchUrl, href).href;
  99. })["catch"](function (error) {
  100. console.error(error);
  101. return null;
  102. });
  103.  
  104. case 4:
  105. url = _context.sent;
  106.  
  107. if (url) {
  108. _context.next = 11;
  109. break;
  110. }
  111.  
  112. if (!(url === 0)) {
  113. _context.next = 10;
  114. break;
  115. }
  116.  
  117. return _context.abrupt("return", Swal.fire({
  118. title: '请先登录!',
  119. icon: 'error',
  120. html: '<a href="https://www.indiegala.com/login" target="_blank">登录</a>'
  121. }));
  122.  
  123. case 10:
  124. return _context.abrupt("return", Swal.update({
  125. title: '获取入库链接失败!',
  126. icon: 'error'
  127. }));
  128.  
  129. case 11:
  130. Swal.update({
  131. title: '正在入库...',
  132. icon: 'info'
  133. });
  134. TM_request({
  135. url: url,
  136. method: 'POST',
  137. responseType: 'json',
  138. nocache: true,
  139. headers: {
  140. 'content-type': 'application/json'
  141. },
  142. timeout: 30000,
  143. retry: 3
  144. }).then(function (response) {
  145. var _response$response, _response$response2, _response$response3;
  146.  
  147. if (((_response$response = response.response) === null || _response$response === void 0 ? void 0 : _response$response.status) === 'ok') {
  148. Swal.update({
  149. title: '入库成功!',
  150. icon: 'success'
  151. });
  152. } else if (((_response$response2 = response.response) === null || _response$response2 === void 0 ? void 0 : _response$response2.status) === 'added') {
  153. Swal.update({
  154. title: '已在库中!',
  155. icon: 'warning'
  156. });
  157. } else if (((_response$response3 = response.response) === null || _response$response3 === void 0 ? void 0 : _response$response3.status) === 'login') {
  158. Swal.fire({
  159. title: '请先登录!',
  160. icon: 'error',
  161. html: '<a href="https://www.indiegala.com/login" target="_blank">登录</a>'
  162. });
  163. } else {
  164. console.error(response);
  165. Swal.update({
  166. title: '入库失败!',
  167. icon: 'error'
  168. });
  169. }
  170. });
  171.  
  172. case 13:
  173. case "end":
  174. return _context.stop();
  175. }
  176. }
  177. }, _callee);
  178. }));
  179.  
  180. return function (_x) {
  181. return _ref.apply(this, arguments);
  182. };
  183. }();
  184.  
  185. GM_addStyle('.add-to-library{margin-left:10px;}');
  186. })();