timetable2lua

Export timetable data as lua table.

目前为 2024-05-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name timetable2lua
  3. // @namespace https://dvxg.de/
  4. // @version 0.1.2
  5. // @author davidxuang
  6. // @description Export timetable data as lua table.
  7. // @license AGPL-3.0-only
  8. // @icon https://cdn.jsdelivr.net/gh/microsoft/fluentui-emoji/assets/Metro/3D/metro_3d.png
  9. // @homepage https://github.com/davidxuang/timetable2lua
  10. // @homepageURL https://github.com/davidxuang/timetable2lua
  11. // @match https://www.cqmetro.cn/smbsj.html
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. function getDefaultExportFromCjs(x) {
  18. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
  19. }
  20. var wcwidth$3 = { exports: {} };
  21. var clone$1 = { exports: {} };
  22. (function(module) {
  23. var clone2 = function() {
  24. function clone3(parent, circular, depth, prototype) {
  25. if (typeof circular === "object") {
  26. depth = circular.depth;
  27. prototype = circular.prototype;
  28. circular.filter;
  29. circular = circular.circular;
  30. }
  31. var allParents = [];
  32. var allChildren = [];
  33. var useBuffer = typeof Buffer != "undefined";
  34. if (typeof circular == "undefined")
  35. circular = true;
  36. if (typeof depth == "undefined")
  37. depth = Infinity;
  38. function _clone(parent2, depth2) {
  39. if (parent2 === null)
  40. return null;
  41. if (depth2 == 0)
  42. return parent2;
  43. var child;
  44. var proto;
  45. if (typeof parent2 != "object") {
  46. return parent2;
  47. }
  48. if (clone3.__isArray(parent2)) {
  49. child = [];
  50. } else if (clone3.__isRegExp(parent2)) {
  51. child = new RegExp(parent2.source, __getRegExpFlags(parent2));
  52. if (parent2.lastIndex)
  53. child.lastIndex = parent2.lastIndex;
  54. } else if (clone3.__isDate(parent2)) {
  55. child = new Date(parent2.getTime());
  56. } else if (useBuffer && Buffer.isBuffer(parent2)) {
  57. if (Buffer.allocUnsafe) {
  58. child = Buffer.allocUnsafe(parent2.length);
  59. } else {
  60. child = new Buffer(parent2.length);
  61. }
  62. parent2.copy(child);
  63. return child;
  64. } else {
  65. if (typeof prototype == "undefined") {
  66. proto = Object.getPrototypeOf(parent2);
  67. child = Object.create(proto);
  68. } else {
  69. child = Object.create(prototype);
  70. proto = prototype;
  71. }
  72. }
  73. if (circular) {
  74. var index = allParents.indexOf(parent2);
  75. if (index != -1) {
  76. return allChildren[index];
  77. }
  78. allParents.push(parent2);
  79. allChildren.push(child);
  80. }
  81. for (var i in parent2) {
  82. var attrs;
  83. if (proto) {
  84. attrs = Object.getOwnPropertyDescriptor(proto, i);
  85. }
  86. if (attrs && attrs.set == null) {
  87. continue;
  88. }
  89. child[i] = _clone(parent2[i], depth2 - 1);
  90. }
  91. return child;
  92. }
  93. return _clone(parent, depth);
  94. }
  95. clone3.clonePrototype = function clonePrototype(parent) {
  96. if (parent === null)
  97. return null;
  98. var c = function() {
  99. };
  100. c.prototype = parent;
  101. return new c();
  102. };
  103. function __objToStr(o) {
  104. return Object.prototype.toString.call(o);
  105. }
  106. clone3.__objToStr = __objToStr;
  107. function __isDate(o) {
  108. return typeof o === "object" && __objToStr(o) === "[object Date]";
  109. }
  110. clone3.__isDate = __isDate;
  111. function __isArray(o) {
  112. return typeof o === "object" && __objToStr(o) === "[object Array]";
  113. }
  114. clone3.__isArray = __isArray;
  115. function __isRegExp(o) {
  116. return typeof o === "object" && __objToStr(o) === "[object RegExp]";
  117. }
  118. clone3.__isRegExp = __isRegExp;
  119. function __getRegExpFlags(re) {
  120. var flags = "";
  121. if (re.global)
  122. flags += "g";
  123. if (re.ignoreCase)
  124. flags += "i";
  125. if (re.multiline)
  126. flags += "m";
  127. return flags;
  128. }
  129. clone3.__getRegExpFlags = __getRegExpFlags;
  130. return clone3;
  131. }();
  132. if (module.exports) {
  133. module.exports = clone2;
  134. }
  135. })(clone$1);
  136. var cloneExports = clone$1.exports;
  137. var clone = cloneExports;
  138. var defaults$2 = function(options, defaults3) {
  139. options = options || {};
  140. Object.keys(defaults3).forEach(function(key) {
  141. if (typeof options[key] === "undefined") {
  142. options[key] = clone(defaults3[key]);
  143. }
  144. });
  145. return options;
  146. };
  147. var combining$2 = [
  148. [768, 879],
  149. [1155, 1158],
  150. [1160, 1161],
  151. [1425, 1469],
  152. [1471, 1471],
  153. [1473, 1474],
  154. [1476, 1477],
  155. [1479, 1479],
  156. [1536, 1539],
  157. [1552, 1557],
  158. [1611, 1630],
  159. [1648, 1648],
  160. [1750, 1764],
  161. [1767, 1768],
  162. [1770, 1773],
  163. [1807, 1807],
  164. [1809, 1809],
  165. [1840, 1866],
  166. [1958, 1968],
  167. [2027, 2035],
  168. [2305, 2306],
  169. [2364, 2364],
  170. [2369, 2376],
  171. [2381, 2381],
  172. [2385, 2388],
  173. [2402, 2403],
  174. [2433, 2433],
  175. [2492, 2492],
  176. [2497, 2500],
  177. [2509, 2509],
  178. [2530, 2531],
  179. [2561, 2562],
  180. [2620, 2620],
  181. [2625, 2626],
  182. [2631, 2632],
  183. [2635, 2637],
  184. [2672, 2673],
  185. [2689, 2690],
  186. [2748, 2748],
  187. [2753, 2757],
  188. [2759, 2760],
  189. [2765, 2765],
  190. [2786, 2787],
  191. [2817, 2817],
  192. [2876, 2876],
  193. [2879, 2879],
  194. [2881, 2883],
  195. [2893, 2893],
  196. [2902, 2902],
  197. [2946, 2946],
  198. [3008, 3008],
  199. [3021, 3021],
  200. [3134, 3136],
  201. [3142, 3144],
  202. [3146, 3149],
  203. [3157, 3158],
  204. [3260, 3260],
  205. [3263, 3263],
  206. [3270, 3270],
  207. [3276, 3277],
  208. [3298, 3299],
  209. [3393, 3395],
  210. [3405, 3405],
  211. [3530, 3530],
  212. [3538, 3540],
  213. [3542, 3542],
  214. [3633, 3633],
  215. [3636, 3642],
  216. [3655, 3662],
  217. [3761, 3761],
  218. [3764, 3769],
  219. [3771, 3772],
  220. [3784, 3789],
  221. [3864, 3865],
  222. [3893, 3893],
  223. [3895, 3895],
  224. [3897, 3897],
  225. [3953, 3966],
  226. [3968, 3972],
  227. [3974, 3975],
  228. [3984, 3991],
  229. [3993, 4028],
  230. [4038, 4038],
  231. [4141, 4144],
  232. [4146, 4146],
  233. [4150, 4151],
  234. [4153, 4153],
  235. [4184, 4185],
  236. [4448, 4607],
  237. [4959, 4959],
  238. [5906, 5908],
  239. [5938, 5940],
  240. [5970, 5971],
  241. [6002, 6003],
  242. [6068, 6069],
  243. [6071, 6077],
  244. [6086, 6086],
  245. [6089, 6099],
  246. [6109, 6109],
  247. [6155, 6157],
  248. [6313, 6313],
  249. [6432, 6434],
  250. [6439, 6440],
  251. [6450, 6450],
  252. [6457, 6459],
  253. [6679, 6680],
  254. [6912, 6915],
  255. [6964, 6964],
  256. [6966, 6970],
  257. [6972, 6972],
  258. [6978, 6978],
  259. [7019, 7027],
  260. [7616, 7626],
  261. [7678, 7679],
  262. [8203, 8207],
  263. [8234, 8238],
  264. [8288, 8291],
  265. [8298, 8303],
  266. [8400, 8431],
  267. [12330, 12335],
  268. [12441, 12442],
  269. [43014, 43014],
  270. [43019, 43019],
  271. [43045, 43046],
  272. [64286, 64286],
  273. [65024, 65039],
  274. [65056, 65059],
  275. [65279, 65279],
  276. [65529, 65531],
  277. [68097, 68099],
  278. [68101, 68102],
  279. [68108, 68111],
  280. [68152, 68154],
  281. [68159, 68159],
  282. [119143, 119145],
  283. [119155, 119170],
  284. [119173, 119179],
  285. [119210, 119213],
  286. [119362, 119364],
  287. [917505, 917505],
  288. [917536, 917631],
  289. [917760, 917999]
  290. ];
  291. var defaults$1 = defaults$2;
  292. var combining$1 = combining$2;
  293. var DEFAULTS$1 = {
  294. nul: 0,
  295. control: 0
  296. };
  297. wcwidth$3.exports = function wcwidth(str) {
  298. return wcswidth$1(str, DEFAULTS$1);
  299. };
  300. wcwidth$3.exports.config = function(opts) {
  301. opts = defaults$1(opts || {}, DEFAULTS$1);
  302. return function wcwidth3(str) {
  303. return wcswidth$1(str, opts);
  304. };
  305. };
  306. function wcswidth$1(str, opts) {
  307. if (typeof str !== "string")
  308. return wcwidth$1(str, opts);
  309. var s = 0;
  310. for (var i = 0; i < str.length; i++) {
  311. var n = wcwidth$1(str.charCodeAt(i), opts);
  312. if (n < 0)
  313. return -1;
  314. s += n;
  315. }
  316. return s;
  317. }
  318. function wcwidth$1(ucs, opts) {
  319. if (ucs === 0)
  320. return opts.nul;
  321. if (ucs < 32 || ucs >= 127 && ucs < 160)
  322. return opts.control;
  323. if (bisearch$1(ucs))
  324. return 0;
  325. return 1 + (ucs >= 4352 && (ucs <= 4447 || // Hangul Jamo init. consonants
  326. ucs == 9001 || ucs == 9002 || ucs >= 11904 && ucs <= 42191 && ucs != 12351 || // CJK ... Yi
  327. ucs >= 44032 && ucs <= 55203 || // Hangul Syllables
  328. ucs >= 63744 && ucs <= 64255 || // CJK Compatibility Ideographs
  329. ucs >= 65040 && ucs <= 65049 || // Vertical forms
  330. ucs >= 65072 && ucs <= 65135 || // CJK Compatibility Forms
  331. ucs >= 65280 && ucs <= 65376 || // Fullwidth Forms
  332. ucs >= 65504 && ucs <= 65510 || ucs >= 131072 && ucs <= 196605 || ucs >= 196608 && ucs <= 262141));
  333. }
  334. function bisearch$1(ucs) {
  335. var min = 0;
  336. var max = combining$1.length - 1;
  337. var mid;
  338. if (ucs < combining$1[0][0] || ucs > combining$1[max][1])
  339. return false;
  340. while (max >= min) {
  341. mid = Math.floor((min + max) / 2);
  342. if (ucs > combining$1[mid][1])
  343. min = mid + 1;
  344. else if (ucs < combining$1[mid][0])
  345. max = mid - 1;
  346. else
  347. return true;
  348. }
  349. return false;
  350. }
  351. var wcwidthExports = wcwidth$3.exports;
  352. const wcwidth$2 = /* @__PURE__ */ getDefaultExportFromCjs(wcwidthExports);
  353. function _typeof(obj) {
  354. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  355. _typeof = function(obj2) {
  356. return typeof obj2;
  357. };
  358. } else {
  359. _typeof = function(obj2) {
  360. return obj2 && typeof Symbol === "function" && obj2.constructor === Symbol && obj2 !== Symbol.prototype ? "symbol" : typeof obj2;
  361. };
  362. }
  363. return _typeof(obj);
  364. }
  365. function createCommonjsModule(fn, module) {
  366. return module = { exports: {} }, fn(module, module.exports), module.exports;
  367. }
  368. var clone_1 = createCommonjsModule(function(module) {
  369. var clone2 = function() {
  370. function clone3(parent, circular, depth, prototype) {
  371. if (_typeof(circular) === "object") {
  372. depth = circular.depth;
  373. prototype = circular.prototype;
  374. circular.filter;
  375. circular = circular.circular;
  376. }
  377. var allParents = [];
  378. var allChildren = [];
  379. var useBuffer = typeof Buffer != "undefined";
  380. if (typeof circular == "undefined")
  381. circular = true;
  382. if (typeof depth == "undefined")
  383. depth = Infinity;
  384. function _clone(parent2, depth2) {
  385. if (parent2 === null)
  386. return null;
  387. if (depth2 == 0)
  388. return parent2;
  389. var child;
  390. var proto;
  391. if (_typeof(parent2) != "object") {
  392. return parent2;
  393. }
  394. if (clone3.__isArray(parent2)) {
  395. child = [];
  396. } else if (clone3.__isRegExp(parent2)) {
  397. child = new RegExp(parent2.source, __getRegExpFlags(parent2));
  398. if (parent2.lastIndex)
  399. child.lastIndex = parent2.lastIndex;
  400. } else if (clone3.__isDate(parent2)) {
  401. child = new Date(parent2.getTime());
  402. } else if (useBuffer && Buffer.isBuffer(parent2)) {
  403. if (Buffer.allocUnsafe) {
  404. child = Buffer.allocUnsafe(parent2.length);
  405. } else {
  406. child = new Buffer(parent2.length);
  407. }
  408. parent2.copy(child);
  409. return child;
  410. } else {
  411. if (typeof prototype == "undefined") {
  412. proto = Object.getPrototypeOf(parent2);
  413. child = Object.create(proto);
  414. } else {
  415. child = Object.create(prototype);
  416. proto = prototype;
  417. }
  418. }
  419. if (circular) {
  420. var index = allParents.indexOf(parent2);
  421. if (index != -1) {
  422. return allChildren[index];
  423. }
  424. allParents.push(parent2);
  425. allChildren.push(child);
  426. }
  427. for (var i in parent2) {
  428. var attrs;
  429. if (proto) {
  430. attrs = Object.getOwnPropertyDescriptor(proto, i);
  431. }
  432. if (attrs && attrs.set == null) {
  433. continue;
  434. }
  435. child[i] = _clone(parent2[i], depth2 - 1);
  436. }
  437. return child;
  438. }
  439. return _clone(parent, depth);
  440. }
  441. clone3.clonePrototype = function clonePrototype(parent) {
  442. if (parent === null)
  443. return null;
  444. var c = function c2() {
  445. };
  446. c.prototype = parent;
  447. return new c();
  448. };
  449. function __objToStr(o) {
  450. return Object.prototype.toString.call(o);
  451. }
  452. clone3.__objToStr = __objToStr;
  453. function __isDate(o) {
  454. return _typeof(o) === "object" && __objToStr(o) === "[object Date]";
  455. }
  456. clone3.__isDate = __isDate;
  457. function __isArray(o) {
  458. return _typeof(o) === "object" && __objToStr(o) === "[object Array]";
  459. }
  460. clone3.__isArray = __isArray;
  461. function __isRegExp(o) {
  462. return _typeof(o) === "object" && __objToStr(o) === "[object RegExp]";
  463. }
  464. clone3.__isRegExp = __isRegExp;
  465. function __getRegExpFlags(re) {
  466. var flags = "";
  467. if (re.global)
  468. flags += "g";
  469. if (re.ignoreCase)
  470. flags += "i";
  471. if (re.multiline)
  472. flags += "m";
  473. return flags;
  474. }
  475. clone3.__getRegExpFlags = __getRegExpFlags;
  476. return clone3;
  477. }();
  478. if (module.exports) {
  479. module.exports = clone2;
  480. }
  481. });
  482. var defaults = function defaults2(options, _defaults) {
  483. options = options || {};
  484. Object.keys(_defaults).forEach(function(key) {
  485. if (typeof options[key] === "undefined") {
  486. options[key] = clone_1(_defaults[key]);
  487. }
  488. });
  489. return options;
  490. };
  491. var combining = [[768, 879], [1155, 1158], [1160, 1161], [1425, 1469], [1471, 1471], [1473, 1474], [1476, 1477], [1479, 1479], [1536, 1539], [1552, 1557], [1611, 1630], [1648, 1648], [1750, 1764], [1767, 1768], [1770, 1773], [1807, 1807], [1809, 1809], [1840, 1866], [1958, 1968], [2027, 2035], [2305, 2306], [2364, 2364], [2369, 2376], [2381, 2381], [2385, 2388], [2402, 2403], [2433, 2433], [2492, 2492], [2497, 2500], [2509, 2509], [2530, 2531], [2561, 2562], [2620, 2620], [2625, 2626], [2631, 2632], [2635, 2637], [2672, 2673], [2689, 2690], [2748, 2748], [2753, 2757], [2759, 2760], [2765, 2765], [2786, 2787], [2817, 2817], [2876, 2876], [2879, 2879], [2881, 2883], [2893, 2893], [2902, 2902], [2946, 2946], [3008, 3008], [3021, 3021], [3134, 3136], [3142, 3144], [3146, 3149], [3157, 3158], [3260, 3260], [3263, 3263], [3270, 3270], [3276, 3277], [3298, 3299], [3393, 3395], [3405, 3405], [3530, 3530], [3538, 3540], [3542, 3542], [3633, 3633], [3636, 3642], [3655, 3662], [3761, 3761], [3764, 3769], [3771, 3772], [3784, 3789], [3864, 3865], [3893, 3893], [3895, 3895], [3897, 3897], [3953, 3966], [3968, 3972], [3974, 3975], [3984, 3991], [3993, 4028], [4038, 4038], [4141, 4144], [4146, 4146], [4150, 4151], [4153, 4153], [4184, 4185], [4448, 4607], [4959, 4959], [5906, 5908], [5938, 5940], [5970, 5971], [6002, 6003], [6068, 6069], [6071, 6077], [6086, 6086], [6089, 6099], [6109, 6109], [6155, 6157], [6313, 6313], [6432, 6434], [6439, 6440], [6450, 6450], [6457, 6459], [6679, 6680], [6912, 6915], [6964, 6964], [6966, 6970], [6972, 6972], [6978, 6978], [7019, 7027], [7616, 7626], [7678, 7679], [8203, 8207], [8234, 8238], [8288, 8291], [8298, 8303], [8400, 8431], [12330, 12335], [12441, 12442], [43014, 43014], [43019, 43019], [43045, 43046], [64286, 64286], [65024, 65039], [65056, 65059], [65279, 65279], [65529, 65531], [68097, 68099], [68101, 68102], [68108, 68111], [68152, 68154], [68159, 68159], [119143, 119145], [119155, 119170], [119173, 119179], [119210, 119213], [119362, 119364], [917505, 917505], [917536, 917631], [917760, 917999]];
  492. var DEFAULTS = {
  493. nul: 0,
  494. control: 0
  495. };
  496. var config = function config2(opts) {
  497. opts = defaults(opts || {}, DEFAULTS);
  498. return function wcwidth3(str) {
  499. return wcswidth(str, opts);
  500. };
  501. };
  502. function wcswidth(str, opts) {
  503. if (typeof str !== "string")
  504. return wcwidth2(str, opts);
  505. var s = 0;
  506. for (var i = 0; i < str.length; i++) {
  507. var n = wcwidth2(str.charCodeAt(i), opts);
  508. if (n < 0)
  509. return -1;
  510. s += n;
  511. }
  512. return s;
  513. }
  514. function wcwidth2(ucs, opts) {
  515. if (ucs === 0)
  516. return opts.nul;
  517. if (ucs < 32 || ucs >= 127 && ucs < 160)
  518. return opts.control;
  519. if (bisearch(ucs))
  520. return 0;
  521. return 1 + (ucs >= 4352 && (ucs <= 4447 || // Hangul Jamo init. consonants
  522. ucs == 9001 || ucs == 9002 || ucs >= 11904 && ucs <= 42191 && ucs != 12351 || // CJK ... Yi
  523. ucs >= 44032 && ucs <= 55203 || // Hangul Syllables
  524. ucs >= 63744 && ucs <= 64255 || // CJK Compatibility Ideographs
  525. ucs >= 65040 && ucs <= 65049 || // Vertical forms
  526. ucs >= 65072 && ucs <= 65135 || // CJK Compatibility Forms
  527. ucs >= 65280 && ucs <= 65376 || // Fullwidth Forms
  528. ucs >= 65504 && ucs <= 65510 || ucs >= 131072 && ucs <= 196605 || ucs >= 196608 && ucs <= 262141));
  529. }
  530. function bisearch(ucs) {
  531. var min = 0;
  532. var max = combining.length - 1;
  533. var mid;
  534. if (ucs < combining[0][0] || ucs > combining[max][1])
  535. return false;
  536. while (max >= min) {
  537. mid = Math.floor((min + max) / 2);
  538. if (ucs > combining[mid][1])
  539. min = mid + 1;
  540. else if (ucs < combining[mid][0])
  541. max = mid - 1;
  542. else
  543. return true;
  544. }
  545. return false;
  546. }
  547. var _pad;
  548. _pad = function pad(text, length, options) {
  549. var escapecolor, invert, padlength, textnocolors;
  550. if (options == null) {
  551. options = {};
  552. }
  553. invert = typeof text === "number";
  554. if (invert) {
  555. var _ref = [text, length];
  556. length = _ref[0];
  557. text = _ref[1];
  558. }
  559. if (typeof options === "string") {
  560. options = {
  561. "char": options
  562. };
  563. }
  564. if (options["char"] == null) {
  565. options["char"] = " ";
  566. }
  567. if (options.strip == null) {
  568. options.strip = false;
  569. }
  570. if (typeof text !== "string") {
  571. text = text.toString();
  572. }
  573. textnocolors = null;
  574. _pad = "";
  575. if (options.colors) {
  576. escapecolor = /\x1B\[(?:[0-9]{1,2}(?:;[0-9]{1,2})?)?[m|K]/g;
  577. textnocolors = text.replace(escapecolor, "");
  578. }
  579. padlength = options.fixed_width ? length - (textnocolors || text).length : length - config(options.wcwidth_options)(textnocolors || text);
  580. if (padlength < 0) {
  581. if (options.strip) {
  582. if (invert) {
  583. return text.substr(length * -1);
  584. } else {
  585. return text.substr(0, length);
  586. }
  587. }
  588. return text;
  589. }
  590. _pad += options["char"].repeat(padlength);
  591. if (invert) {
  592. return _pad + text;
  593. } else {
  594. return text + _pad;
  595. }
  596. };
  597. var _pad$1 = _pad;
  598. function assert(condition, message) {
  599. if (!condition) {
  600. throw new Error(message || "Assertion failed");
  601. }
  602. }
  603. function compareNestedStringArray(x, y) {
  604. if (x instanceof Array) {
  605. if (x.length != y.length) {
  606. return false;
  607. } else {
  608. return x.filter((v, i) => !compareNestedStringArray(v, y[i])).length == 0;
  609. }
  610. } else {
  611. return x === y;
  612. }
  613. }
  614. function deduplicateDays(timetable) {
  615. let dedup = true;
  616. timetable.forEach((days) => {
  617. if (days.length > 1) {
  618. days.slice(1).forEach((day) => {
  619. if (!compareNestedStringArray(days[0], day)) {
  620. dedup = false;
  621. }
  622. });
  623. }
  624. });
  625. if (dedup) {
  626. return new Map(
  627. Array.from(timetable.entries()).map(([station, days]) => [
  628. station,
  629. [days[0]]
  630. ])
  631. );
  632. } else {
  633. return timetable;
  634. }
  635. }
  636. function luaifyNestedStringArray(array, padding = 0) {
  637. if (array instanceof Array) {
  638. return `{ ${array.map((child) => luaifyNestedStringArray(child, padding)).join(", ")} }`;
  639. } else if (array == "nil") {
  640. return _pad$1(padding, `nil`);
  641. } else {
  642. return _pad$1(padding, `'${array}'`);
  643. }
  644. }
  645. function luaifyTimetable(timetable, padding) {
  646. timetable = deduplicateDays(timetable);
  647. return ` {
  648. stations = ${luaifyNestedStringArray([...timetable.keys()])},
  649. data = {
  650. ${Array.from(timetable).map(
  651. ([key, value]) => ` ${_pad$1(`['${key}']`, padding)} = ${luaifyNestedStringArray(value, 7)},`
  652. ).join("\n")}
  653. }
  654. }`;
  655. }
  656. function getItemsTextByIndex(items, terminalOffset, dayOffset, closed) {
  657. if (terminalOffset instanceof Array) {
  658. return terminalOffset.map(
  659. (index) => items[index + dayOffset].innerHTML.trim().replace("--", closed ? "nil" : "")
  660. );
  661. } else {
  662. return items[terminalOffset + dayOffset].innerHTML.trim().replace("--", closed ? "nil" : "");
  663. }
  664. }
  665. const CRT = {
  666. bootstrap: () => {
  667. document.querySelectorAll(".line-time-table").forEach((table) => {
  668. let caption = table.querySelector("caption");
  669. let copyData = () => {
  670. let [days, termini, child_termini] = [0, 1, 2].map(
  671. (r) => Array.from(
  672. table.tHead.rows[r].querySelectorAll(".bg-f7f7f7")
  673. ).filter((th) => th.innerText.trim().length > 2)
  674. );
  675. if (days.length == 0) {
  676. days.push(document.createElement("th"));
  677. days[0].colSpan = termini.map((g) => g.colSpan).reduce((p, c) => p + c, 0);
  678. }
  679. assert([1, 2].includes(days.length), "Invalid # of days.");
  680. assert(
  681. termini.length % days.length == 0 && termini.length >= 2,
  682. "Invalid # of termini."
  683. );
  684. assert(
  685. child_termini.length % days.length == 0 && child_termini.length >= 2,
  686. "Invalid # of child termini."
  687. );
  688. let dayWidth = days[0].colSpan;
  689. let dayOffsets = [];
  690. for (let i = 0; i < days.length; i++) {
  691. assert(days[i].colSpan == dayWidth);
  692. dayOffsets.push(i * dayWidth);
  693. }
  694. let terminalOffsets = [1, 2, 3, 4];
  695. if (termini.length / days.length == 2 && child_termini.length / termini.length == 2)
  696. ;
  697. else if (termini.length / days.length >= 2) {
  698. terminalOffsets = [[], [], [], []];
  699. let i = 0;
  700. let dayWidth2 = days[0].colSpan;
  701. for (var orientation of termini) {
  702. let orientationWidth = orientation.colSpan;
  703. if (orientation.innerHTML.trim().startsWith("首班车")) {
  704. for (let j = 0; j < orientationWidth; j++) {
  705. let terminusText = child_termini[i + j].innerHTML.trim();
  706. if (terminusText.includes("↓") || terminusText.includes("内环")) {
  707. terminalOffsets[0].push(i + j + 1);
  708. } else {
  709. terminalOffsets[1].push(i + j + 1);
  710. }
  711. }
  712. } else {
  713. for (let j = 0; j < orientationWidth; j++) {
  714. let terminusText = child_termini[i + j].innerHTML.trim();
  715. if (terminusText.includes("↓") || terminusText.includes("内环")) {
  716. terminalOffsets[2].push(i + j + 1);
  717. } else {
  718. terminalOffsets[3].push(i + j + 1);
  719. }
  720. }
  721. }
  722. i += orientationWidth;
  723. if (i >= dayWidth2) {
  724. break;
  725. }
  726. }
  727. for (let i2 = 0; i2 < terminalOffsets.length; i2++) {
  728. const v = terminalOffsets[i2];
  729. terminalOffsets[i2] = v instanceof Array ? v[0] : v;
  730. }
  731. } else {
  732. throw termini.length;
  733. }
  734. let rows = Array.from(table.tBodies[0].rows).map(
  735. (row2) => Array.from(row2.cells)
  736. );
  737. let timetable = /* @__PURE__ */ new Map();
  738. for (var row of rows) {
  739. if (row.length == 0) {
  740. break;
  741. }
  742. let name = row[0].innerHTML.trim().replace("航站楼", "");
  743. if (!name || name == "--") {
  744. break;
  745. }
  746. timetable.set(
  747. name,
  748. dayOffsets.map((dayOffset) => {
  749. let closed = (
  750. // 判断车站是否关闭
  751. row.slice(1).filter((cell) => cell.innerHTML.trim().length > 2).length == 0
  752. );
  753. return [
  754. [
  755. getItemsTextByIndex(
  756. row,
  757. terminalOffsets[0],
  758. dayOffset,
  759. closed
  760. ),
  761. getItemsTextByIndex(
  762. row,
  763. terminalOffsets[1],
  764. dayOffset,
  765. closed
  766. )
  767. ],
  768. [
  769. getItemsTextByIndex(
  770. row,
  771. terminalOffsets[2],
  772. dayOffset,
  773. closed
  774. ),
  775. getItemsTextByIndex(
  776. row,
  777. terminalOffsets[3],
  778. dayOffset,
  779. closed
  780. )
  781. ]
  782. ];
  783. })
  784. );
  785. }
  786. navigator.clipboard.writeText(
  787. luaifyTimetable(
  788. timetable,
  789. Math.max(...[...timetable.keys()].map((str) => wcwidth$2(str))) + 4
  790. )
  791. );
  792. };
  793. let button = document.createElement("a");
  794. button.append("导出");
  795. button.addEventListener("click", copyData, false);
  796. button.style.cursor = "pointer";
  797. button.style.position = "absolute";
  798. button.style.zIndex = "1";
  799. button.style.color = "white";
  800. button.style.opacity = ".75";
  801. button.style.paddingInlineStart = ".5em";
  802. caption == null ? void 0 : caption.appendChild(button);
  803. });
  804. }
  805. };
  806. switch (new URL(document.URL).hostname) {
  807. case "www.cqmetro.cn":
  808. CRT.bootstrap();
  809. break;
  810. }
  811.  
  812. })();