gistSync

使用gist进行数据同步

目前为 2021-05-22 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/426803/933451/gistSync.js

  1. "use strict";
  2.  
  3. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  4.  
  5. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  6.  
  7. function _iterableToArrayLimit(arr, i) { var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  8.  
  9. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  10.  
  11. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { 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(_e2) { throw _e2; }, 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 = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  12.  
  13. 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); }
  14.  
  15. 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; }
  16.  
  17. 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); } }
  18.  
  19. 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); }); }; }
  20.  
  21. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  22.  
  23. // ==UserScript==
  24. // @name gistSync
  25. // @namespace gistSync
  26. // @version 1.0.2
  27. // @description 使用gist进行数据同步
  28. // @author HCLonely
  29. // @license MIT
  30. // @include *://*/*
  31. // @grant GM_setValue
  32. // @grant GM_getValue
  33. // @grant GM_listValues
  34. // @grant GM_xmlhttpRequest
  35. // @grant GM_registerMenuCommand
  36. // @require https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js
  37. // @require https://cdn.jsdelivr.net/npm/regenerator-runtime@0.13.7/runtime.min.js
  38. // @require https://cdn.jsdelivr.net/npm/sweetalert2@9
  39. // @require https://cdn.jsdelivr.net/npm/promise-polyfill@8.1.3/dist/polyfill.min.js
  40. // @require https://greasyfork.org/scripts/418102-tm-request/code/TM_request.js?version=902218
  41. // @connect api.github.com
  42. // @run-at document-end
  43. // ==/UserScript==
  44.  
  45. /* global Swal,TM_request, GM_setValue, GM_getValue, GM_listValues */
  46.  
  47. /*
  48. const { TOKEN, GIST_ID, FILE_NAME, AUTO_SYNC } = GM_getValue('gistConf') || { TOKEN: '', GIST_ID: '', FILE_NAME: '' }
  49. if (TOKEN && GIST_ID && FILE_NAME && AUTO_SYNC){
  50.  
  51. }
  52. */
  53. function setGistData(token, gistId, fileName, content) {
  54. var data = JSON.stringify({
  55. files: _defineProperty({}, fileName, {
  56. content: JSON.stringify(content)
  57. })
  58. });
  59. return TM_request({
  60. url: 'https://api.github.com/gists/' + gistId,
  61. headers: {
  62. Accept: 'application/vnd.github.v3+json',
  63. Authorization: 'token ' + token
  64. },
  65. data: data,
  66. responseType: 'json',
  67. method: 'POST',
  68. timeout: 30000,
  69. retry: 3
  70. }).then(function (response) {
  71. var _response$response, _response$response$fi, _response$response$fi2;
  72.  
  73. if (response.status === 200 && ((_response$response = response.response) === null || _response$response === void 0 ? void 0 : (_response$response$fi = _response$response.files) === null || _response$response$fi === void 0 ? void 0 : (_response$response$fi2 = _response$response$fi[fileName]) === null || _response$response$fi2 === void 0 ? void 0 : _response$response$fi2.content) === JSON.stringify(content)) {
  74. return true;
  75. } else {
  76. console.error(response);
  77. return false;
  78. }
  79. })["catch"](function (error) {
  80. console.error(error);
  81. return false;
  82. });
  83. }
  84.  
  85. function getGistData(token, gistId, fileName) {
  86. return TM_request({
  87. url: 'https://api.github.com/gists/' + gistId,
  88. headers: {
  89. Accept: 'application/vnd.github.v3+json',
  90. Authorization: 'token ' + token
  91. },
  92. responseType: 'json',
  93. method: 'GET',
  94. timeout: 30000,
  95. retry: 3
  96. }).then(function (response) {
  97. if (response.status === 200) {
  98. var _response$response2, _response$response2$f, _response$response2$f2;
  99.  
  100. return JSON.parse(((_response$response2 = response.response) === null || _response$response2 === void 0 ? void 0 : (_response$response2$f = _response$response2.files) === null || _response$response2$f === void 0 ? void 0 : (_response$response2$f2 = _response$response2$f[fileName]) === null || _response$response2$f2 === void 0 ? void 0 : _response$response2$f2.content) || null);
  101. } else {
  102. console.error(response);
  103. return false;
  104. }
  105. })["catch"](function (error) {
  106. console.error(error);
  107. return false;
  108. });
  109. }
  110.  
  111. function setting() {
  112. var _ref = GM_getValue('gistConf') || {
  113. TOKEN: '',
  114. GIST_ID: '',
  115. FILE_NAME: ''
  116. },
  117. TOKEN = _ref.TOKEN,
  118. GIST_ID = _ref.GIST_ID,
  119. FILE_NAME = _ref.FILE_NAME;
  120.  
  121. Swal.fire({
  122. title: 'Gist 设置',
  123. html: '<p>Github Token<input id="github-token" class="swal2-input" placeholder="Github Token" value="' + TOKEN + '"></p>' + '<p>Gist ID<input id="gist-id" class="swal2-input" placeholder="Gist ID" value="' + GIST_ID + '"></p>' + '<p>文件名<input id="file-name" class="swal2-input" placeholder="文件名" value="' + FILE_NAME + '"></p>' + // '<p><input id="auto-sync" type="checkbox" ' + (AUTO_SYNC ? 'checked="checked"' : '') + '><span class="swal2-label">自动同步</span></p>' +
  124. '<p>' + '<button id="upload-data" type="button" class="swal2-confirm swal2-styled" aria-label="" style="display: inline-block;">同步到Gist</button>' + '<button id="download-data" type="button" class="swal2-confirm swal2-styled" aria-label="" style="display: inline-block;">从Gist同步</button>' + '</p>',
  125. focusConfirm: false,
  126. showLoaderOnConfirm: true,
  127. footer: '<a href="https://github.com/HCLonely/IG-Helper/blob/master/README.md" target-"_blank" class>帮助?</a>',
  128. preConfirm: function () {
  129. var _preConfirm = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
  130. var token, gistId, fileName;
  131. return regeneratorRuntime.wrap(function _callee$(_context) {
  132. while (1) {
  133. switch (_context.prev = _context.next) {
  134. case 0:
  135. token = document.getElementById('github-token').value;
  136. gistId = document.getElementById('gist-id').value;
  137. fileName = document.getElementById('file-name').value; // const autoSync = document.getElementById('auto-sync').checked
  138.  
  139. GM_setValue('gistConf', {
  140. TOKEN: token,
  141. GIST_ID: gistId,
  142. FILE_NAME: fileName
  143. /* , AUTO_SYNC: autoSync */
  144.  
  145. });
  146. _context.next = 6;
  147. return getGistData(token, gistId, fileName);
  148.  
  149. case 6:
  150. return _context.abrupt("return", _context.sent);
  151.  
  152. case 7:
  153. case "end":
  154. return _context.stop();
  155. }
  156. }
  157. }, _callee);
  158. }));
  159.  
  160. function preConfirm() {
  161. return _preConfirm.apply(this, arguments);
  162. }
  163.  
  164. return preConfirm;
  165. }(),
  166. allowOutsideClick: function allowOutsideClick() {
  167. return !Swal.isLoading();
  168. },
  169. confirmButtonText: '保存配置并测试',
  170. showCancelButton: true,
  171. cancelButtonText: '关闭'
  172. }).then(function (_ref2) {
  173. var value = _ref2.value;
  174.  
  175. if (value) {
  176. Swal.fire({
  177. icon: 'success',
  178. title: '测试成功!'
  179. }).then(function () {
  180. setting();
  181. });
  182. } else if (value !== undefined) {
  183. Swal.fire({
  184. icon: 'error',
  185. title: '测试失败!'
  186. }).then(function () {
  187. setting();
  188. });
  189. }
  190. });
  191. $('#upload-data').click( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
  192. var _ref4, TOKEN, GIST_ID, FILE_NAME, data, names, _iterator, _step, name;
  193.  
  194. return regeneratorRuntime.wrap(function _callee2$(_context2) {
  195. while (1) {
  196. switch (_context2.prev = _context2.next) {
  197. case 0:
  198. _ref4 = GM_getValue('gistConf') || {}, TOKEN = _ref4.TOKEN, GIST_ID = _ref4.GIST_ID, FILE_NAME = _ref4.FILE_NAME;
  199.  
  200. if (TOKEN && GIST_ID && FILE_NAME) {
  201. _context2.next = 3;
  202. break;
  203. }
  204.  
  205. return _context2.abrupt("return", Swal.fire({
  206. icon: 'error',
  207. title: '请先保存配置并测试!'
  208. }).then(function () {
  209. setting();
  210. }));
  211.  
  212. case 3:
  213. Swal.fire({
  214. icon: 'info',
  215. title: '正在处理数据...'
  216. });
  217. data = {};
  218. names = GM_listValues();
  219. _iterator = _createForOfIteratorHelper(names);
  220.  
  221. try {
  222. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  223. name = _step.value;
  224. data[name] = GM_getValue(name);
  225. }
  226. } catch (err) {
  227. _iterator.e(err);
  228. } finally {
  229. _iterator.f();
  230. }
  231.  
  232. Swal.update({
  233. icon: 'info',
  234. title: '正在上传数据...'
  235. });
  236. _context2.next = 11;
  237. return setGistData(TOKEN, GIST_ID, FILE_NAME, data);
  238.  
  239. case 11:
  240. if (!_context2.sent) {
  241. _context2.next = 15;
  242. break;
  243. }
  244.  
  245. Swal.fire({
  246. icon: 'success',
  247. title: '同步数据成功!'
  248. });
  249. _context2.next = 16;
  250. break;
  251.  
  252. case 15:
  253. Swal.fire({
  254. icon: 'error',
  255. title: '同步数据失败,请在控制台查看错误信息!'
  256. });
  257.  
  258. case 16:
  259. case "end":
  260. return _context2.stop();
  261. }
  262. }
  263. }, _callee2);
  264. })));
  265. $('#download-data').click( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
  266. var _ref6, TOKEN, GIST_ID, FILE_NAME, data, _i, _Object$entries, _Object$entries$_i, name, value;
  267.  
  268. return regeneratorRuntime.wrap(function _callee3$(_context3) {
  269. while (1) {
  270. switch (_context3.prev = _context3.next) {
  271. case 0:
  272. _ref6 = GM_getValue('gistConf') || {}, TOKEN = _ref6.TOKEN, GIST_ID = _ref6.GIST_ID, FILE_NAME = _ref6.FILE_NAME;
  273.  
  274. if (TOKEN && GIST_ID && FILE_NAME) {
  275. _context3.next = 3;
  276. break;
  277. }
  278.  
  279. return _context3.abrupt("return", Swal.fire({
  280. icon: 'error',
  281. title: '请先保存配置并测试!'
  282. }).then(function () {
  283. setting();
  284. }));
  285.  
  286. case 3:
  287. Swal.fire({
  288. icon: 'info',
  289. title: '正在下载数据...'
  290. });
  291. _context3.next = 6;
  292. return getGistData(TOKEN, GIST_ID, FILE_NAME);
  293.  
  294. case 6:
  295. data = _context3.sent;
  296.  
  297. if (data) {
  298. _context3.next = 9;
  299. break;
  300. }
  301.  
  302. return _context3.abrupt("return", Swal.fire({
  303. icon: 'error',
  304. title: '没有检测到远程数据,请确认配置是否正确!'
  305. }).then(function () {
  306. setting();
  307. }));
  308.  
  309. case 9:
  310. Swal.update({
  311. icon: 'info',
  312. title: '正在保存数据...'
  313. });
  314.  
  315. for (_i = 0, _Object$entries = Object.entries(data); _i < _Object$entries.length; _i++) {
  316. _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), name = _Object$entries$_i[0], value = _Object$entries$_i[1];
  317. GM_setValue(name, value);
  318. }
  319.  
  320. Swal.fire({
  321. icon: 'success',
  322. title: '同步数据成功!'
  323. });
  324.  
  325. case 12:
  326. case "end":
  327. return _context3.stop();
  328. }
  329. }
  330. }, _callee3);
  331. })));
  332. }
  333.  
  334. GM_registerMenuCommand('数据同步设置', setting);