IG-Add2Lib

indiegala 快速领取免费游戏

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

  1. "use strict";
  2.  
  3. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  4.  
  5. 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."); }
  6.  
  7. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
  8.  
  9. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  10.  
  11. 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); } }
  12.  
  13. 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); }); }; }
  14.  
  15. 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; } } }; }
  16.  
  17. 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); }
  18.  
  19. 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; }
  20.  
  21. // ==UserScript==
  22. // @name IG-Add2Lib
  23. // @namespace IG-Add2Lib
  24. // @version 1.0.2
  25. // @description indiegala 快速领取免费游戏
  26. // @author HCLonely
  27. // @license MIT
  28. // @iconURL https://auto-task-test.hclonely.com/img/favicon.ico
  29. // @homepage https://github.com/HCLonely/IG-Add2Lib/
  30. // @supportURL https://github.com/HCLonely/IG-Add2Lib/issues/
  31. // @include *://keylol.com/*
  32. // @grant GM_addStyle
  33. // @grant GM_xmlhttpRequest
  34. // @grant GM_registerMenuCommand
  35. // @grant unsafeWindow
  36. // @require https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js
  37. // @require https://cdn.jsdelivr.net/npm/sweetalert2@9
  38. // @require https://cdn.jsdelivr.net/npm/promise-polyfill@8.1.3/dist/polyfill.min.js
  39. // @require https://greasyfork.org/scripts/418102-tm-request/code/TM_request.js?version=902218
  40. // @connect indiegala.com
  41. // @run-at document-end
  42. // ==/UserScript==
  43.  
  44. /* global addToIndiegalaLibrary */
  45. (function () {
  46. addButton();
  47. var observer = new MutationObserver(addButton);
  48. observer.observe(document.documentElement, {
  49. attributes: true,
  50. characterData: true,
  51. childList: true,
  52. subtree: true
  53. });
  54.  
  55. function addButton() {
  56. var _iterator = _createForOfIteratorHelper($('a[href*=".indiegala.com/"]:not(".id-add2lib")')),
  57. _step;
  58.  
  59. try {
  60. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  61. var el = _step.value;
  62. var $this = $(el).addClass('id-add2lib');
  63. var href = $this.attr('href');
  64.  
  65. if (/^https?:\/\/[\w\d]+?\.indiegala\.com\/.+$/.test(href)) {
  66. $this.after("<a class=\"add-to-library\" href=\"javascript:void(0)\" onclick=\"addToIndiegalaLibrary(this)\" data-href=\"".concat(href, "\" target=\"_self\">\u5165\u5E93</a>"));
  67. }
  68. }
  69. } catch (err) {
  70. _iterator.e(err);
  71. } finally {
  72. _iterator.f();
  73. }
  74. }
  75.  
  76. unsafeWindow.addToIndiegalaLibrary = /*#__PURE__*/function () {
  77. var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(el) {
  78. var href, url;
  79. return regeneratorRuntime.wrap(function _callee$(_context) {
  80. while (1) {
  81. switch (_context.prev = _context.next) {
  82. case 0:
  83. href = typeof el === 'string' ? el : $(el).attr('data-href');
  84. Swal.fire({
  85. title: '正在获取入库链接...',
  86. text: href,
  87. icon: 'info'
  88. });
  89. _context.next = 4;
  90. return TM_request({
  91. url: href,
  92. method: 'GET',
  93. timeout: 30000,
  94. retry: 3,
  95. cookie: 'incap_ses_896_255598='
  96. }).then(function (response) {
  97. var _response$responseTex;
  98.  
  99. if (!response.responseText) {
  100. console.error(response);
  101. return null;
  102. }
  103.  
  104. var pageId = (_response$responseTex = response.responseText.match(/dataToSend\.gala_page_id[\s]*?=[\s]*?'(.*?)';/)) === null || _response$responseTex === void 0 ? void 0 : _response$responseTex[1];
  105.  
  106. if (!pageId) {
  107. console.error(response);
  108. return null;
  109. }
  110.  
  111. 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;
  112. })["catch"](function (error) {
  113. console.error(error);
  114. return null;
  115. });
  116.  
  117. case 4:
  118. url = _context.sent;
  119.  
  120. if (url) {
  121. _context.next = 8;
  122. break;
  123. }
  124.  
  125. Swal.update({
  126. title: '获取入库链接失败!',
  127. text: href,
  128. icon: 'error'
  129. });
  130. return _context.abrupt("return", null);
  131.  
  132. case 8:
  133. Swal.update({
  134. title: '正在入库...',
  135. text: href,
  136. icon: 'info'
  137. });
  138. return _context.abrupt("return", TM_request({
  139. url: url,
  140. method: 'POST',
  141. responseType: 'json',
  142. nocache: true,
  143. headers: {
  144. 'content-type': 'application/json'
  145. },
  146. timeout: 30000,
  147. retry: 3
  148. }).then(function (response) {
  149. var _response$response, _response$response2, _response$response3;
  150.  
  151. if (((_response$response = response.response) === null || _response$response === void 0 ? void 0 : _response$response.status) === 'ok') {
  152. Swal.update({
  153. title: '入库成功!',
  154. text: href,
  155. icon: 'success'
  156. });
  157. return true;
  158. } else if (((_response$response2 = response.response) === null || _response$response2 === void 0 ? void 0 : _response$response2.status) === 'added') {
  159. Swal.update({
  160. title: '已在库中!',
  161. text: href,
  162. icon: 'warning'
  163. });
  164. return true;
  165. } else if (((_response$response3 = response.response) === null || _response$response3 === void 0 ? void 0 : _response$response3.status) === 'login') {
  166. Swal.fire({
  167. title: '请先登录!',
  168. icon: 'error',
  169. html: '<a href="https://www.indiegala.com/login" target="_blank">登录</a>'
  170. });
  171. return false;
  172. } else {
  173. console.error(response);
  174. Swal.update({
  175. title: '入库失败!',
  176. text: href,
  177. icon: 'error'
  178. });
  179. return null;
  180. }
  181. }));
  182.  
  183. case 10:
  184. case "end":
  185. return _context.stop();
  186. }
  187. }
  188. }, _callee);
  189. }));
  190.  
  191. return function (_x) {
  192. return _ref.apply(this, arguments);
  193. };
  194. }();
  195.  
  196. GM_registerMenuCommand('入库所有', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
  197. var links, newLinks, failedLinks, _iterator2, _step2, link, result;
  198.  
  199. return regeneratorRuntime.wrap(function _callee2$(_context2) {
  200. while (1) {
  201. switch (_context2.prev = _context2.next) {
  202. case 0:
  203. links = $.makeArray($('a.add-to-library')).map(function (e, i) {
  204. return $(e).attr('data-href');
  205. });
  206. newLinks = _toConsumableArray(new Set(links));
  207. failedLinks = [];
  208. _iterator2 = _createForOfIteratorHelper(newLinks);
  209. _context2.prev = 4;
  210.  
  211. _iterator2.s();
  212.  
  213. case 6:
  214. if ((_step2 = _iterator2.n()).done) {
  215. _context2.next = 18;
  216. break;
  217. }
  218.  
  219. link = _step2.value;
  220. _context2.next = 10;
  221. return addToIndiegalaLibrary(link);
  222.  
  223. case 10:
  224. result = _context2.sent;
  225.  
  226. if (!(result === false)) {
  227. _context2.next = 15;
  228. break;
  229. }
  230.  
  231. return _context2.abrupt("break", 18);
  232.  
  233. case 15:
  234. if (!result) {
  235. failedLinks.push("<a href=\"".concat(link, "\" target=_blank\">").concat(link, "</a>"));
  236. }
  237.  
  238. case 16:
  239. _context2.next = 6;
  240. break;
  241.  
  242. case 18:
  243. _context2.next = 23;
  244. break;
  245.  
  246. case 20:
  247. _context2.prev = 20;
  248. _context2.t0 = _context2["catch"](4);
  249.  
  250. _iterator2.e(_context2.t0);
  251.  
  252. case 23:
  253. _context2.prev = 23;
  254.  
  255. _iterator2.f();
  256.  
  257. return _context2.finish(23);
  258.  
  259. case 26:
  260. if (failedLinks.length === 0) {
  261. Swal.fire({
  262. title: '全部任务完成!',
  263. icon: 'success'
  264. });
  265. } else {
  266. Swal.fire({
  267. title: '以下任务未完成!',
  268. icon: 'warning',
  269. html: failedLinks.join('<br/>')
  270. });
  271. }
  272.  
  273. case 27:
  274. case "end":
  275. return _context2.stop();
  276. }
  277. }
  278. }, _callee2, null, [[4, 20, 23, 26]]);
  279. })));
  280. GM_addStyle('.add-to-library{margin-left:10px;}');
  281. })();