Embed Me!

Embed video, images from links.

  1. // ==UserScript==
  2. // @name Embed Me!
  3. // @version 0.4.0
  4. // @description Embed video, images from links.
  5. // @license MIT
  6. // @author eight04 <eight04@gmail.com>
  7. // @homepageURL https://github.com/eight04/embed-me
  8. // @supportURL https://github.com/eight04/embed-me/issues
  9. // @namespace eight04.blogspot.com
  10. // @grant GM_addStyle
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_deleteValue
  15. // @grant GM_xmlhttpRequest
  16. // @require https://gf.qytechs.cn/scripts/371339-gm-webextpref/code/GM_webextPref.js?version=705415
  17. // @connect gfycat.com
  18. // @connect soundcloud.com
  19. // @connect www.youtube.com
  20. // @noframes true
  21. // @include *
  22. // ==/UserScript==
  23.  
  24. const _module_exports_$9 = {};
  25. Object.defineProperty(_module_exports_$9, "__esModule", { value: true });
  26. _module_exports_$9.parseRotation = _module_exports_$9.parseRotationName = _module_exports_$9.Rotation = _module_exports_$9.parsePiece = _module_exports_$9.parsePieceName = _module_exports_$9.isMinoPiece = _module_exports_$9.Piece = void 0;
  27. var Piece;
  28. (function (Piece) {
  29. Piece[Piece["Empty"] = 0] = "Empty";
  30. Piece[Piece["I"] = 1] = "I";
  31. Piece[Piece["L"] = 2] = "L";
  32. Piece[Piece["O"] = 3] = "O";
  33. Piece[Piece["Z"] = 4] = "Z";
  34. Piece[Piece["T"] = 5] = "T";
  35. Piece[Piece["J"] = 6] = "J";
  36. Piece[Piece["S"] = 7] = "S";
  37. Piece[Piece["Gray"] = 8] = "Gray";
  38. })(Piece = _module_exports_$9.Piece || (_module_exports_$9.Piece = {}));
  39. function isMinoPiece(piece) {
  40. return piece !== Piece.Empty && piece !== Piece.Gray;
  41. }
  42. _module_exports_$9.isMinoPiece = isMinoPiece;
  43. function parsePieceName(piece) {
  44. switch (piece) {
  45. case Piece.I:
  46. return 'I';
  47. case Piece.L:
  48. return 'L';
  49. case Piece.O:
  50. return 'O';
  51. case Piece.Z:
  52. return 'Z';
  53. case Piece.T:
  54. return 'T';
  55. case Piece.J:
  56. return 'J';
  57. case Piece.S:
  58. return 'S';
  59. case Piece.Gray:
  60. return 'X';
  61. case Piece.Empty:
  62. return '_';
  63. }
  64. throw new Error("Unknown piece: ".concat(piece));
  65. }
  66. _module_exports_$9.parsePieceName = parsePieceName;
  67. function parsePiece(piece) {
  68. switch (piece.toUpperCase()) {
  69. case 'I':
  70. return Piece.I;
  71. case 'L':
  72. return Piece.L;
  73. case 'O':
  74. return Piece.O;
  75. case 'Z':
  76. return Piece.Z;
  77. case 'T':
  78. return Piece.T;
  79. case 'J':
  80. return Piece.J;
  81. case 'S':
  82. return Piece.S;
  83. case 'X':
  84. case 'GRAY':
  85. return Piece.Gray;
  86. case ' ':
  87. case '_':
  88. case 'EMPTY':
  89. return Piece.Empty;
  90. }
  91. throw new Error("Unknown piece: ".concat(piece));
  92. }
  93. _module_exports_$9.parsePiece = parsePiece;
  94. var Rotation;
  95. (function (Rotation) {
  96. Rotation[Rotation["Spawn"] = 0] = "Spawn";
  97. Rotation[Rotation["Right"] = 1] = "Right";
  98. Rotation[Rotation["Reverse"] = 2] = "Reverse";
  99. Rotation[Rotation["Left"] = 3] = "Left";
  100. })(Rotation = _module_exports_$9.Rotation || (_module_exports_$9.Rotation = {}));
  101. function parseRotationName(rotation) {
  102. switch (rotation) {
  103. case Rotation.Spawn:
  104. return 'spawn';
  105. case Rotation.Left:
  106. return 'left';
  107. case Rotation.Right:
  108. return 'right';
  109. case Rotation.Reverse:
  110. return 'reverse';
  111. }
  112. throw new Error("Unknown rotation: ".concat(rotation));
  113. }
  114. _module_exports_$9.parseRotationName = parseRotationName;
  115. function parseRotation(rotation) {
  116. switch (rotation.toLowerCase()) {
  117. case 'spawn':
  118. return Rotation.Spawn;
  119. case 'left':
  120. return Rotation.Left;
  121. case 'right':
  122. return Rotation.Right;
  123. case 'reverse':
  124. return Rotation.Reverse;
  125. }
  126. throw new Error("Unknown rotation: ".concat(rotation));
  127. }
  128. _module_exports_$9.parseRotation = parseRotation;
  129.  
  130. const _module_exports_$8 = {};
  131. Object.defineProperty(_module_exports_$8, "__esModule", { value: true });
  132. _module_exports_$8.getPieces = _module_exports_$8.getBlocks = _module_exports_$8.getBlockXYs = _module_exports_$8.getBlockPositions = _module_exports_$8.PlayField = _module_exports_$8.InnerField = _module_exports_$8.createInnerField = _module_exports_$8.createNewInnerField = void 0;
  133. var FieldConstants$2 = {
  134. Width: 10,
  135. Height: 23,
  136. PlayBlocks: 23 * 10, // Height * Width
  137. };
  138. function createNewInnerField() {
  139. return new InnerField({});
  140. }
  141. _module_exports_$8.createNewInnerField = createNewInnerField;
  142. function createInnerField(field) {
  143. var innerField = new InnerField({});
  144. for (var y = -1; y < FieldConstants$2.Height; y += 1) {
  145. for (var x = 0; x < FieldConstants$2.Width; x += 1) {
  146. var at = field.at(x, y);
  147. innerField.setNumberAt(x, y, (0, _module_exports_$9.parsePiece)(at));
  148. }
  149. }
  150. return innerField;
  151. }
  152. _module_exports_$8.createInnerField = createInnerField;
  153. var InnerField = /** @class */ (function () {
  154. function InnerField(_a) {
  155. var _b = _a.field, field = _b === void 0 ? InnerField.create(FieldConstants$2.PlayBlocks) : _b, _c = _a.garbage, garbage = _c === void 0 ? InnerField.create(FieldConstants$2.Width) : _c;
  156. this.field = field;
  157. this.garbage = garbage;
  158. }
  159. InnerField.create = function (length) {
  160. return new PlayField({ length: length });
  161. };
  162. InnerField.prototype.fill = function (operation) {
  163. this.field.fill(operation);
  164. };
  165. InnerField.prototype.fillAll = function (positions, type) {
  166. this.field.fillAll(positions, type);
  167. };
  168. InnerField.prototype.canFill = function (piece, rotation, x, y) {
  169. var _this = this;
  170. var positions = getBlockPositions(piece, rotation, x, y);
  171. return positions.every(function (_a) {
  172. var px = _a[0], py = _a[1];
  173. return 0 <= px && px < 10
  174. && 0 <= py && py < FieldConstants$2.Height
  175. && _this.getNumberAt(px, py) === _module_exports_$9.Piece.Empty;
  176. });
  177. };
  178. InnerField.prototype.canFillAll = function (positions) {
  179. var _this = this;
  180. return positions.every(function (_a) {
  181. var x = _a.x, y = _a.y;
  182. return 0 <= x && x < 10
  183. && 0 <= y && y < FieldConstants$2.Height
  184. && _this.getNumberAt(x, y) === _module_exports_$9.Piece.Empty;
  185. });
  186. };
  187. InnerField.prototype.isOnGround = function (piece, rotation, x, y) {
  188. return !this.canFill(piece, rotation, x, y - 1);
  189. };
  190. InnerField.prototype.clearLine = function () {
  191. this.field.clearLine();
  192. };
  193. InnerField.prototype.riseGarbage = function () {
  194. this.field.up(this.garbage);
  195. this.garbage.clearAll();
  196. };
  197. InnerField.prototype.mirror = function () {
  198. this.field.mirror();
  199. };
  200. InnerField.prototype.shiftToLeft = function () {
  201. this.field.shiftToLeft();
  202. };
  203. InnerField.prototype.shiftToRight = function () {
  204. this.field.shiftToRight();
  205. };
  206. InnerField.prototype.shiftToUp = function () {
  207. this.field.shiftToUp();
  208. };
  209. InnerField.prototype.shiftToBottom = function () {
  210. this.field.shiftToBottom();
  211. };
  212. InnerField.prototype.copy = function () {
  213. return new InnerField({ field: this.field.copy(), garbage: this.garbage.copy() });
  214. };
  215. InnerField.prototype.equals = function (other) {
  216. return this.field.equals(other.field) && this.garbage.equals(other.garbage);
  217. };
  218. InnerField.prototype.addNumber = function (x, y, value) {
  219. if (0 <= y) {
  220. this.field.addOffset(x, y, value);
  221. }
  222. else {
  223. this.garbage.addOffset(x, -(y + 1), value);
  224. }
  225. };
  226. InnerField.prototype.setNumberFieldAt = function (index, value) {
  227. this.field.setAt(index, value);
  228. };
  229. InnerField.prototype.setNumberGarbageAt = function (index, value) {
  230. this.garbage.setAt(index, value);
  231. };
  232. InnerField.prototype.setNumberAt = function (x, y, value) {
  233. return 0 <= y ? this.field.set(x, y, value) : this.garbage.set(x, -(y + 1), value);
  234. };
  235. InnerField.prototype.getNumberAt = function (x, y) {
  236. return 0 <= y ? this.field.get(x, y) : this.garbage.get(x, -(y + 1));
  237. };
  238. InnerField.prototype.getNumberAtIndex = function (index, isField) {
  239. if (isField) {
  240. return this.getNumberAt(index % 10, Math.floor(index / 10));
  241. }
  242. return this.getNumberAt(index % 10, -(Math.floor(index / 10) + 1));
  243. };
  244. InnerField.prototype.toFieldNumberArray = function () {
  245. return this.field.toArray();
  246. };
  247. InnerField.prototype.toGarbageNumberArray = function () {
  248. return this.garbage.toArray();
  249. };
  250. return InnerField;
  251. }());
  252. _module_exports_$8.InnerField = InnerField;
  253. var PlayField = /** @class */ (function () {
  254. function PlayField(_a) {
  255. var pieces = _a.pieces, _b = _a.length, length = _b === void 0 ? FieldConstants$2.PlayBlocks : _b;
  256. if (pieces !== undefined) {
  257. this.pieces = pieces;
  258. }
  259. else {
  260. this.pieces = Array.from({ length: length }).map(function () { return _module_exports_$9.Piece.Empty; });
  261. }
  262. this.length = length;
  263. }
  264. PlayField.load = function () {
  265. var lines = [];
  266. for (var _i = 0; _i < arguments.length; _i++) {
  267. lines[_i] = arguments[_i];
  268. }
  269. var blocks = lines.join('').trim();
  270. return PlayField.loadInner(blocks);
  271. };
  272. PlayField.loadMinify = function () {
  273. var lines = [];
  274. for (var _i = 0; _i < arguments.length; _i++) {
  275. lines[_i] = arguments[_i];
  276. }
  277. var blocks = lines.join('').trim();
  278. return PlayField.loadInner(blocks, blocks.length);
  279. };
  280. PlayField.loadInner = function (blocks, length) {
  281. var len = length !== undefined ? length : blocks.length;
  282. if (len % 10 !== 0) {
  283. throw new Error('Num of blocks in field should be mod 10');
  284. }
  285. var field = length !== undefined ? new PlayField({ length: length }) : new PlayField({});
  286. for (var index = 0; index < len; index += 1) {
  287. var block = blocks[index];
  288. field.set(index % 10, Math.floor((len - index - 1) / 10), (0, _module_exports_$9.parsePiece)(block));
  289. }
  290. return field;
  291. };
  292. PlayField.prototype.get = function (x, y) {
  293. return this.pieces[x + y * FieldConstants$2.Width];
  294. };
  295. PlayField.prototype.addOffset = function (x, y, value) {
  296. this.pieces[x + y * FieldConstants$2.Width] += value;
  297. };
  298. PlayField.prototype.set = function (x, y, piece) {
  299. this.setAt(x + y * FieldConstants$2.Width, piece);
  300. };
  301. PlayField.prototype.setAt = function (index, piece) {
  302. this.pieces[index] = piece;
  303. };
  304. PlayField.prototype.fill = function (_a) {
  305. var type = _a.type, rotation = _a.rotation, x = _a.x, y = _a.y;
  306. var blocks = getBlocks(type, rotation);
  307. for (var _i = 0, blocks_1 = blocks; _i < blocks_1.length; _i++) {
  308. var block = blocks_1[_i];
  309. var _b = [x + block[0], y + block[1]], nx = _b[0], ny = _b[1];
  310. this.set(nx, ny, type);
  311. }
  312. };
  313. PlayField.prototype.fillAll = function (positions, type) {
  314. for (var _i = 0, positions_1 = positions; _i < positions_1.length; _i++) {
  315. var _a = positions_1[_i], x = _a.x, y = _a.y;
  316. this.set(x, y, type);
  317. }
  318. };
  319. PlayField.prototype.clearLine = function () {
  320. var newField = this.pieces.concat();
  321. var top = this.pieces.length / FieldConstants$2.Width - 1;
  322. for (var y = top; 0 <= y; y -= 1) {
  323. var line = this.pieces.slice(y * FieldConstants$2.Width, (y + 1) * FieldConstants$2.Width);
  324. var isFilled = line.every(function (value) { return value !== _module_exports_$9.Piece.Empty; });
  325. if (isFilled) {
  326. var bottom = newField.slice(0, y * FieldConstants$2.Width);
  327. var over = newField.slice((y + 1) * FieldConstants$2.Width);
  328. newField = bottom.concat(over, Array.from({ length: FieldConstants$2.Width }).map(function () { return _module_exports_$9.Piece.Empty; }));
  329. }
  330. }
  331. this.pieces = newField;
  332. };
  333. PlayField.prototype.up = function (blockUp) {
  334. this.pieces = blockUp.pieces.concat(this.pieces).slice(0, this.length);
  335. };
  336. PlayField.prototype.mirror = function () {
  337. var newField = [];
  338. for (var y = 0; y < this.pieces.length; y += 1) {
  339. var line = this.pieces.slice(y * FieldConstants$2.Width, (y + 1) * FieldConstants$2.Width);
  340. line.reverse();
  341. for (var _i = 0, line_1 = line; _i < line_1.length; _i++) {
  342. var obj = line_1[_i];
  343. newField.push(obj);
  344. }
  345. }
  346. this.pieces = newField;
  347. };
  348. PlayField.prototype.shiftToLeft = function () {
  349. var height = this.pieces.length / 10;
  350. for (var y = 0; y < height; y += 1) {
  351. for (var x = 0; x < FieldConstants$2.Width - 1; x += 1) {
  352. this.pieces[x + y * FieldConstants$2.Width] = this.pieces[x + 1 + y * FieldConstants$2.Width];
  353. }
  354. this.pieces[9 + y * FieldConstants$2.Width] = _module_exports_$9.Piece.Empty;
  355. }
  356. };
  357. PlayField.prototype.shiftToRight = function () {
  358. var height = this.pieces.length / 10;
  359. for (var y = 0; y < height; y += 1) {
  360. for (var x = FieldConstants$2.Width - 1; 1 <= x; x -= 1) {
  361. this.pieces[x + y * FieldConstants$2.Width] = this.pieces[x - 1 + y * FieldConstants$2.Width];
  362. }
  363. this.pieces[y * FieldConstants$2.Width] = _module_exports_$9.Piece.Empty;
  364. }
  365. };
  366. PlayField.prototype.shiftToUp = function () {
  367. var blanks = Array.from({ length: 10 }).map(function () { return _module_exports_$9.Piece.Empty; });
  368. this.pieces = blanks.concat(this.pieces).slice(0, this.length);
  369. };
  370. PlayField.prototype.shiftToBottom = function () {
  371. var blanks = Array.from({ length: 10 }).map(function () { return _module_exports_$9.Piece.Empty; });
  372. this.pieces = this.pieces.slice(10, this.length).concat(blanks);
  373. };
  374. PlayField.prototype.toArray = function () {
  375. return this.pieces.concat();
  376. };
  377. Object.defineProperty(PlayField.prototype, "numOfBlocks", {
  378. get: function () {
  379. return this.pieces.length;
  380. },
  381. enumerable: false,
  382. configurable: true
  383. });
  384. PlayField.prototype.copy = function () {
  385. return new PlayField({ pieces: this.pieces.concat(), length: this.length });
  386. };
  387. PlayField.prototype.toShallowArray = function () {
  388. return this.pieces;
  389. };
  390. PlayField.prototype.clearAll = function () {
  391. this.pieces = this.pieces.map(function () { return _module_exports_$9.Piece.Empty; });
  392. };
  393. PlayField.prototype.equals = function (other) {
  394. if (this.pieces.length !== other.pieces.length) {
  395. return false;
  396. }
  397. for (var index = 0; index < this.pieces.length; index += 1) {
  398. if (this.pieces[index] !== other.pieces[index]) {
  399. return false;
  400. }
  401. }
  402. return true;
  403. };
  404. return PlayField;
  405. }());
  406. _module_exports_$8.PlayField = PlayField;
  407. function getBlockPositions(piece, rotation, x, y) {
  408. return getBlocks(piece, rotation).map(function (position) {
  409. position[0] += x;
  410. position[1] += y;
  411. return position;
  412. });
  413. }
  414. _module_exports_$8.getBlockPositions = getBlockPositions;
  415. function getBlockXYs(piece, rotation, x, y) {
  416. return getBlocks(piece, rotation).map(function (position) {
  417. return { x: position[0] + x, y: position[1] + y };
  418. });
  419. }
  420. _module_exports_$8.getBlockXYs = getBlockXYs;
  421. function getBlocks(piece, rotation) {
  422. var blocks = getPieces(piece);
  423. switch (rotation) {
  424. case _module_exports_$9.Rotation.Spawn:
  425. return blocks;
  426. case _module_exports_$9.Rotation.Left:
  427. return rotateLeft(blocks);
  428. case _module_exports_$9.Rotation.Reverse:
  429. return rotateReverse(blocks);
  430. case _module_exports_$9.Rotation.Right:
  431. return rotateRight(blocks);
  432. }
  433. throw new Error('Unsupported block');
  434. }
  435. _module_exports_$8.getBlocks = getBlocks;
  436. function getPieces(piece) {
  437. switch (piece) {
  438. case _module_exports_$9.Piece.I:
  439. return [[0, 0], [-1, 0], [1, 0], [2, 0]];
  440. case _module_exports_$9.Piece.T:
  441. return [[0, 0], [-1, 0], [1, 0], [0, 1]];
  442. case _module_exports_$9.Piece.O:
  443. return [[0, 0], [1, 0], [0, 1], [1, 1]];
  444. case _module_exports_$9.Piece.L:
  445. return [[0, 0], [-1, 0], [1, 0], [1, 1]];
  446. case _module_exports_$9.Piece.J:
  447. return [[0, 0], [-1, 0], [1, 0], [-1, 1]];
  448. case _module_exports_$9.Piece.S:
  449. return [[0, 0], [-1, 0], [0, 1], [1, 1]];
  450. case _module_exports_$9.Piece.Z:
  451. return [[0, 0], [1, 0], [0, 1], [-1, 1]];
  452. }
  453. throw new Error('Unsupported rotation');
  454. }
  455. _module_exports_$8.getPieces = getPieces;
  456. function rotateRight(positions) {
  457. return positions.map(function (current) { return [current[1], -current[0]]; });
  458. }
  459. function rotateLeft(positions) {
  460. return positions.map(function (current) { return [-current[1], current[0]]; });
  461. }
  462. function rotateReverse(positions) {
  463. return positions.map(function (current) { return [-current[0], -current[1]]; });
  464. }
  465.  
  466. const _module_exports_$7 = {};
  467. Object.defineProperty(_module_exports_$7, "__esModule", { value: true });
  468. _module_exports_$7.Buffer = void 0;
  469. var ENCODE_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  470. var Buffer = /** @class */ (function () {
  471. function Buffer(data) {
  472. if (data === void 0) { data = ''; }
  473. this.values = data.split('').map(decodeToValue);
  474. }
  475. Buffer.prototype.poll = function (max) {
  476. var value = 0;
  477. for (var count = 0; count < max; count += 1) {
  478. var v = this.values.shift();
  479. if (v === undefined) {
  480. throw new Error('Unexpected fumen');
  481. }
  482. value += v * Math.pow(Buffer.tableLength, count);
  483. }
  484. return value;
  485. };
  486. Buffer.prototype.push = function (value, splitCount) {
  487. if (splitCount === void 0) { splitCount = 1; }
  488. var current = value;
  489. for (var count = 0; count < splitCount; count += 1) {
  490. this.values.push(current % Buffer.tableLength);
  491. current = Math.floor(current / Buffer.tableLength);
  492. }
  493. };
  494. Buffer.prototype.merge = function (postBuffer) {
  495. for (var _i = 0, _a = postBuffer.values; _i < _a.length; _i++) {
  496. var value = _a[_i];
  497. this.values.push(value);
  498. }
  499. };
  500. Buffer.prototype.isEmpty = function () {
  501. return this.values.length === 0;
  502. };
  503. Object.defineProperty(Buffer.prototype, "length", {
  504. get: function () {
  505. return this.values.length;
  506. },
  507. enumerable: false,
  508. configurable: true
  509. });
  510. Buffer.prototype.get = function (index) {
  511. return this.values[index];
  512. };
  513. Buffer.prototype.set = function (index, value) {
  514. this.values[index] = value;
  515. };
  516. Buffer.prototype.toString = function () {
  517. return this.values.map(encodeFromValue).join('');
  518. };
  519. Buffer.tableLength = ENCODE_TABLE.length;
  520. return Buffer;
  521. }());
  522. _module_exports_$7.Buffer = Buffer;
  523. function decodeToValue(v) {
  524. return ENCODE_TABLE.indexOf(v);
  525. }
  526. function encodeFromValue(index) {
  527. return ENCODE_TABLE[index];
  528. }
  529.  
  530. const _module_exports_$6 = {};
  531. var __assign$2 = (undefined && undefined.__assign) || function () {
  532. __assign$2 = Object.assign || function(t) {
  533. for (var s, i = 1, n = arguments.length; i < n; i++) {
  534. s = arguments[i];
  535. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  536. t[p] = s[p];
  537. }
  538. return t;
  539. };
  540. return __assign$2.apply(this, arguments);
  541. };
  542. Object.defineProperty(_module_exports_$6, "__esModule", { value: true });
  543. _module_exports_$6.createActionEncoder = _module_exports_$6.createActionDecoder = void 0;
  544. function decodeBool(n) {
  545. return n !== 0;
  546. }
  547. var createActionDecoder = function (width, fieldTop, garbageLine) {
  548. var fieldMaxHeight = fieldTop + garbageLine;
  549. var numFieldBlocks = fieldMaxHeight * width;
  550. function decodePiece(n) {
  551. switch (n) {
  552. case 0:
  553. return _module_exports_$9.Piece.Empty;
  554. case 1:
  555. return _module_exports_$9.Piece.I;
  556. case 2:
  557. return _module_exports_$9.Piece.L;
  558. case 3:
  559. return _module_exports_$9.Piece.O;
  560. case 4:
  561. return _module_exports_$9.Piece.Z;
  562. case 5:
  563. return _module_exports_$9.Piece.T;
  564. case 6:
  565. return _module_exports_$9.Piece.J;
  566. case 7:
  567. return _module_exports_$9.Piece.S;
  568. case 8:
  569. return _module_exports_$9.Piece.Gray;
  570. }
  571. throw new Error('Unexpected piece');
  572. }
  573. function decodeRotation(n) {
  574. switch (n) {
  575. case 0:
  576. return _module_exports_$9.Rotation.Reverse;
  577. case 1:
  578. return _module_exports_$9.Rotation.Right;
  579. case 2:
  580. return _module_exports_$9.Rotation.Spawn;
  581. case 3:
  582. return _module_exports_$9.Rotation.Left;
  583. }
  584. throw new Error('Unexpected rotation');
  585. }
  586. function decodeCoordinate(n, piece, rotation) {
  587. var x = n % width;
  588. var originY = Math.floor(n / 10);
  589. var y = fieldTop - originY - 1;
  590. if (piece === _module_exports_$9.Piece.O && rotation === _module_exports_$9.Rotation.Left) {
  591. x += 1;
  592. y -= 1;
  593. }
  594. else if (piece === _module_exports_$9.Piece.O && rotation === _module_exports_$9.Rotation.Reverse) {
  595. x += 1;
  596. }
  597. else if (piece === _module_exports_$9.Piece.O && rotation === _module_exports_$9.Rotation.Spawn) {
  598. y -= 1;
  599. }
  600. else if (piece === _module_exports_$9.Piece.I && rotation === _module_exports_$9.Rotation.Reverse) {
  601. x += 1;
  602. }
  603. else if (piece === _module_exports_$9.Piece.I && rotation === _module_exports_$9.Rotation.Left) {
  604. y -= 1;
  605. }
  606. else if (piece === _module_exports_$9.Piece.S && rotation === _module_exports_$9.Rotation.Spawn) {
  607. y -= 1;
  608. }
  609. else if (piece === _module_exports_$9.Piece.S && rotation === _module_exports_$9.Rotation.Right) {
  610. x -= 1;
  611. }
  612. else if (piece === _module_exports_$9.Piece.Z && rotation === _module_exports_$9.Rotation.Spawn) {
  613. y -= 1;
  614. }
  615. else if (piece === _module_exports_$9.Piece.Z && rotation === _module_exports_$9.Rotation.Left) {
  616. x += 1;
  617. }
  618. return { x: x, y: y };
  619. }
  620. return {
  621. decode: function (v) {
  622. var value = v;
  623. var type = decodePiece(value % 8);
  624. value = Math.floor(value / 8);
  625. var rotation = decodeRotation(value % 4);
  626. value = Math.floor(value / 4);
  627. var coordinate = decodeCoordinate(value % numFieldBlocks, type, rotation);
  628. value = Math.floor(value / numFieldBlocks);
  629. var isBlockUp = decodeBool(value % 2);
  630. value = Math.floor(value / 2);
  631. var isMirror = decodeBool(value % 2);
  632. value = Math.floor(value / 2);
  633. var isColor = decodeBool(value % 2);
  634. value = Math.floor(value / 2);
  635. var isComment = decodeBool(value % 2);
  636. value = Math.floor(value / 2);
  637. var isLock = !decodeBool(value % 2);
  638. return {
  639. rise: isBlockUp,
  640. mirror: isMirror,
  641. colorize: isColor,
  642. comment: isComment,
  643. lock: isLock,
  644. piece: __assign$2(__assign$2({}, coordinate), { type: type, rotation: rotation }),
  645. };
  646. },
  647. };
  648. };
  649. _module_exports_$6.createActionDecoder = createActionDecoder;
  650. function encodeBool(flag) {
  651. return flag ? 1 : 0;
  652. }
  653. var createActionEncoder = function (width, fieldTop, garbageLine) {
  654. var fieldMaxHeight = fieldTop + garbageLine;
  655. var numFieldBlocks = fieldMaxHeight * width;
  656. function encodePosition(operation) {
  657. var type = operation.type, rotation = operation.rotation;
  658. var x = operation.x;
  659. var y = operation.y;
  660. if (!(0, _module_exports_$9.isMinoPiece)(type)) {
  661. x = 0;
  662. y = 22;
  663. }
  664. else if (type === _module_exports_$9.Piece.O && rotation === _module_exports_$9.Rotation.Left) {
  665. x -= 1;
  666. y += 1;
  667. }
  668. else if (type === _module_exports_$9.Piece.O && rotation === _module_exports_$9.Rotation.Reverse) {
  669. x -= 1;
  670. }
  671. else if (type === _module_exports_$9.Piece.O && rotation === _module_exports_$9.Rotation.Spawn) {
  672. y += 1;
  673. }
  674. else if (type === _module_exports_$9.Piece.I && rotation === _module_exports_$9.Rotation.Reverse) {
  675. x -= 1;
  676. }
  677. else if (type === _module_exports_$9.Piece.I && rotation === _module_exports_$9.Rotation.Left) {
  678. y += 1;
  679. }
  680. else if (type === _module_exports_$9.Piece.S && rotation === _module_exports_$9.Rotation.Spawn) {
  681. y += 1;
  682. }
  683. else if (type === _module_exports_$9.Piece.S && rotation === _module_exports_$9.Rotation.Right) {
  684. x += 1;
  685. }
  686. else if (type === _module_exports_$9.Piece.Z && rotation === _module_exports_$9.Rotation.Spawn) {
  687. y += 1;
  688. }
  689. else if (type === _module_exports_$9.Piece.Z && rotation === _module_exports_$9.Rotation.Left) {
  690. x -= 1;
  691. }
  692. return (fieldTop - y - 1) * width + x;
  693. }
  694. function encodeRotation(_a) {
  695. var type = _a.type, rotation = _a.rotation;
  696. if (!(0, _module_exports_$9.isMinoPiece)(type)) {
  697. return 0;
  698. }
  699. switch (rotation) {
  700. case _module_exports_$9.Rotation.Reverse:
  701. return 0;
  702. case _module_exports_$9.Rotation.Right:
  703. return 1;
  704. case _module_exports_$9.Rotation.Spawn:
  705. return 2;
  706. case _module_exports_$9.Rotation.Left:
  707. return 3;
  708. }
  709. throw new Error('No reachable');
  710. }
  711. return {
  712. encode: function (action) {
  713. var lock = action.lock, comment = action.comment, colorize = action.colorize, mirror = action.mirror, rise = action.rise, piece = action.piece;
  714. var value = encodeBool(!lock);
  715. value *= 2;
  716. value += encodeBool(comment);
  717. value *= 2;
  718. value += (encodeBool(colorize));
  719. value *= 2;
  720. value += encodeBool(mirror);
  721. value *= 2;
  722. value += encodeBool(rise);
  723. value *= numFieldBlocks;
  724. value += encodePosition(piece);
  725. value *= 4;
  726. value += encodeRotation(piece);
  727. value *= 8;
  728. value += piece.type;
  729. return value;
  730. },
  731. };
  732. };
  733. _module_exports_$6.createActionEncoder = createActionEncoder;
  734.  
  735. const _module_exports_$5 = {};
  736. Object.defineProperty(_module_exports_$5, "__esModule", { value: true });
  737. _module_exports_$5.createCommentParser = void 0;
  738. var COMMENT_TABLE = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
  739. var MAX_COMMENT_CHAR_VALUE = COMMENT_TABLE.length + 1;
  740. var createCommentParser = function () {
  741. return {
  742. decode: function (v) {
  743. var str = '';
  744. var value = v;
  745. for (var count = 0; count < 4; count += 1) {
  746. var index = value % MAX_COMMENT_CHAR_VALUE;
  747. str += COMMENT_TABLE[index];
  748. value = Math.floor(value / MAX_COMMENT_CHAR_VALUE);
  749. }
  750. return str;
  751. },
  752. encode: function (ch, count) {
  753. return COMMENT_TABLE.indexOf(ch) * Math.pow(MAX_COMMENT_CHAR_VALUE, count);
  754. },
  755. };
  756. };
  757. _module_exports_$5.createCommentParser = createCommentParser;
  758.  
  759. const _module_exports_$4 = {};
  760. Object.defineProperty(_module_exports_$4, "__esModule", { value: true });
  761. _module_exports_$4.Quiz = void 0;
  762. var Operation;
  763. (function (Operation) {
  764. Operation["Direct"] = "direct";
  765. Operation["Swap"] = "swap";
  766. Operation["Stock"] = "stock";
  767. })(Operation || (Operation = {}));
  768. var Quiz = /** @class */ (function () {
  769. function Quiz(quiz) {
  770. this.quiz = Quiz.verify(quiz);
  771. }
  772. Object.defineProperty(Quiz.prototype, "next", {
  773. get: function () {
  774. var index = this.quiz.indexOf(')') + 1;
  775. var name = this.quiz[index];
  776. if (name === undefined || name === ';') {
  777. return '';
  778. }
  779. return name;
  780. },
  781. enumerable: false,
  782. configurable: true
  783. });
  784. Quiz.isQuizComment = function (comment) {
  785. return comment.startsWith('#Q=');
  786. };
  787. Quiz.create = function (first, second) {
  788. var create = function (hold, other) {
  789. var parse = function (s) { return s ? s : ''; };
  790. return new Quiz("#Q=[".concat(parse(hold), "](").concat(parse(other[0]), ")").concat(parse(other.substring(1))));
  791. };
  792. return second !== undefined ? create(first, second) : create(undefined, first);
  793. };
  794. Quiz.trim = function (quiz) {
  795. return quiz.trim().replace(/\s+/g, '');
  796. };
  797. Object.defineProperty(Quiz.prototype, "least", {
  798. get: function () {
  799. var index = this.quiz.indexOf(')');
  800. return this.quiz.substr(index + 1);
  801. },
  802. enumerable: false,
  803. configurable: true
  804. });
  805. Object.defineProperty(Quiz.prototype, "current", {
  806. get: function () {
  807. var index = this.quiz.indexOf('(') + 1;
  808. var name = this.quiz[index];
  809. if (name === ')') {
  810. return '';
  811. }
  812. return name;
  813. },
  814. enumerable: false,
  815. configurable: true
  816. });
  817. Object.defineProperty(Quiz.prototype, "hold", {
  818. get: function () {
  819. var index = this.quiz.indexOf('[') + 1;
  820. var name = this.quiz[index];
  821. if (name === ']') {
  822. return '';
  823. }
  824. return name;
  825. },
  826. enumerable: false,
  827. configurable: true
  828. });
  829. Object.defineProperty(Quiz.prototype, "leastAfterNext2", {
  830. get: function () {
  831. var index = this.quiz.indexOf(')');
  832. if (this.quiz[index + 1] === ';') {
  833. return this.quiz.substr(index + 1);
  834. }
  835. return this.quiz.substr(index + 2);
  836. },
  837. enumerable: false,
  838. configurable: true
  839. });
  840. Quiz.prototype.getOperation = function (used) {
  841. var usedName = (0, _module_exports_$9.parsePieceName)(used);
  842. var current = this.current;
  843. if (usedName === current) {
  844. return Operation.Direct;
  845. }
  846. var hold = this.hold;
  847. if (usedName === hold) {
  848. return Operation.Swap;
  849. }
  850. // 次のミノを利用できる
  851. if (hold === '') {
  852. if (usedName === this.next) {
  853. return Operation.Stock;
  854. }
  855. }
  856. else {
  857. if (current === '' && usedName === this.next) {
  858. return Operation.Direct;
  859. }
  860. }
  861. throw new Error("Unexpected hold piece in quiz: ".concat(this.quiz));
  862. };
  863. Object.defineProperty(Quiz.prototype, "leastInActiveBag", {
  864. get: function () {
  865. var separateIndex = this.quiz.indexOf(';');
  866. var quiz = 0 <= separateIndex ? this.quiz.substring(0, separateIndex) : this.quiz;
  867. var index = quiz.indexOf(')');
  868. if (quiz[index + 1] === ';') {
  869. return quiz.substr(index + 1);
  870. }
  871. return quiz.substr(index + 2);
  872. },
  873. enumerable: false,
  874. configurable: true
  875. });
  876. Quiz.verify = function (quiz) {
  877. var replaced = this.trim(quiz);
  878. if (replaced.length === 0 || quiz === '#Q=[]()' || !quiz.startsWith('#Q=')) {
  879. return quiz;
  880. }
  881. if (!replaced.match(/^#Q=\[[TIOSZJL]?]\([TIOSZJL]?\)[TIOSZJL]*;?.*$/i)) {
  882. throw new Error("Current piece doesn't exist, however next pieces exist: ".concat(quiz));
  883. }
  884. return replaced;
  885. };
  886. Quiz.prototype.direct = function () {
  887. if (this.current === '') {
  888. var least = this.leastAfterNext2;
  889. return new Quiz("#Q=[".concat(this.hold, "](").concat(least[0], ")").concat(least.substr(1)));
  890. }
  891. return new Quiz("#Q=[".concat(this.hold, "](").concat(this.next, ")").concat(this.leastAfterNext2));
  892. };
  893. Quiz.prototype.swap = function () {
  894. if (this.hold === '') {
  895. throw new Error("Cannot find hold piece: ".concat(this.quiz));
  896. }
  897. var next = this.next;
  898. return new Quiz("#Q=[".concat(this.current, "](").concat(next, ")").concat(this.leastAfterNext2));
  899. };
  900. Quiz.prototype.stock = function () {
  901. if (this.hold !== '' || this.next === '') {
  902. throw new Error("Cannot stock: ".concat(this.quiz));
  903. }
  904. var least = this.leastAfterNext2;
  905. var head = least[0] !== undefined ? least[0] : '';
  906. if (1 < least.length) {
  907. return new Quiz("#Q=[".concat(this.current, "](").concat(head, ")").concat(least.substr(1)));
  908. }
  909. return new Quiz("#Q=[".concat(this.current, "](").concat(head, ")"));
  910. };
  911. Quiz.prototype.operate = function (operation) {
  912. switch (operation) {
  913. case Operation.Direct:
  914. return this.direct();
  915. case Operation.Swap:
  916. return this.swap();
  917. case Operation.Stock:
  918. return this.stock();
  919. }
  920. throw new Error('Unexpected operation');
  921. };
  922. Quiz.prototype.format = function () {
  923. var quiz = this.nextIfEnd();
  924. if (quiz.quiz === '#Q=[]()') {
  925. return new Quiz('');
  926. }
  927. var current = quiz.current;
  928. var hold = quiz.hold;
  929. if (current === '' && hold !== '') {
  930. return new Quiz("#Q=[](".concat(hold, ")").concat(quiz.least));
  931. }
  932. if (current === '') {
  933. var least = quiz.least;
  934. var head = least[0];
  935. if (head === undefined) {
  936. return new Quiz('');
  937. }
  938. if (head === ';') {
  939. return new Quiz(least.substr(1));
  940. }
  941. return new Quiz("#Q=[](".concat(head, ")").concat(least.substr(1)));
  942. }
  943. return quiz;
  944. };
  945. Quiz.prototype.getHoldPiece = function () {
  946. if (!this.canOperate()) {
  947. return _module_exports_$9.Piece.Empty;
  948. }
  949. var name = this.hold;
  950. if (name === undefined || name === '' || name === ';') {
  951. return _module_exports_$9.Piece.Empty;
  952. }
  953. return (0, _module_exports_$9.parsePiece)(name);
  954. };
  955. Quiz.prototype.getNextPieces = function (max) {
  956. if (!this.canOperate()) {
  957. return max !== undefined ? Array.from({ length: max }).map(function () { return _module_exports_$9.Piece.Empty; }) : [];
  958. }
  959. var names = (this.current + this.next + this.leastInActiveBag).substr(0, max);
  960. if (max !== undefined && names.length < max) {
  961. names += ' '.repeat(max - names.length);
  962. }
  963. return names.split('').map(function (name) {
  964. if (name === undefined || name === ' ' || name === ';') {
  965. return _module_exports_$9.Piece.Empty;
  966. }
  967. return (0, _module_exports_$9.parsePiece)(name);
  968. });
  969. };
  970. Quiz.prototype.toString = function () {
  971. return this.quiz;
  972. };
  973. Quiz.prototype.canOperate = function () {
  974. var quiz = this.quiz;
  975. if (quiz.startsWith('#Q=[]();')) {
  976. quiz = this.quiz.substr(8);
  977. }
  978. return quiz.startsWith('#Q=') && quiz !== '#Q=[]()';
  979. };
  980. Quiz.prototype.nextIfEnd = function () {
  981. if (this.quiz.startsWith('#Q=[]();')) {
  982. return new Quiz(this.quiz.substr(8));
  983. }
  984. return this;
  985. };
  986. return Quiz;
  987. }());
  988. _module_exports_$4.Quiz = Quiz;
  989.  
  990. const _module_exports_$3 = {};
  991. var __assign$1 = (undefined && undefined.__assign) || function () {
  992. __assign$1 = Object.assign || function(t) {
  993. for (var s, i = 1, n = arguments.length; i < n; i++) {
  994. s = arguments[i];
  995. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  996. t[p] = s[p];
  997. }
  998. return t;
  999. };
  1000. return __assign$1.apply(this, arguments);
  1001. };
  1002. Object.defineProperty(_module_exports_$3, "__esModule", { value: true });
  1003. _module_exports_$3.Mino = _module_exports_$3.Field = void 0;
  1004. function toMino(operationOrMino) {
  1005. return operationOrMino instanceof Mino ? operationOrMino.copy() : Mino.from(operationOrMino);
  1006. }
  1007. var Field = /** @class */ (function () {
  1008. function Field(field) {
  1009. this.field = field;
  1010. }
  1011. Field.create = function (field, garbage) {
  1012. return new Field(new _module_exports_$8.InnerField({
  1013. field: field !== undefined ? _module_exports_$8.PlayField.load(field) : undefined,
  1014. garbage: garbage !== undefined ? _module_exports_$8.PlayField.loadMinify(garbage) : undefined,
  1015. }));
  1016. };
  1017. Field.prototype.canFill = function (operation) {
  1018. if (operation === undefined) {
  1019. return true;
  1020. }
  1021. var mino = toMino(operation);
  1022. return this.field.canFillAll(mino.positions());
  1023. };
  1024. Field.prototype.canLock = function (operation) {
  1025. if (operation === undefined) {
  1026. return true;
  1027. }
  1028. if (!this.canFill(operation)) {
  1029. return false;
  1030. }
  1031. // Check on the ground
  1032. return !this.canFill(__assign$1(__assign$1({}, operation), { y: operation.y - 1 }));
  1033. };
  1034. Field.prototype.fill = function (operation, force) {
  1035. if (force === void 0) { force = false; }
  1036. if (operation === undefined) {
  1037. return undefined;
  1038. }
  1039. var mino = toMino(operation);
  1040. if (!force && !this.canFill(mino)) {
  1041. throw Error('Cannot fill piece on field');
  1042. }
  1043. this.field.fillAll(mino.positions(), (0, _module_exports_$9.parsePiece)(mino.type));
  1044. return mino;
  1045. };
  1046. Field.prototype.put = function (operation) {
  1047. if (operation === undefined) {
  1048. return undefined;
  1049. }
  1050. var mino = toMino(operation);
  1051. for (; 0 <= mino.y; mino.y -= 1) {
  1052. if (!this.canLock(mino)) {
  1053. continue;
  1054. }
  1055. this.fill(mino);
  1056. return mino;
  1057. }
  1058. throw Error('Cannot put piece on field');
  1059. };
  1060. Field.prototype.clearLine = function () {
  1061. this.field.clearLine();
  1062. };
  1063. Field.prototype.at = function (x, y) {
  1064. return (0, _module_exports_$9.parsePieceName)(this.field.getNumberAt(x, y));
  1065. };
  1066. Field.prototype.set = function (x, y, type) {
  1067. this.field.setNumberAt(x, y, (0, _module_exports_$9.parsePiece)(type));
  1068. };
  1069. Field.prototype.copy = function () {
  1070. return new Field(this.field.copy());
  1071. };
  1072. Field.prototype.str = function (option) {
  1073. if (option === void 0) { option = {}; }
  1074. var skip = option.reduced !== undefined ? option.reduced : true;
  1075. var separator = option.separator !== undefined ? option.separator : '\n';
  1076. var minY = option.garbage === undefined || option.garbage ? -1 : 0;
  1077. var output = '';
  1078. for (var y = 22; minY <= y; y -= 1) {
  1079. var line = '';
  1080. for (var x = 0; x < 10; x += 1) {
  1081. line += this.at(x, y);
  1082. }
  1083. if (skip && line === '__________') {
  1084. continue;
  1085. }
  1086. skip = false;
  1087. output += line;
  1088. if (y !== minY) {
  1089. output += separator;
  1090. }
  1091. }
  1092. return output;
  1093. };
  1094. return Field;
  1095. }());
  1096. _module_exports_$3.Field = Field;
  1097. var Mino = /** @class */ (function () {
  1098. function Mino(type, rotation, x, y) {
  1099. this.type = type;
  1100. this.rotation = rotation;
  1101. this.x = x;
  1102. this.y = y;
  1103. }
  1104. Mino.from = function (operation) {
  1105. return new Mino(operation.type, operation.rotation, operation.x, operation.y);
  1106. };
  1107. Mino.prototype.positions = function () {
  1108. return (0, _module_exports_$8.getBlockXYs)((0, _module_exports_$9.parsePiece)(this.type), (0, _module_exports_$9.parseRotation)(this.rotation), this.x, this.y).sort(function (a, b) {
  1109. if (a.y === b.y) {
  1110. return a.x - b.x;
  1111. }
  1112. return a.y - b.y;
  1113. });
  1114. };
  1115. Mino.prototype.operation = function () {
  1116. return {
  1117. type: this.type,
  1118. rotation: this.rotation,
  1119. x: this.x,
  1120. y: this.y,
  1121. };
  1122. };
  1123. Mino.prototype.isValid = function () {
  1124. try {
  1125. (0, _module_exports_$9.parsePiece)(this.type);
  1126. (0, _module_exports_$9.parseRotation)(this.rotation);
  1127. }
  1128. catch (e) {
  1129. return false;
  1130. }
  1131. return this.positions().every(function (_a) {
  1132. var x = _a.x, y = _a.y;
  1133. return 0 <= x && x < 10 && 0 <= y && y < 23;
  1134. });
  1135. };
  1136. Mino.prototype.copy = function () {
  1137. return new Mino(this.type, this.rotation, this.x, this.y);
  1138. };
  1139. return Mino;
  1140. }());
  1141. _module_exports_$3.Mino = Mino;
  1142.  
  1143. const _module_exports_$2 = {};
  1144. Object.defineProperty(_module_exports_$2, "__esModule", { value: true });
  1145. _module_exports_$2.decode = _module_exports_$2.extract = _module_exports_$2.Page = void 0;
  1146. var Page = /** @class */ (function () {
  1147. function Page(index, field, operation, comment, flags, refs) {
  1148. this.index = index;
  1149. this.operation = operation;
  1150. this.comment = comment;
  1151. this.flags = flags;
  1152. this.refs = refs;
  1153. this._field = field.copy();
  1154. }
  1155. Object.defineProperty(Page.prototype, "field", {
  1156. get: function () {
  1157. return new _module_exports_$3.Field(this._field.copy());
  1158. },
  1159. set: function (field) {
  1160. this._field = (0, _module_exports_$8.createInnerField)(field);
  1161. },
  1162. enumerable: false,
  1163. configurable: true
  1164. });
  1165. Page.prototype.mino = function () {
  1166. return _module_exports_$3.Mino.from(this.operation);
  1167. };
  1168. return Page;
  1169. }());
  1170. _module_exports_$2.Page = Page;
  1171. var FieldConstants$1 = {
  1172. GarbageLine: 1,
  1173. Width: 10,
  1174. };
  1175. function extract(str) {
  1176. var format = function (version, data) {
  1177. var trim = data.trim().replace(/[?\s]+/g, '');
  1178. return { version: version, data: trim };
  1179. };
  1180. var data = str;
  1181. // url parameters
  1182. var paramIndex = data.indexOf('&');
  1183. if (0 <= paramIndex) {
  1184. data = data.substring(0, paramIndex);
  1185. }
  1186. // v115@~
  1187. {
  1188. var match = str.match(/[vmd]115@/);
  1189. if (match !== undefined && match !== null && match.index !== undefined) {
  1190. var sub = data.substr(match.index + 5);
  1191. return format('115', sub);
  1192. }
  1193. }
  1194. // v110@~
  1195. {
  1196. var match = str.match(/[vmd]110@/);
  1197. if (match !== undefined && match !== null && match.index !== undefined) {
  1198. var sub = data.substr(match.index + 5);
  1199. return format('110', sub);
  1200. }
  1201. }
  1202. throw new Error('Unsupported fumen version');
  1203. }
  1204. _module_exports_$2.extract = extract;
  1205. function decode(fumen) {
  1206. var _a = extract(fumen), version = _a.version, data = _a.data;
  1207. switch (version) {
  1208. case '115':
  1209. return innerDecode(data, 23);
  1210. case '110':
  1211. return innerDecode(data, 21);
  1212. }
  1213. throw new Error('Unsupported fumen version');
  1214. }
  1215. _module_exports_$2.decode = decode;
  1216. function innerDecode(data, fieldTop) {
  1217. var fieldMaxHeight = fieldTop + FieldConstants$1.GarbageLine;
  1218. var numFieldBlocks = fieldMaxHeight * FieldConstants$1.Width;
  1219. var buffer = new _module_exports_$7.Buffer(data);
  1220. var updateField = function (prev) {
  1221. var result = {
  1222. changed: true,
  1223. field: prev,
  1224. };
  1225. var index = 0;
  1226. while (index < numFieldBlocks) {
  1227. var diffBlock = buffer.poll(2);
  1228. var diff = Math.floor(diffBlock / numFieldBlocks);
  1229. var numOfBlocks = diffBlock % numFieldBlocks;
  1230. if (diff === 8 && numOfBlocks === numFieldBlocks - 1) {
  1231. result.changed = false;
  1232. }
  1233. for (var block = 0; block < numOfBlocks + 1; block += 1) {
  1234. var x = index % FieldConstants$1.Width;
  1235. var y = fieldTop - Math.floor(index / FieldConstants$1.Width) - 1;
  1236. result.field.addNumber(x, y, diff - 8);
  1237. index += 1;
  1238. }
  1239. }
  1240. return result;
  1241. };
  1242. var pageIndex = 0;
  1243. var prevField = (0, _module_exports_$8.createNewInnerField)();
  1244. var store = {
  1245. repeatCount: -1,
  1246. refIndex: {
  1247. comment: 0,
  1248. field: 0,
  1249. },
  1250. quiz: undefined,
  1251. lastCommentText: '',
  1252. };
  1253. var pages = [];
  1254. var actionDecoder = (0, _module_exports_$6.createActionDecoder)(FieldConstants$1.Width, fieldTop, FieldConstants$1.GarbageLine);
  1255. var commentDecoder = (0, _module_exports_$5.createCommentParser)();
  1256. while (!buffer.isEmpty()) {
  1257. // Parse field
  1258. var currentFieldObj = void 0;
  1259. if (0 < store.repeatCount) {
  1260. currentFieldObj = {
  1261. field: prevField,
  1262. changed: false,
  1263. };
  1264. store.repeatCount -= 1;
  1265. }
  1266. else {
  1267. currentFieldObj = updateField(prevField.copy());
  1268. if (!currentFieldObj.changed) {
  1269. store.repeatCount = buffer.poll(1);
  1270. }
  1271. }
  1272. // Parse action
  1273. var actionValue = buffer.poll(3);
  1274. var action = actionDecoder.decode(actionValue);
  1275. // Parse comment
  1276. var comment = void 0;
  1277. if (action.comment) {
  1278. // コメントに更新があるとき
  1279. var commentValues = [];
  1280. var commentLength = buffer.poll(2);
  1281. for (var commentCounter = 0; commentCounter < Math.floor((commentLength + 3) / 4); commentCounter += 1) {
  1282. var commentValue = buffer.poll(5);
  1283. commentValues.push(commentValue);
  1284. }
  1285. var flatten = '';
  1286. for (var _i = 0, commentValues_1 = commentValues; _i < commentValues_1.length; _i++) {
  1287. var value = commentValues_1[_i];
  1288. flatten += commentDecoder.decode(value);
  1289. }
  1290. var commentText = unescape(flatten.slice(0, commentLength));
  1291. store.lastCommentText = commentText;
  1292. comment = { text: commentText };
  1293. store.refIndex.comment = pageIndex;
  1294. var text = comment.text;
  1295. if (_module_exports_$4.Quiz.isQuizComment(text)) {
  1296. try {
  1297. store.quiz = new _module_exports_$4.Quiz(text);
  1298. }
  1299. catch (e) {
  1300. store.quiz = undefined;
  1301. }
  1302. }
  1303. else {
  1304. store.quiz = undefined;
  1305. }
  1306. }
  1307. else if (pageIndex === 0) {
  1308. // コメントに更新がないが、先頭のページのとき
  1309. comment = { text: '' };
  1310. }
  1311. else {
  1312. // コメントに更新がないとき
  1313. comment = {
  1314. text: store.quiz !== undefined ? store.quiz.format().toString() : undefined,
  1315. ref: store.refIndex.comment,
  1316. };
  1317. }
  1318. // Quiz用の操作を取得し、次ページ開始時点のQuizに1手進める
  1319. var quiz = false;
  1320. if (store.quiz !== undefined) {
  1321. quiz = true;
  1322. if (store.quiz.canOperate() && action.lock) {
  1323. if ((0, _module_exports_$9.isMinoPiece)(action.piece.type)) {
  1324. try {
  1325. var nextQuiz = store.quiz.nextIfEnd();
  1326. var operation = nextQuiz.getOperation(action.piece.type);
  1327. store.quiz = nextQuiz.operate(operation);
  1328. }
  1329. catch (e) {
  1330. // console.error(e.message);
  1331. // Not operate
  1332. store.quiz = store.quiz.format();
  1333. }
  1334. }
  1335. else {
  1336. store.quiz = store.quiz.format();
  1337. }
  1338. }
  1339. }
  1340. // データ処理用に加工する
  1341. var currentPiece = void 0;
  1342. if (action.piece.type !== _module_exports_$9.Piece.Empty) {
  1343. currentPiece = action.piece;
  1344. }
  1345. // pageの作成
  1346. var field = void 0;
  1347. if (currentFieldObj.changed || pageIndex === 0) {
  1348. // フィールドに変化があったとき
  1349. // フィールドに変化がなかったが、先頭のページだったとき
  1350. field = {};
  1351. store.refIndex.field = pageIndex;
  1352. }
  1353. else {
  1354. // フィールドに変化がないとき
  1355. field = { ref: store.refIndex.field };
  1356. }
  1357. pages.push(new Page(pageIndex, currentFieldObj.field, currentPiece !== undefined ? _module_exports_$3.Mino.from({
  1358. type: (0, _module_exports_$9.parsePieceName)(currentPiece.type),
  1359. rotation: (0, _module_exports_$9.parseRotationName)(currentPiece.rotation),
  1360. x: currentPiece.x,
  1361. y: currentPiece.y,
  1362. }) : undefined, comment.text !== undefined ? comment.text : store.lastCommentText, {
  1363. quiz: quiz,
  1364. lock: action.lock,
  1365. mirror: action.mirror,
  1366. colorize: action.colorize,
  1367. rise: action.rise,
  1368. }, {
  1369. field: field.ref,
  1370. comment: comment.ref,
  1371. }));
  1372. // callback(
  1373. // currentFieldObj.field.copy()
  1374. // , currentPiece
  1375. // , store.quiz !== undefined ? store.quiz.format().toString() : store.lastCommentText,
  1376. // );
  1377. pageIndex += 1;
  1378. if (action.lock) {
  1379. if ((0, _module_exports_$9.isMinoPiece)(action.piece.type)) {
  1380. currentFieldObj.field.fill(action.piece);
  1381. }
  1382. currentFieldObj.field.clearLine();
  1383. if (action.rise) {
  1384. currentFieldObj.field.riseGarbage();
  1385. }
  1386. if (action.mirror) {
  1387. currentFieldObj.field.mirror();
  1388. }
  1389. }
  1390. prevField = currentFieldObj.field;
  1391. }
  1392. return pages;
  1393. }
  1394.  
  1395. const _module_exports_$1 = {};
  1396. var __assign = (undefined && undefined.__assign) || function () {
  1397. __assign = Object.assign || function(t) {
  1398. for (var s, i = 1, n = arguments.length; i < n; i++) {
  1399. s = arguments[i];
  1400. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  1401. t[p] = s[p];
  1402. }
  1403. return t;
  1404. };
  1405. return __assign.apply(this, arguments);
  1406. };
  1407. Object.defineProperty(_module_exports_$1, "__esModule", { value: true });
  1408. _module_exports_$1.encode = void 0;
  1409. var FieldConstants = {
  1410. GarbageLine: 1,
  1411. Width: 10,
  1412. };
  1413. function encode(pages) {
  1414. var updateField = function (prev, current) {
  1415. var _a = encodeField(prev, current), changed = _a.changed, values = _a.values;
  1416. if (changed) {
  1417. // フィールドを記録して、リピートを終了する
  1418. buffer.merge(values);
  1419. lastRepeatIndex = -1;
  1420. }
  1421. else if (lastRepeatIndex < 0 || buffer.get(lastRepeatIndex) === _module_exports_$7.Buffer.tableLength - 1) {
  1422. // フィールドを記録して、リピートを開始する
  1423. buffer.merge(values);
  1424. buffer.push(0);
  1425. lastRepeatIndex = buffer.length - 1;
  1426. }
  1427. else if (buffer.get(lastRepeatIndex) < (_module_exports_$7.Buffer.tableLength - 1)) {
  1428. // フィールドは記録せず、リピートを進める
  1429. var currentRepeatValue = buffer.get(lastRepeatIndex);
  1430. buffer.set(lastRepeatIndex, currentRepeatValue + 1);
  1431. }
  1432. };
  1433. var lastRepeatIndex = -1;
  1434. var buffer = new _module_exports_$7.Buffer();
  1435. var prevField = (0, _module_exports_$8.createNewInnerField)();
  1436. var actionEncoder = (0, _module_exports_$6.createActionEncoder)(FieldConstants.Width, 23, FieldConstants.GarbageLine);
  1437. var commentParser = (0, _module_exports_$5.createCommentParser)();
  1438. var prevComment = '';
  1439. var prevQuiz = undefined;
  1440. var innerEncode = function (index) {
  1441. var currentPage = pages[index];
  1442. currentPage.flags = currentPage.flags ? currentPage.flags : {};
  1443. var field = currentPage.field;
  1444. var currentField = field !== undefined ? (0, _module_exports_$8.createInnerField)(field) : prevField.copy();
  1445. // フィールドの更新
  1446. updateField(prevField, currentField);
  1447. // アクションの更新
  1448. var currentComment = currentPage.comment !== undefined
  1449. ? ((index !== 0 || currentPage.comment !== '') ? currentPage.comment : undefined)
  1450. : undefined;
  1451. var piece = currentPage.operation !== undefined ? {
  1452. type: (0, _module_exports_$9.parsePiece)(currentPage.operation.type),
  1453. rotation: (0, _module_exports_$9.parseRotation)(currentPage.operation.rotation),
  1454. x: currentPage.operation.x,
  1455. y: currentPage.operation.y,
  1456. } : {
  1457. type: _module_exports_$9.Piece.Empty,
  1458. rotation: _module_exports_$9.Rotation.Reverse,
  1459. x: 0,
  1460. y: 22,
  1461. };
  1462. var nextComment;
  1463. if (currentComment !== undefined) {
  1464. if (currentComment.startsWith('#Q=')) {
  1465. // Quiz on
  1466. if (prevQuiz !== undefined && prevQuiz.format().toString() === currentComment) {
  1467. nextComment = undefined;
  1468. }
  1469. else {
  1470. nextComment = currentComment;
  1471. prevComment = nextComment;
  1472. prevQuiz = new _module_exports_$4.Quiz(currentComment);
  1473. }
  1474. }
  1475. else {
  1476. // Quiz off
  1477. if (prevQuiz !== undefined && prevQuiz.format().toString() === currentComment) {
  1478. nextComment = undefined;
  1479. prevComment = currentComment;
  1480. prevQuiz = undefined;
  1481. }
  1482. else {
  1483. nextComment = prevComment !== currentComment ? currentComment : undefined;
  1484. prevComment = prevComment !== currentComment ? nextComment : prevComment;
  1485. prevQuiz = undefined;
  1486. }
  1487. }
  1488. }
  1489. else {
  1490. nextComment = undefined;
  1491. prevQuiz = undefined;
  1492. }
  1493. if (prevQuiz !== undefined && prevQuiz.canOperate() && currentPage.flags.lock) {
  1494. if ((0, _module_exports_$9.isMinoPiece)(piece.type)) {
  1495. try {
  1496. var nextQuiz = prevQuiz.nextIfEnd();
  1497. var operation = nextQuiz.getOperation(piece.type);
  1498. prevQuiz = nextQuiz.operate(operation);
  1499. }
  1500. catch (e) {
  1501. // console.error(e.message);
  1502. // Not operate
  1503. prevQuiz = prevQuiz.format();
  1504. }
  1505. }
  1506. else {
  1507. prevQuiz = prevQuiz.format();
  1508. }
  1509. }
  1510. var currentFlags = __assign({ lock: true, colorize: index === 0 }, currentPage.flags);
  1511. var action = {
  1512. piece: piece,
  1513. rise: !!currentFlags.rise,
  1514. mirror: !!currentFlags.mirror,
  1515. colorize: !!currentFlags.colorize,
  1516. lock: !!currentFlags.lock,
  1517. comment: nextComment !== undefined,
  1518. };
  1519. var actionNumber = actionEncoder.encode(action);
  1520. buffer.push(actionNumber, 3);
  1521. // コメントの更新
  1522. if (nextComment !== undefined) {
  1523. var comment = escape(currentPage.comment);
  1524. var commentLength = Math.min(comment.length, 4095);
  1525. buffer.push(commentLength, 2);
  1526. // コメントを符号化
  1527. for (var index_1 = 0; index_1 < commentLength; index_1 += 4) {
  1528. var value = 0;
  1529. for (var count = 0; count < 4; count += 1) {
  1530. var newIndex = index_1 + count;
  1531. if (commentLength <= newIndex) {
  1532. break;
  1533. }
  1534. var ch = comment.charAt(newIndex);
  1535. value += commentParser.encode(ch, count);
  1536. }
  1537. buffer.push(value, 5);
  1538. }
  1539. }
  1540. else if (currentPage.comment === undefined) {
  1541. prevComment = undefined;
  1542. }
  1543. // 地形の更新
  1544. if (action.lock) {
  1545. if ((0, _module_exports_$9.isMinoPiece)(action.piece.type)) {
  1546. currentField.fill(action.piece);
  1547. }
  1548. currentField.clearLine();
  1549. if (action.rise) {
  1550. currentField.riseGarbage();
  1551. }
  1552. if (action.mirror) {
  1553. currentField.mirror();
  1554. }
  1555. }
  1556. prevField = currentField;
  1557. };
  1558. for (var index = 0; index < pages.length; index += 1) {
  1559. innerEncode(index);
  1560. }
  1561. // テト譜が短いときはそのまま出力する
  1562. // 47文字ごとに?が挿入されるが、実際は先頭にv115@が入るため、最初の?は42文字後になる
  1563. var data = buffer.toString();
  1564. if (data.length < 41) {
  1565. return data;
  1566. }
  1567. // ?を挿入する
  1568. var head = [data.substr(0, 42)];
  1569. var tails = data.substring(42);
  1570. var split = tails.match(/[\S]{1,47}/g) || [];
  1571. return head.concat(split).join('?');
  1572. }
  1573. _module_exports_$1.encode = encode;
  1574. // フィールドをエンコードする
  1575. // 前のフィールドがないときは空のフィールドを指定する
  1576. // 入力フィールドの高さは23, 幅は10
  1577. function encodeField(prev, current) {
  1578. var FIELD_TOP = 23;
  1579. var FIELD_MAX_HEIGHT = FIELD_TOP + 1;
  1580. var FIELD_BLOCKS = FIELD_MAX_HEIGHT * FieldConstants.Width;
  1581. var buffer = new _module_exports_$7.Buffer();
  1582. // 前のフィールドとの差を計算: 0〜16
  1583. var getDiff = function (xIndex, yIndex) {
  1584. var y = FIELD_TOP - yIndex - 1;
  1585. return current.getNumberAt(xIndex, y) - prev.getNumberAt(xIndex, y) + 8;
  1586. };
  1587. // データの記録
  1588. var recordBlockCounts = function (diff, counter) {
  1589. var value = diff * FIELD_BLOCKS + counter;
  1590. buffer.push(value, 2);
  1591. };
  1592. // フィールド値から連続したブロック数に変換
  1593. var changed = true;
  1594. var prev_diff = getDiff(0, 0);
  1595. var counter = -1;
  1596. for (var yIndex = 0; yIndex < FIELD_MAX_HEIGHT; yIndex += 1) {
  1597. for (var xIndex = 0; xIndex < FieldConstants.Width; xIndex += 1) {
  1598. var diff = getDiff(xIndex, yIndex);
  1599. if (diff !== prev_diff) {
  1600. recordBlockCounts(prev_diff, counter);
  1601. counter = 0;
  1602. prev_diff = diff;
  1603. }
  1604. else {
  1605. counter += 1;
  1606. }
  1607. }
  1608. }
  1609. // 最後の連続ブロックを処理
  1610. recordBlockCounts(prev_diff, counter);
  1611. if (prev_diff === 8 && counter === FIELD_BLOCKS - 1) {
  1612. changed = false;
  1613. }
  1614. return {
  1615. changed: changed,
  1616. values: buffer,
  1617. };
  1618. }
  1619.  
  1620. const _module_exports_ = {};
  1621. Object.defineProperty(_module_exports_, "__esModule", { value: true });
  1622. _module_exports_.encoder = _module_exports_.decoder = _module_exports_.Mino = _module_exports_.Field = void 0;
  1623. Object.defineProperty(_module_exports_, "Field", { enumerable: true, get: function () { return _module_exports_$3.Field; } });
  1624. Object.defineProperty(_module_exports_, "Mino", { enumerable: true, get: function () { return _module_exports_$3.Mino; } });
  1625. _module_exports_.decoder = {
  1626. decode: function (data) {
  1627. return (0, _module_exports_$2.decode)(data);
  1628. },
  1629. };
  1630. _module_exports_.encoder = {
  1631. encode: function (data) {
  1632. return "v115@".concat((0, _module_exports_$1.encode)(data));
  1633. },
  1634. };
  1635.  
  1636. /*!
  1637. * escape-html
  1638. * Copyright(c) 2012-2013 TJ Holowaychuk
  1639. * Copyright(c) 2015 Andreas Lubbe
  1640. * Copyright(c) 2015 Tiancheng "Timothy" Gu
  1641. * MIT Licensed
  1642. */
  1643.  
  1644.  
  1645. /**
  1646. * Module variables.
  1647. * @private
  1648. */
  1649.  
  1650. var matchHtmlRegExp = /["'&<>]/;
  1651.  
  1652. /**
  1653. * Escape special characters in the given string of html.
  1654. *
  1655. * @param {string} string The string to escape for inserting into HTML
  1656. * @return {string}
  1657. * @public
  1658. */
  1659.  
  1660. function escapeHtml(string) {
  1661. var str = '' + string;
  1662. var match = matchHtmlRegExp.exec(str);
  1663.  
  1664. if (!match) {
  1665. return str;
  1666. }
  1667.  
  1668. var escape;
  1669. var html = '';
  1670. var index = 0;
  1671. var lastIndex = 0;
  1672.  
  1673. for (index = match.index; index < str.length; index++) {
  1674. switch (str.charCodeAt(index)) {
  1675. case 34: // "
  1676. escape = '&quot;';
  1677. break;
  1678. case 38: // &
  1679. escape = '&amp;';
  1680. break;
  1681. case 39: // '
  1682. escape = '&#39;';
  1683. break;
  1684. case 60: // <
  1685. escape = '&lt;';
  1686. break;
  1687. case 62: // >
  1688. escape = '&gt;';
  1689. break;
  1690. default:
  1691. continue;
  1692. }
  1693.  
  1694. if (lastIndex !== index) {
  1695. html += str.substring(lastIndex, index);
  1696. }
  1697.  
  1698. lastIndex = index + 1;
  1699. html += escape;
  1700. }
  1701.  
  1702. return lastIndex !== index
  1703. ? html + str.substring(lastIndex, index)
  1704. : html;
  1705. }
  1706.  
  1707. const {decoder} = _module_exports_;
  1708.  
  1709. const tiles = {
  1710. _: "#000",
  1711. X: "#999",
  1712. Xl: "#ccc",
  1713. I: "hsl(180, 100%, 35%)",
  1714. Il: "#0ff",
  1715. L: "hsl(30, 95%, 47%)",
  1716. Ll: "hsl(36, 100%, 55%)",
  1717. O: "hsl(60, 70%, 40%)",
  1718. Ol: "#ff0",
  1719. Z: "hsl(0, 70%, 50%)",
  1720. Zl: "hsl(0, 100%, 63%)",
  1721. T: "hsl(295, 80%, 47%)",
  1722. Tl: "hsl(300, 90%, 57%)",
  1723. J: "hsl(240, 75%, 50%)",
  1724. Jl: "hsl(240, 100%, 60%)",
  1725. S: "hsl(120, 100%, 33%)",
  1726. Sl: "#0f0"
  1727. };
  1728.  
  1729. function createTile(name, size) {
  1730. const fill = tiles[name];
  1731. const stroke = name === "_" ? ' stroke="rgba(255,255,255,0.05)"' : "";
  1732. return `<rect id="t${name}" fill="${fill}" width="${size}" height="${size}"${stroke}/>`;
  1733. }
  1734.  
  1735. function pageToFrame(page) {
  1736. const field = page.field.copy();
  1737. const filledMino = page.operation && field.fill(page.operation);
  1738. const output = Array(200);
  1739. const touchedY = new Set;
  1740. // extract field data
  1741. for (let y = 0; y < 20; y++) {
  1742. for (let x = 0; x < 10; x++) {
  1743. output[y * 10 + x] = {
  1744. type: field.at(x, y),
  1745. light: false
  1746. };
  1747. }
  1748. }
  1749. // draw active mino
  1750. if (filledMino) {
  1751. for (const {x, y} of filledMino.positions()) {
  1752. touchedY.add(y);
  1753. output[y * 10 + x].light = true;
  1754. }
  1755. }
  1756. if (page.flags.lock) {
  1757. // lighten lines which should be cleared
  1758. for (const y of touchedY) {
  1759. if (isLineFilled(field, y)) {
  1760. for (let x = 0; x < 10; x++) {
  1761. output[y * 10 + x].light = true;
  1762. }
  1763. }
  1764. }
  1765. }
  1766. return output;
  1767. }
  1768.  
  1769. function isLineFilled(field, y) {
  1770. for (let x = 0; x < 10; x++) {
  1771. if (field.at(x, y) === "_") {
  1772. return false;
  1773. }
  1774. }
  1775. return true;
  1776. }
  1777.  
  1778. function createSVG({
  1779. data,
  1780. pages = decoder.decode(data),
  1781. index,
  1782. delay = 500,
  1783. size = 16,
  1784. commentSize = 16,
  1785. animateType = "css",
  1786. comment = "auto"
  1787. }) {
  1788. if (index != null) {
  1789. pages = pages.slice(index, index + 1);
  1790. }
  1791. const frames = pages.map(pageToFrame);
  1792. const drawComment =
  1793. comment === "always" ? true :
  1794. comment === "none" ? false :
  1795. pages.some(p => p.comment);
  1796. const commentHeight = Math.round(commentSize * 1.6);
  1797. const commentOffset = commentSize * 1.2;
  1798. const commentY = size * 20;
  1799. const width = size * 10;
  1800. const height = size * 20 + (drawComment ? commentHeight : 0);
  1801. // diff frames
  1802. for (let i = frames.length - 1; i >= 1; i--) {
  1803. for (let j = 0; j < frames[i].length; j++) {
  1804. if (isTileEqual(frames[i - 1][j], frames[i][j])) {
  1805. frames[i][j] = null;
  1806. }
  1807. }
  1808. }
  1809. // diff comment
  1810. for (let i = pages.length - 1; i >= 1; i--) {
  1811. if (pages[i].comment && pages[i].comment === pages[i - 1].comment) {
  1812. pages[i].comment = null;
  1813. }
  1814. }
  1815. const layers = [];
  1816. const usedTiles = new Set;
  1817. for (let i = 0; i < frames.length; i++) {
  1818. const layer = createLayer(frames[i], size);
  1819. for (const t of layer.used) {
  1820. usedTiles.add(t);
  1821. }
  1822. const comment = drawComment ?
  1823. createComment({
  1824. text: pages[i].comment,
  1825. width,
  1826. height: commentHeight,
  1827. y: commentY,
  1828. size: commentSize,
  1829. offset: commentOffset,
  1830. index: i
  1831. }) : "";
  1832. layers.push(`
  1833. <svg viewBox="0 0 ${width} ${height}" id="f${i}">
  1834. ${layer.tiles.join("")}
  1835. ${comment}
  1836. ${createAnimate(i, frames.length, delay, animateType)}
  1837. </svg>`);
  1838. }
  1839. return `
  1840. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}" style="font-family: sans-serif">
  1841. <defs>
  1842. ${createDefs(usedTiles, size)}
  1843. </defs>
  1844. ${layers.join("")}
  1845. ${createProgress(commentY, frames.length, width, delay, animateType)}
  1846. </svg>`.trim().replace(/>\s+</g, ">\n<");
  1847. }
  1848.  
  1849. function createProgress(y, total, width, delay, type) {
  1850. if (total === 1) {
  1851. return "";
  1852. }
  1853. const attr = `d="M 0,${y} H${width}" stroke="silver" stroke-width="${width / 50}" stroke-dasharray="${width}"`;
  1854. if (type === "css") {
  1855. return `
  1856. <path ${attr} id="p"/>
  1857. <style>
  1858. #p {animation: p ${delay * total}ms steps(${total}, start) infinite}
  1859. @keyframes p {
  1860. from {
  1861. stroke-dashoffset: ${width};
  1862. }
  1863. to {
  1864. stroke-dashoffset: 0;
  1865. }
  1866. }
  1867. </style>`;
  1868. }
  1869. return `
  1870. <path ${attr}>
  1871. <animate attributeName="stroke-dashoffset" values="${createValues()}" calcMode="discrete" dur="${total * delay}ms" repeatCount="indefinite"/>
  1872. </path>`;
  1873. function createValues() {
  1874. const values = [];
  1875. for (let i = 0; i < total; i++) {
  1876. values.push((total - i - 1) / total * width);
  1877. }
  1878. return values.join(";");
  1879. }
  1880. }
  1881.  
  1882. function createComment({text, width, height, y, size, offset, index}) {
  1883. if (!text && index) {
  1884. return "";
  1885. }
  1886. const bg = index === 0 ?
  1887. `<rect id="cbg" fill="#333" y="${y}" width="${width}" height="${height}"/>` :
  1888. '<use href="#cbg"/>';
  1889. const textEl = text ?
  1890. `<text x="${width / 2}" y="${y + offset}" text-anchor="middle" font-size="${size}" fill="white">${escapeHtml(text)}</text>` : "";
  1891. return bg + textEl;
  1892. }
  1893.  
  1894. function createAnimate(i, total, delay, type) {
  1895. if (i === 0) {
  1896. return "";
  1897. }
  1898. if (type === "css") {
  1899. return `<style>
  1900. #f${i} {
  1901. animation: a${i} ${total * delay}ms step-end infinite;
  1902. }
  1903. @keyframes a${i} {
  1904. 0% {visibility: hidden} ${i * 100 / total}% {visibility: visible}
  1905. }
  1906. </style>`;
  1907. }
  1908. return `<animate attributeName="display" values="none;inline" calcMode="discrete" keyTimes="0;${i/total}" dur="${delay * total}ms" repeatCount="indefinite" />`;
  1909. }
  1910.  
  1911. function createDefs(include, size) {
  1912. const els = [];
  1913. for (const name of include) {
  1914. els.push(createTile(name, size));
  1915. }
  1916. return els.join("");
  1917. }
  1918.  
  1919. function createLayer(buffer, size) {
  1920. const bgTiles = [];
  1921. const tiles = [];
  1922. const used = new Set;
  1923. for (let i = 0; i < buffer.length; i++) {
  1924. if (!buffer[i]) {
  1925. continue;
  1926. }
  1927. const name = `${buffer[i].type}${buffer[i].light ? "l" : ""}`;
  1928. used.add(name);
  1929. const y = Math.floor(i / 10);
  1930. const x = i % 10;
  1931. const data = `<use href="#t${name}" x="${x * size}" y="${(20 - y - 1) * size}"/>`;
  1932. if (name === "_") {
  1933. bgTiles.push(data);
  1934. } else {
  1935. tiles.push(data);
  1936. }
  1937. }
  1938. return {
  1939. used,
  1940. tiles: bgTiles.concat(tiles)
  1941. };
  1942. }
  1943.  
  1944. function isTileEqual(a, b) {
  1945. return a.type === b.type && a.light === b.light;
  1946. }
  1947.  
  1948. var fumen = {
  1949. name: "Fumen",
  1950. global: true,
  1951. getPatterns: function() {
  1952. return [
  1953. /(?:fumen\.zui\.jp|harddrop\.com\/fumen)[^?]*\?(v115@.*)/i
  1954. ];
  1955. },
  1956. getEmbedFunction: function() {
  1957. return function (data, url, text, node) {
  1958. var image = new Image;
  1959. image.title = text;
  1960. image.className = "embed-me-fumen";
  1961. image.src = `data:image/svg+xml,${encodeURIComponent(createSVG({data}))}`;
  1962. node = node.cloneNode(false);
  1963. node.appendChild(image);
  1964. return node;
  1965. };
  1966. }
  1967. };
  1968.  
  1969. var image = {
  1970. name: "Image",
  1971. global: true,
  1972. getPatterns: function() {
  1973. return [
  1974. /^[^?#]+\.(?:jpg|png|gif|jpeg)(?:$|[?#])/i
  1975. ];
  1976. },
  1977. getEmbedFunction: function() {
  1978. GM_addStyle(".embed-me-image { max-width: 90%; }");
  1979. return function(url, text, node) {
  1980. var image = new Image;
  1981. image.title = text;
  1982. image.className = "embed-me-image";
  1983. image.src = url;
  1984. node = node.cloneNode(false);
  1985. node.appendChild(image);
  1986. return node;
  1987. };
  1988. }
  1989. };
  1990.  
  1991. var imgur = {
  1992. name: "Imgur gifv",
  1993. domains: ["i.imgur.com", "imgur.com"],
  1994. getPatterns: function() {
  1995. return [
  1996. /imgur\.com\/(\w+)(\.gifv|$)/i
  1997. ];
  1998. },
  1999. getEmbedFunction: function() {
  2000. GM_addStyle('.imgur-embed-iframe-pub { box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.10); border: 1px solid #ddd; border-radius: 2px; margin: 10px 0; width: 540px; overflow: hidden; }');
  2001.  
  2002. window.addEventListener("message", function(e){
  2003. if (e.origin.indexOf("imgur.com") < 0) {
  2004. return;
  2005. }
  2006.  
  2007. var data = JSON.parse(e.data),
  2008. id = data.href.match(/imgur\.com\/(\w+)\//)[1],
  2009. css = '.imgur-embed-iframe-pub-' + id + '-' + data.context + '-540 { height: ' + data.height + 'px!important; width: 540px!important; }';
  2010.  
  2011. GM_addStyle(css);
  2012. });
  2013.  
  2014. return function(id) {
  2015. var iframe = document.createElement("iframe");
  2016. iframe.className = "imgur-embed-iframe-pub imgur-embed-iframe-pub-" + id + "-true-540";
  2017. iframe.scrolling = "no";
  2018. iframe.src = "//imgur.com/" + id + "/embed?w=540&ref=" + location.href + "#embed-me";
  2019. return iframe;
  2020. };
  2021. }
  2022. };
  2023.  
  2024. var soundcloud = {
  2025. name: "SoundCloud",
  2026. domains: ["soundcloud.com"],
  2027. getPatterns: function() {
  2028. return [
  2029. /soundcloud\.com\/[\w-]+\/[\w-]+(?:\?|$)/i
  2030. ];
  2031. },
  2032. getEmbedFunction: function(){
  2033. return function(url, text, node, replace) {
  2034. GM_xmlhttpRequest({
  2035. method: "GET",
  2036. url: "https://soundcloud.com/oembed?format=json&url=" + url,
  2037. onload: function(response) {
  2038. if (!response.responseText) {
  2039. return;
  2040. }
  2041. var html = JSON.parse(response.responseText).html;
  2042. var container = document.createElement("div");
  2043. container.innerHTML = html;
  2044. replace(container);
  2045. }
  2046. });
  2047. };
  2048. }
  2049. };
  2050.  
  2051. var twitch = {
  2052. name: "Twitch",
  2053. domains: ["www.twitch.tv"],
  2054. getPatterns: function() {
  2055. return [
  2056. /twitch\.tv\/(\w+)\/(v)\/(\d+)/i,
  2057. /twitch\.tv\/()(videos)\/(\d+)/i,
  2058. /twitch\.tv\/(\w+)\/(clip)\/([^/]+)/i,
  2059. ];
  2060. },
  2061. getEmbedFunction: function() {
  2062. return function (user, type, id) {
  2063. var container = document.createElement("div");
  2064. if (type == "v" || type == "videos") {
  2065. container.innerHTML = `<iframe src="https://player.twitch.tv/?video=${id}&autoplay=false&parent=${location.hostname}" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>`;
  2066. } else if (type == "clip") {
  2067. container.innerHTML = `<iframe src="https://clips.twitch.tv/embed?clip=${id}&autoplay=false&parent=${location.hostname}" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>`;
  2068. }
  2069. return container;
  2070. };
  2071. }
  2072. };
  2073.  
  2074. var video = {
  2075. name: "Video",
  2076. global: true,
  2077. getPatterns: function() {
  2078. return [
  2079. /^[^?#]+\.(?:mp4|webm|ogv|mov)(?:$|[?#])/i
  2080. ];
  2081. },
  2082. getEmbedFunction: function() {
  2083. return function (url, text) {
  2084. var video = document.createElement("video");
  2085. video.controls = true;
  2086. video.title = text;
  2087. video.src = url;
  2088. return video;
  2089. };
  2090. }
  2091. };
  2092.  
  2093. var youtube = {
  2094. name: "Youtube",
  2095. domains: [
  2096. "www.youtube.com",
  2097. "youtu.be"
  2098. ],
  2099. getPatterns: function() {
  2100. return [
  2101. /youtube\.com\/watch\?.*?v=([^&]+)/i,
  2102. /youtu\.be\/([^?]+)/i,
  2103. /youtube\.com\/embed\/([^?#]+)/,
  2104. /youtube\.com\/v\/([^?#]+)/
  2105. ];
  2106. },
  2107. getEmbedFunction: function() {
  2108. return function(id, url, text, node, replace) {
  2109. url = "https://www.youtube.com/watch?v=" + id;
  2110. GM_xmlhttpRequest({
  2111. method: "GET",
  2112. url: "https://www.youtube.com/oembed?format=json&url=" + url,
  2113. onload: function(response) {
  2114. var html = JSON.parse(response.responseText).html,
  2115. container = document.createElement("div");
  2116.  
  2117. container.innerHTML = html;
  2118. replace(container);
  2119. }
  2120. });
  2121. };
  2122. }
  2123. };
  2124.  
  2125. var modules = [fumen,image,imgur,soundcloud,twitch,video,youtube];
  2126.  
  2127. /* global GM_webextPref */
  2128.  
  2129.  
  2130. const pref = GM_webextPref({
  2131. default: {
  2132. simple: true,
  2133. excludes: "",
  2134. ...Object.fromEntries(modules.map(m => [m.name, true]))
  2135. },
  2136. body: [
  2137. {
  2138. label: "Ignore complex anchor",
  2139. type: "checkbox",
  2140. key: "simple"
  2141. },
  2142. {
  2143. label: "Excludes these urls (regexp per line)",
  2144. type: "textarea",
  2145. key: "excludes"
  2146. },
  2147. ...modules.map(module => ({
  2148. label: module.name,
  2149. key: module.name,
  2150. type: "checkbox"
  2151. }))
  2152. ],
  2153. getNewScope: () => location.hostname
  2154. });
  2155. const globalMods = [];
  2156. const index = {};
  2157. let excludedUrl = null;
  2158.  
  2159. pref.ready()
  2160. .then(() => pref.setCurrentScope(location.hostname))
  2161. .then(init);
  2162.  
  2163. function init() {
  2164. pref.on("change", change => {
  2165. if (change.excludes != null) {
  2166. updateExclude();
  2167. }
  2168. });
  2169. updateExclude();
  2170. for (const mod of modules) {
  2171. if (mod.global) {
  2172. globalMods.push(mod);
  2173. } else {
  2174. var i;
  2175. for (i = 0; i < mod.domains.length; i++) {
  2176. index[mod.domains[i]] = mod;
  2177. }
  2178. }
  2179. }
  2180. observeDocument(function(node){
  2181. var links = node.querySelectorAll("a[href]"), i;
  2182. for (i = 0; i < links.length; i++) {
  2183. embed(links[i]);
  2184. }
  2185. });
  2186. }
  2187.  
  2188. function validParent(node) {
  2189. var cache = node;
  2190. while (node != document.documentElement) {
  2191. if (node.INVALID || node.className.indexOf("embed-me") >= 0) {
  2192. cache.INVALID = true;
  2193. return false;
  2194. }
  2195. if (!node.parentNode) {
  2196. return false;
  2197. }
  2198. if (node.VALID) {
  2199. break;
  2200. }
  2201. node = node.parentNode;
  2202. }
  2203. cache.VALID = true;
  2204. return true;
  2205. }
  2206.  
  2207. function valid(node) {
  2208. if (!validParent(node)) {
  2209. return false;
  2210. }
  2211. if (node.nodeName != "A" || !node.href) {
  2212. return false;
  2213. }
  2214. if (pref.get("simple") && (node.childNodes.length != 1 || node.childNodes[0].nodeType != 3)) {
  2215. return false;
  2216. }
  2217. if (excludedUrl && excludedUrl.test(node.href)) {
  2218. return false;
  2219. }
  2220. return true;
  2221. }
  2222.  
  2223. function getPatterns(mod) {
  2224. if (!mod.getPatterns) {
  2225. return [];
  2226. }
  2227. if (!mod.patterns) {
  2228. mod.patterns = mod.getPatterns();
  2229. }
  2230. return mod.patterns;
  2231. }
  2232.  
  2233. function getEmbedFunction(mod) {
  2234. if (!mod.embedFunction) {
  2235. mod.embedFunction = mod.getEmbedFunction();
  2236. }
  2237. return mod.embedFunction;
  2238. }
  2239.  
  2240. function callEmbedFunc(node, params, func) {
  2241. var replace = function (newNode) {
  2242. if (!node.parentNode) {
  2243. // The node was detached from DOM tree
  2244. return;
  2245. }
  2246. newNode.classList.add("embed-me");
  2247. node.parentNode.replaceChild(newNode, node);
  2248. };
  2249. params.push(node.href, node.textContent, node, replace);
  2250. var result = func.apply(null, params);
  2251. if (result) {
  2252. replace(result);
  2253. }
  2254. }
  2255.  
  2256. function embed(node) {
  2257. if (!valid(node)) {
  2258. return;
  2259. }
  2260. // Never process same element twice
  2261. node.INVALID = true;
  2262.  
  2263. var mods = [], mod, patterns, match, i, j;
  2264.  
  2265. if (node.hostname in index) {
  2266. mods.push(index[node.hostname]);
  2267. }
  2268.  
  2269. mods = mods.concat(globalMods).filter(mod => pref.get(mod.name));
  2270.  
  2271. for (j = 0; j < mods.length; j++) {
  2272. mod = mods[j];
  2273. patterns = getPatterns(mod);
  2274.  
  2275. for (i = 0; i < patterns.length; i++) {
  2276. if ((match = patterns[i].exec(node.href))) {
  2277. callEmbedFunc(node, Array.prototype.slice.call(match, 1), getEmbedFunction(mod));
  2278. return;
  2279. }
  2280. }
  2281. }
  2282. }
  2283.  
  2284. function observeDocument(callback) {
  2285.  
  2286. setTimeout(callback, 0, document.body);
  2287.  
  2288. new MutationObserver(function(mutations){
  2289. var i;
  2290. for (i = 0; i < mutations.length; i++) {
  2291. if (!mutations[i].addedNodes.length) {
  2292. continue;
  2293. }
  2294. callback(mutations[i].target);
  2295. }
  2296. }).observe(document.body, {
  2297. childList: true,
  2298. subtree: true
  2299. });
  2300. }
  2301.  
  2302. function updateExclude() {
  2303. const excludes = pref.get("excludes").trim();
  2304. excludedUrl = excludes && new RegExp(excludes.split(/\s*\n\s*/).join("|"), "i");
  2305. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址