🏠 Home 

timetable2lua

Export timetable data as lua table.


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