@xpadev-net/niconicomments 0.2.20 → 0.2.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bundle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- niconicomments.js v0.2.20
2
+ niconicomments.js v0.2.23
3
3
  (c) 2021 xpadev-net https://xpadev.net
4
4
  Released under the MIT License.
5
5
  */
@@ -24,35 +24,782 @@
24
24
 
25
25
  return _assign.apply(this, arguments);
26
26
  };
27
+ function __spreadArray(to, from, pack) {
28
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
29
+ if (ar || !(i in from)) {
30
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
31
+ ar[i] = from[i];
32
+ }
33
+ }
34
+ return to.concat(ar || Array.prototype.slice.call(from));
35
+ }
36
+
37
+ var typeGuard = {
38
+ formatted: {
39
+ comment: function (i) {
40
+ return typeVerify(i, [
41
+ "id",
42
+ "vpos",
43
+ "content",
44
+ "date",
45
+ "date_usec",
46
+ "owner",
47
+ "premium",
48
+ "mail",
49
+ "user_id",
50
+ "layer",
51
+ ]);
52
+ },
53
+ comments: function (i) {
54
+ if (typeof i !== "object")
55
+ return false;
56
+ for (var _i = 0, _a = i; _i < _a.length; _i++) {
57
+ var item = _a[_i];
58
+ if (!typeGuard.formatted.comment(item))
59
+ return false;
60
+ }
61
+ return true;
62
+ },
63
+ legacyComment: function (i) {
64
+ return typeVerify(i, [
65
+ "id",
66
+ "vpos",
67
+ "content",
68
+ "date",
69
+ "owner",
70
+ "premium",
71
+ "mail",
72
+ ]);
73
+ },
74
+ legacyComments: function (i) {
75
+ if (typeof i !== "object")
76
+ return false;
77
+ for (var _i = 0, _a = i; _i < _a.length; _i++) {
78
+ var item = _a[_i];
79
+ if (!typeGuard.formatted.legacyComment(item))
80
+ return false;
81
+ }
82
+ return true;
83
+ },
84
+ },
85
+ legacy: {
86
+ rawApiResponses: function (i) {
87
+ if (typeof i !== "object")
88
+ return false;
89
+ for (var _i = 0, _a = i; _i < _a.length; _i++) {
90
+ var itemWrapper = _a[_i];
91
+ for (var _b = 0, _c = Object.keys(itemWrapper); _b < _c.length; _b++) {
92
+ var key = _c[_b];
93
+ var item = itemWrapper[key];
94
+ if (!item)
95
+ continue;
96
+ if (!(typeGuard.legacy.apiChat(item) ||
97
+ typeGuard.legacy.apiGlobalNumRes(item) ||
98
+ typeGuard.legacy.apiLeaf(item) ||
99
+ typeGuard.legacy.apiPing(item) ||
100
+ typeGuard.legacy.apiThread(item))) {
101
+ return false;
102
+ }
103
+ }
104
+ }
105
+ return true;
106
+ },
107
+ apiChat: function (i) {
108
+ return typeof i === "object" &&
109
+ typeVerify(i, [
110
+ "content",
111
+ "date",
112
+ "date_usec",
113
+ "no",
114
+ "thread",
115
+ "vpos",
116
+ ]);
117
+ },
118
+ apiGlobalNumRes: function (i) {
119
+ return typeVerify(i, ["num_res", "thread"]);
120
+ },
121
+ apiLeaf: function (i) { return typeVerify(i, ["count", "thread"]); },
122
+ apiPing: function (i) { return typeVerify(i, ["content"]); },
123
+ apiThread: function (i) {
124
+ return typeVerify(i, [
125
+ "resultcode",
126
+ "revision",
127
+ "server_time",
128
+ "thread",
129
+ "ticket",
130
+ ]);
131
+ },
132
+ },
133
+ niconicome: {
134
+ xmlDocument: function (i) {
135
+ if (!i.documentElement ||
136
+ i.documentElement.nodeName !== "packet")
137
+ return false;
138
+ if (!i.documentElement.children)
139
+ return false;
140
+ for (var index = 0; index < i.documentElement.children.length; index++) {
141
+ var value = i.documentElement.children[index];
142
+ if (!value)
143
+ continue;
144
+ if (index === 0) {
145
+ if (value.nodeName !== "thread" ||
146
+ !typeAttributeVerify(value, [
147
+ "resultcode",
148
+ "thread",
149
+ "server_time",
150
+ "last_res",
151
+ "revision",
152
+ ]))
153
+ return false;
154
+ }
155
+ else {
156
+ if (value.nodeName !== "chat" ||
157
+ !typeAttributeVerify(value, [
158
+ "thread",
159
+ "no",
160
+ "vpos",
161
+ "date",
162
+ "date_usec",
163
+ "anonymity",
164
+ "user_id",
165
+ "mail",
166
+ "leaf",
167
+ "premium",
168
+ "score",
169
+ ]))
170
+ return false;
171
+ }
172
+ }
173
+ return true;
174
+ },
175
+ },
176
+ legacyOwner: {
177
+ comments: function (i) {
178
+ if (typeof i !== "string")
179
+ return false;
180
+ var lists = i.split("\n");
181
+ for (var _i = 0, lists_1 = lists; _i < lists_1.length; _i++) {
182
+ var list = lists_1[_i];
183
+ if (list.split(":").length < 3) {
184
+ return false;
185
+ }
186
+ }
187
+ return true;
188
+ },
189
+ },
190
+ owner: {
191
+ comment: function (i) {
192
+ return typeVerify(i, ["time", "command", "comment"]);
193
+ },
194
+ comments: function (i) {
195
+ if (typeof i !== "object")
196
+ return false;
197
+ for (var _i = 0, _a = i; _i < _a.length; _i++) {
198
+ var item = _a[_i];
199
+ if (!typeGuard.owner.comment(item))
200
+ return false;
201
+ }
202
+ return true;
203
+ },
204
+ },
205
+ v1: {
206
+ comment: function (i) {
207
+ return typeVerify(i, [
208
+ "id",
209
+ "no",
210
+ "vposMs",
211
+ "body",
212
+ "commands",
213
+ "userId",
214
+ "isPremium",
215
+ "score",
216
+ "postedAt",
217
+ "nicoruCount",
218
+ "nicoruId",
219
+ "source",
220
+ "isMyPost",
221
+ ]);
222
+ },
223
+ thread: function (i) {
224
+ if (!typeVerify(i, ["id", "fork", "commentCount", "comments"]))
225
+ return false;
226
+ for (var _i = 0, _a = Object.keys(i.comments); _i < _a.length; _i++) {
227
+ var item = _a[_i];
228
+ if (!typeGuard.v1.comment(i.comments[item]))
229
+ return false;
230
+ }
231
+ return true;
232
+ },
233
+ threads: function (i) {
234
+ if (typeof i !== "object")
235
+ return false;
236
+ for (var _i = 0, _a = i; _i < _a.length; _i++) {
237
+ var item = _a[_i];
238
+ if (!typeGuard.v1.thread(item))
239
+ return false;
240
+ }
241
+ return true;
242
+ },
243
+ },
244
+ nicoScript: {
245
+ range: {
246
+ target: function (i) {
247
+ return typeof i === "string" && !!i.match(/^(?:コメ|投コメ|全)$/);
248
+ },
249
+ },
250
+ replace: {
251
+ range: function (i) {
252
+ return typeof i === "string" && !!i.match(/^(?:単|全)$/);
253
+ },
254
+ target: function (i) {
255
+ return typeof i === "string" &&
256
+ !!i.match(/^(?:コメ|投コメ|全|含む|含まない)$/);
257
+ },
258
+ condition: function (i) {
259
+ return typeof i === "string" && !!i.match(/^(?:部分一致|完全一致)$/);
260
+ },
261
+ },
262
+ },
263
+ comment: {
264
+ font: function (i) {
265
+ return typeof i === "string" && !!i.match(/^(?:gothic|mincho|defont)$/);
266
+ },
267
+ loc: function (i) {
268
+ return typeof i === "string" && !!i.match(/^(?:ue|naka|shita)$/);
269
+ },
270
+ size: function (i) {
271
+ return typeof i === "string" && !!i.match(/^(?:big|medium|small)$/);
272
+ },
273
+ command: {
274
+ key: function (i) {
275
+ return typeof i === "string" && !!i.match(/^(?:full|ender|_live|invisible)$/);
276
+ },
277
+ },
278
+ },
279
+ };
280
+ var typeVerify = function (item, keys) {
281
+ if (typeof item !== "object" || !item)
282
+ return false;
283
+ for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
284
+ var key = keys_1[_i];
285
+ if (!Object.prototype.hasOwnProperty.call(item, key))
286
+ return false;
287
+ }
288
+ return true;
289
+ };
290
+ var typeAttributeVerify = function (item, keys) {
291
+ if (typeof item !== "object" || !item)
292
+ return false;
293
+ for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
294
+ var key = keys_2[_i];
295
+ if (item.getAttribute(key) === null)
296
+ return false;
297
+ }
298
+ return true;
299
+ };
300
+
301
+ var convert2formattedComment = function (data, type) {
302
+ var result = [];
303
+ if (type === "niconicome" && typeGuard.niconicome.xmlDocument(data)) {
304
+ result = fromNiconicome(data);
305
+ }
306
+ else if (type === "formatted" && typeGuard.formatted.legacyComments(data)) {
307
+ result = fromFormatted(data);
308
+ }
309
+ else if (type === "legacy" && typeGuard.legacy.rawApiResponses(data)) {
310
+ result = fromLegacy(data);
311
+ }
312
+ else if (type === "legacyOwner" && typeGuard.legacyOwner.comments(data)) {
313
+ result = fromLegacyOwner(data);
314
+ }
315
+ else if (type === "owner" && typeGuard.owner.comments(data)) {
316
+ result = fromOwner(data);
317
+ }
318
+ else if (type === "v1" && typeGuard.v1.threads(data)) {
319
+ result = fromV1(data);
320
+ }
321
+ else {
322
+ throw new Error("unknown input format");
323
+ }
324
+ return sort(result);
325
+ };
326
+ var fromNiconicome = function (data) {
327
+ var _a;
328
+ var data_ = [], userList = [];
329
+ for (var _i = 0, _b = Array.from(data.documentElement.children); _i < _b.length; _i++) {
330
+ var item = _b[_i];
331
+ if (item.nodeName !== "chat")
332
+ continue;
333
+ var tmpParam = {
334
+ id: Number(item.getAttribute("no")),
335
+ vpos: Number(item.getAttribute("vpos")),
336
+ content: item.innerHTML,
337
+ date: Number(item.getAttribute("date")),
338
+ date_usec: Number(item.getAttribute("date_usec")),
339
+ owner: !item.getAttribute("user_id"),
340
+ premium: item.getAttribute("premium") === "1",
341
+ mail: [],
342
+ user_id: -1,
343
+ layer: -1,
344
+ };
345
+ if (item.getAttribute("mail")) {
346
+ tmpParam.mail = ((_a = item.getAttribute("mail")) === null || _a === void 0 ? void 0 : _a.split(/\s+/g)) || [];
347
+ }
348
+ if (tmpParam.content.startsWith("/") && tmpParam.owner) {
349
+ tmpParam.mail.push("invisible");
350
+ }
351
+ var userId = item.getAttribute("user_id") || "";
352
+ var isUserExist = userList.indexOf(userId);
353
+ if (isUserExist === -1) {
354
+ tmpParam.user_id = userList.length;
355
+ userList.push(userId);
356
+ }
357
+ else {
358
+ tmpParam.user_id = isUserExist;
359
+ }
360
+ data_.push(tmpParam);
361
+ }
362
+ return data_;
363
+ };
364
+ var fromFormatted = function (data) {
365
+ var tmpData = data;
366
+ if (!typeGuard.formatted.comments(data)) {
367
+ for (var _i = 0, tmpData_1 = tmpData; _i < tmpData_1.length; _i++) {
368
+ var item = tmpData_1[_i];
369
+ item.layer = -1;
370
+ item.user_id = 0;
371
+ if (!item.date_usec)
372
+ item.date_usec = 0;
373
+ }
374
+ }
375
+ return tmpData;
376
+ };
377
+ var fromLegacy = function (data) {
378
+ var data_ = [], userList = [];
379
+ for (var i = 0; i < data.length; i++) {
380
+ var val = data[i];
381
+ if (!val || !typeGuard.legacy.apiChat(val === null || val === void 0 ? void 0 : val.chat))
382
+ continue;
383
+ var value = val.chat;
384
+ if (value.deleted !== 1) {
385
+ var tmpParam = {
386
+ id: value.no,
387
+ vpos: value.vpos,
388
+ content: value.content,
389
+ date: value.date,
390
+ date_usec: value.date_usec || 0,
391
+ owner: !value.user_id,
392
+ premium: value.premium === 1,
393
+ mail: [],
394
+ user_id: -1,
395
+ layer: -1,
396
+ };
397
+ if (value.mail) {
398
+ tmpParam.mail = value.mail.split(/\s+/g);
399
+ }
400
+ if (value.content.startsWith("/") && !value.user_id) {
401
+ tmpParam.mail.push("invisible");
402
+ }
403
+ var isUserExist = userList.indexOf(value.mail);
404
+ if (isUserExist === -1) {
405
+ tmpParam.user_id = userList.length;
406
+ userList.push(value.user_id);
407
+ }
408
+ else {
409
+ tmpParam.user_id = isUserExist;
410
+ }
411
+ data_.push(tmpParam);
412
+ }
413
+ }
414
+ return data_;
415
+ };
416
+ var fromLegacyOwner = function (data) {
417
+ var data_ = [], comments = data.split("\n");
418
+ for (var i = 0; i < comments.length; i++) {
419
+ var commentData = comments[i].split(":");
420
+ if (commentData.length < 3) {
421
+ continue;
422
+ }
423
+ else if (commentData.length > 3) {
424
+ for (var j = 3; j < commentData.length; j++) {
425
+ commentData[2] += ":" + commentData[j];
426
+ }
427
+ }
428
+ var tmpParam = {
429
+ id: i,
430
+ vpos: Number(commentData[0]),
431
+ content: commentData[2] || "",
432
+ date: i,
433
+ date_usec: 0,
434
+ owner: true,
435
+ premium: true,
436
+ mail: [],
437
+ user_id: -1,
438
+ layer: -1,
439
+ };
440
+ if (commentData[1]) {
441
+ tmpParam.mail = commentData[1].split(/[\s+]/g);
442
+ }
443
+ if (tmpParam.content.startsWith("/")) {
444
+ tmpParam.mail.push("invisible");
445
+ }
446
+ data_.push(tmpParam);
447
+ }
448
+ return data_;
449
+ };
450
+ var fromOwner = function (data) {
451
+ var data_ = [];
452
+ data.forEach(function (value, index) {
453
+ var tmpParam = {
454
+ id: index,
455
+ vpos: time2vpos(value.time),
456
+ content: value.comment,
457
+ date: index,
458
+ date_usec: 0,
459
+ owner: true,
460
+ premium: true,
461
+ mail: [],
462
+ user_id: -1,
463
+ layer: -1,
464
+ };
465
+ if (value.command) {
466
+ tmpParam.mail = value.command.split(/\s+/g);
467
+ }
468
+ if (tmpParam.content.startsWith("/")) {
469
+ tmpParam.mail.push("invisible");
470
+ }
471
+ data_.push(tmpParam);
472
+ });
473
+ return data_;
474
+ };
475
+ var fromV1 = function (data) {
476
+ var data_ = [], userList = [];
477
+ for (var _i = 0, data_1 = data; _i < data_1.length; _i++) {
478
+ var item = data_1[_i];
479
+ var val = item.comments, forkName = item.fork;
480
+ for (var _a = 0, _b = Object.keys(val); _a < _b.length; _a++) {
481
+ var key = _b[_a];
482
+ var value = val[key];
483
+ if (!value)
484
+ continue;
485
+ var tmpParam = {
486
+ id: value.no,
487
+ vpos: Math.floor(value.vposMs / 10),
488
+ content: value.body,
489
+ date: date2time(value.postedAt),
490
+ date_usec: 0,
491
+ owner: forkName === "owner",
492
+ premium: value.isPremium,
493
+ mail: value.commands,
494
+ user_id: -1,
495
+ layer: -1,
496
+ };
497
+ if (tmpParam.content.startsWith("/") && tmpParam.owner) {
498
+ tmpParam.mail.push("invisible");
499
+ }
500
+ var isUserExist = userList.indexOf(value.userId);
501
+ if (isUserExist === -1) {
502
+ tmpParam.user_id = userList.length;
503
+ userList.push(value.userId);
504
+ }
505
+ else {
506
+ tmpParam.user_id = isUserExist;
507
+ }
508
+ data_.push(tmpParam);
509
+ }
510
+ }
511
+ return data_;
512
+ };
513
+ var sort = function (data) {
514
+ data.sort(function (a, b) {
515
+ if (a.vpos < b.vpos)
516
+ return -1;
517
+ if (a.vpos > b.vpos)
518
+ return 1;
519
+ if (a.date < b.date)
520
+ return -1;
521
+ if (a.date > b.date)
522
+ return 1;
523
+ if (a.date_usec < b.date_usec)
524
+ return -1;
525
+ if (a.date_usec > b.date_usec)
526
+ return 1;
527
+ return 0;
528
+ });
529
+ return data;
530
+ };
531
+ var time2vpos = function (time_str) {
532
+ var time = time_str.match(/^(?:(\d+):(\d+)\.(\d+)|(\d+):(\d+)|(\d+)\.(\d+)|(\d+))$/);
533
+ if (time) {
534
+ if (time[1] !== undefined &&
535
+ time[2] !== undefined &&
536
+ time[3] !== undefined) {
537
+ return ((Number(time[1]) * 60 + Number(time[2])) * 100 +
538
+ Number(time[3]) / Math.pow(10, time[3].length - 2));
539
+ }
540
+ else if (time[4] !== undefined && time[5] !== undefined) {
541
+ return (Number(time[4]) * 60 + Number(time[5])) * 100;
542
+ }
543
+ else if (time[6] !== undefined && time[7] !== undefined) {
544
+ return (Number(time[6]) * 100 +
545
+ Number(time[7]) / Math.pow(10, time[7].length - 2));
546
+ }
547
+ else if (time[8] !== undefined) {
548
+ return Number(time[8]) * 100;
549
+ }
550
+ }
551
+ return 0;
552
+ };
553
+ var date2time = function (date) {
554
+ return Math.floor(new Date(date).getTime() / 1000);
555
+ };
556
+
557
+ var colors = {
558
+ white: "#FFFFFF",
559
+ red: "#FF0000",
560
+ pink: "#FF8080",
561
+ orange: "#FFC000",
562
+ yellow: "#FFFF00",
563
+ green: "#00FF00",
564
+ cyan: "#00FFFF",
565
+ blue: "#0000FF",
566
+ purple: "#C000FF",
567
+ black: "#000000",
568
+ white2: "#CCCC99",
569
+ niconicowhite: "#CCCC99",
570
+ red2: "#CC0033",
571
+ truered: "#CC0033",
572
+ pink2: "#FF33CC",
573
+ orange2: "#FF6600",
574
+ passionorange: "#FF6600",
575
+ yellow2: "#999900",
576
+ madyellow: "#999900",
577
+ green2: "#00CC66",
578
+ elementalgreen: "#00CC66",
579
+ cyan2: "#00CCCC",
580
+ blue2: "#3399FF",
581
+ marinblue: "#3399FF",
582
+ purple2: "#6633CC",
583
+ nobleviolet: "#6633CC",
584
+ black2: "#666666",
585
+ };
586
+ var commentYPaddingTop = 0.08;
587
+ var commentYMarginBottom = 0.24;
588
+ var fontSize = {
589
+ small: {
590
+ default: 47,
591
+ resized: 26.1,
592
+ },
593
+ medium: {
594
+ default: 74,
595
+ resized: 38.7,
596
+ },
597
+ big: {
598
+ default: 110,
599
+ resized: 61,
600
+ },
601
+ };
602
+ var lineHeight = {
603
+ small: {
604
+ default: 1,
605
+ resized: 1,
606
+ },
607
+ medium: {
608
+ default: 1,
609
+ resized: 1,
610
+ },
611
+ big: {
612
+ default: 1.03,
613
+ resized: 1.01,
614
+ },
615
+ };
616
+ var doubleResizeMaxWidth = {
617
+ full: {
618
+ legacy: 3020,
619
+ default: 3550,
620
+ },
621
+ normal: {
622
+ legacy: 2540,
623
+ default: 2650,
624
+ },
625
+ };
626
+ var defaultOptions = {
627
+ drawAllImageOnLoad: false,
628
+ format: "default",
629
+ formatted: false,
630
+ debug: false,
631
+ enableLegacyPiP: false,
632
+ keepCA: false,
633
+ showCollision: false,
634
+ showCommentCount: false,
635
+ showFPS: false,
636
+ useLegacy: false,
637
+ video: undefined,
638
+ };
639
+ var fpsInterval = 500;
640
+ var cacheAge = 2000;
641
+ var canvasWidth = 1920;
642
+ var canvasHeight = 1080;
643
+ var commentDrawRange = 1450;
644
+ var commentDrawPadding = (canvasWidth - commentDrawRange) / 2;
645
+ var collisionWidth = 40;
646
+ var collisionRange = {
647
+ left: collisionWidth,
648
+ right: canvasWidth - collisionWidth,
649
+ };
650
+ var sameCARange = 3600;
651
+ var sameCAGap = 100;
652
+ var sameCAMinScore = 10;
653
+
654
+ var groupBy = function (array) {
655
+ var data = ["defont", "gothic", "mincho"].reduce(function (pv, font) {
656
+ pv[font] = {};
657
+ return pv;
658
+ }, {});
659
+ array.forEach(function (item, index) {
660
+ var value = data[item.font][item.fontSize] || [];
661
+ value.push(_assign(_assign({}, item), { index: index }));
662
+ if (value.length === 1) {
663
+ data[item.font][item.fontSize] = value;
664
+ }
665
+ });
666
+ return data;
667
+ };
668
+ var getPosY = function (currentPos, targetComment, collision, data) {
669
+ var isChanged = false, isBreak = false;
670
+ if (!collision)
671
+ return { currentPos: currentPos, isChanged: isChanged, isBreak: isBreak };
672
+ for (var _i = 0, collision_1 = collision; _i < collision_1.length; _i++) {
673
+ var index = collision_1[_i];
674
+ var collisionItem = data[index];
675
+ if (!collisionItem)
676
+ continue;
677
+ if (currentPos < collisionItem.posY + collisionItem.height &&
678
+ currentPos + targetComment.height > collisionItem.posY &&
679
+ collisionItem.owner === targetComment.owner &&
680
+ collisionItem.layer === targetComment.layer) {
681
+ if (collisionItem.posY + collisionItem.height > currentPos) {
682
+ currentPos = collisionItem.posY + collisionItem.height;
683
+ isChanged = true;
684
+ }
685
+ if (currentPos + targetComment.height > canvasHeight) {
686
+ if (canvasHeight < targetComment.height) {
687
+ if (targetComment.mail.includes("naka")) {
688
+ currentPos = (targetComment.height - canvasHeight) / -2;
689
+ }
690
+ else {
691
+ currentPos = 0;
692
+ }
693
+ }
694
+ else {
695
+ currentPos = Math.floor(Math.random() * (canvasHeight - targetComment.height));
696
+ }
697
+ isBreak = true;
698
+ break;
699
+ }
700
+ }
701
+ }
702
+ return { currentPos: currentPos, isChanged: isChanged, isBreak: isBreak };
703
+ };
704
+ var getPosX = function (width, vpos, long) {
705
+ return (commentDrawRange -
706
+ ((((width + commentDrawRange) * ((vpos + 100) / 100)) / 4) * 300) / long +
707
+ commentDrawPadding);
708
+ };
709
+ var parseFont = function (font, size, useLegacy) {
710
+ switch (font) {
711
+ case "gothic":
712
+ return "normal 400 ".concat(size, "px \"\u6E38\u30B4\u30B7\u30C3\u30AF\u4F53\", \"\u6E38\u30B4\u30B7\u30C3\u30AF\", \"Yu Gothic\", YuGothic, yugothic, YuGo-Medium");
713
+ case "mincho":
714
+ return "normal 400 ".concat(size, "px \"\u6E38\u660E\u671D\u4F53\", \"\u6E38\u660E\u671D\", \"Yu Mincho\", YuMincho, yumincho, YuMin-Medium");
715
+ default:
716
+ if (useLegacy) {
717
+ return "normal 600 ".concat(size, "px Arial, \"\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF\", \"MS PGothic\", MSPGothic, MS-PGothic");
718
+ }
719
+ else {
720
+ return "normal 600 ".concat(size, "px sans-serif, Arial, \"\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF\", \"MS PGothic\", MSPGothic, MS-PGothic");
721
+ }
722
+ }
723
+ };
724
+ var arrayPush = function (array, key, push) {
725
+ var _a;
726
+ if (!array) {
727
+ array = {};
728
+ }
729
+ if (!array[Number(key)]) {
730
+ array[Number(key)] = [];
731
+ }
732
+ (_a = array[Number(key)]) === null || _a === void 0 ? void 0 : _a.push(push);
733
+ };
734
+ var hex2rgb = function (hex) {
735
+ if (hex.slice(0, 1) === "#")
736
+ hex = hex.slice(1);
737
+ if (hex.length === 3)
738
+ hex =
739
+ hex.slice(0, 1) +
740
+ hex.slice(0, 1) +
741
+ hex.slice(1, 2) +
742
+ hex.slice(1, 2) +
743
+ hex.slice(2, 3) +
744
+ hex.slice(2, 3);
745
+ return [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)].map(function (str) {
746
+ return parseInt(str, 16);
747
+ });
748
+ };
749
+ var replaceAll = function (string, target, replace) {
750
+ while (string.indexOf(target) !== -1) {
751
+ string = string.replace(target, replace);
752
+ }
753
+ return string;
754
+ };
755
+ var changeCALayer = function (rawData) {
756
+ var userList = {};
757
+ var data = [], index = {};
758
+ for (var _i = 0, rawData_1 = rawData; _i < rawData_1.length; _i++) {
759
+ var value = rawData_1[_i];
760
+ if (value.user_id === undefined || value.user_id === -1)
761
+ continue;
762
+ if (userList[value.user_id] === undefined)
763
+ userList[value.user_id] = 0;
764
+ if (value.mail.indexOf("ca") > -1 ||
765
+ value.mail.indexOf("patissier") > -1 ||
766
+ value.mail.indexOf("ender") > -1 ||
767
+ value.mail.indexOf("full") > -1) {
768
+ userList[value.user_id] += 5;
769
+ }
770
+ if ((value.content.match(/\r\n|\n|\r/g) || []).length > 2) {
771
+ userList[value.user_id] +=
772
+ (value.content.match(/\r\n|\n|\r/g) || []).length / 2;
773
+ }
774
+ var key = "".concat(value.content, "@@").concat(Array.from(new Set(__spreadArray([], value.mail, true).sort()))
775
+ .filter(function (e) { return !e.match(/@[\d.]+|184|device:.+|patissier|ca/); })
776
+ .join("")), lastComment = index[key];
777
+ if (lastComment !== undefined) {
778
+ if (value.vpos - lastComment.vpos > sameCAGap ||
779
+ Math.abs(value.date - lastComment.date) < sameCARange) {
780
+ data.push(value);
781
+ index[key] = value;
782
+ }
783
+ }
784
+ else {
785
+ data.push(value);
786
+ index[key] = value;
787
+ }
788
+ }
789
+ for (var _a = 0, data_1 = data; _a < data_1.length; _a++) {
790
+ var value = data_1[_a];
791
+ if (userList[value.user_id] || 0 >= sameCAMinScore)
792
+ value.layer = value.user_id;
793
+ }
794
+ return data;
795
+ };
27
796
 
28
797
  var isDebug = false;
29
798
  var NiconiComments = (function () {
30
799
  function NiconiComments(canvas, data, initOptions) {
31
- if (initOptions === void 0) { initOptions = {
32
- useLegacy: false,
33
- formatted: false,
34
- video: null,
35
- showCollision: false,
36
- showFPS: false,
37
- showCommentCount: false,
38
- drawAllImageOnLoad: false,
39
- debug: false,
40
- enableLegacyPiP: false,
41
- keepCA: false
42
- }; }
800
+ if (initOptions === void 0) { initOptions = {}; }
43
801
  var _this = this;
44
- var options = Object.assign({
45
- useLegacy: false,
46
- formatted: false,
47
- video: null,
48
- showCollision: false,
49
- showFPS: false,
50
- showCommentCount: false,
51
- drawAllImageOnLoad: false,
52
- debug: false,
53
- enableLegacyPiP: false,
54
- keepCA: true
55
- }, initOptions);
802
+ var options = Object.assign(defaultOptions, initOptions);
56
803
  isDebug = options.debug;
57
804
  var constructorStart = performance.now();
58
805
  this.canvas = canvas;
@@ -64,59 +811,27 @@
64
811
  this.context.textAlign = "start";
65
812
  this.context.textBaseline = "alphabetic";
66
813
  this.context.lineWidth = 4;
67
- this.commentYPaddingTop = 0.08;
68
- this.commentYMarginBottom = 0.24;
69
- this.fontSize = {
70
- "small": {
71
- "default": 47,
72
- "resized": 26.1
73
- },
74
- "medium": {
75
- "default": 74,
76
- "resized": 38.7
77
- },
78
- "big": {
79
- "default": 110,
80
- "resized": 61
81
- }
82
- };
83
- this.lineHeight = {
84
- "small": {
85
- "default": 1,
86
- "resized": 1
87
- },
88
- "medium": {
89
- "default": 1,
90
- "resized": 1
91
- },
92
- "big": {
93
- "default": 1.03,
94
- "resized": 1.01
95
- }
96
- };
97
- this.doubleResizeMaxWidth = {
98
- full: {
99
- legacy: 3020,
100
- default: 3550
101
- },
102
- normal: {
103
- legacy: 2540,
104
- default: 2650
105
- }
106
- };
107
- var parsedData = options.formatted ? data : this.parseData(data);
108
- this.video = options.video ? options.video : null;
814
+ var formatType = options.format;
815
+ if (options.formatted) {
816
+ console.warn("Deprecated: options.formatted is no longer recommended. Please use options.format");
817
+ }
818
+ if (formatType === "default") {
819
+ formatType = options.formatted ? "formatted" : "legacy";
820
+ }
821
+ var parsedData = convert2formattedComment(data, formatType);
822
+ this.video = options.video || undefined;
109
823
  this.showCollision = options.showCollision;
110
824
  this.showFPS = options.showFPS;
111
825
  this.showCommentCount = options.showCommentCount;
112
826
  this.enableLegacyPiP = options.enableLegacyPiP;
113
827
  this.keepCA = options.keepCA;
828
+ this.cacheIndex = {};
114
829
  this.timeline = {};
115
830
  this.nicoScripts = { reverse: [], default: [], replace: [], ban: [] };
116
- this.collision_right = {};
117
- this.collision_left = {};
118
- this.collision_ue = {};
119
- this.collision_shita = {};
831
+ this.collision = ["ue", "shita", "right", "left"].reduce(function (pv, value) {
832
+ pv[value] = [];
833
+ return pv;
834
+ }, {});
120
835
  this.data = [];
121
836
  this.lastVpos = -1;
122
837
  this.useLegacy = options.useLegacy;
@@ -124,70 +839,13 @@
124
839
  this.fpsCount = 0;
125
840
  this.fps = 0;
126
841
  window.setInterval(function () {
127
- _this.fps = _this.fpsCount * 2;
842
+ _this.fps = _this.fpsCount * (1000 / fpsInterval);
128
843
  _this.fpsCount = 0;
129
- }, 500);
844
+ }, fpsInterval);
130
845
  logger("constructor complete: ".concat(performance.now() - constructorStart, "ms"));
131
846
  }
132
- NiconiComments.prototype.parseData = function (data) {
133
- var parseDataStart = performance.now();
134
- var data_ = [], userList = [];
135
- for (var i = 0; i < data.length; i++) {
136
- var val = data[i];
137
- if (!val)
138
- continue;
139
- for (var key in val) {
140
- var value = val[key];
141
- if (isApiChat(value) && value["deleted"] !== 1) {
142
- var tmpParam = {
143
- "id": value["no"],
144
- "vpos": value["vpos"],
145
- "content": value["content"],
146
- "date": value["date"],
147
- "date_usec": value["date_usec"],
148
- "owner": !value["user_id"],
149
- "premium": value["premium"] === 1,
150
- "mail": [],
151
- "user_id": -1,
152
- "layer": -1
153
- };
154
- if (value["mail"]) {
155
- tmpParam["mail"] = value["mail"].split(/[\s ]/g);
156
- }
157
- if (value["content"].startsWith("/") && !value["user_id"]) {
158
- tmpParam["mail"].push("invisible");
159
- }
160
- var isUserExist = userList.indexOf(value["user_id"]);
161
- if (isUserExist === -1) {
162
- tmpParam.user_id = userList.length;
163
- userList.push(value.user_id);
164
- }
165
- else {
166
- tmpParam.user_id = isUserExist;
167
- }
168
- data_.push(tmpParam);
169
- }
170
- }
171
- }
172
- data_.sort(function (a, b) {
173
- if (a.vpos < b.vpos)
174
- return -1;
175
- if (a.vpos > b.vpos)
176
- return 1;
177
- if (a.date < b.date)
178
- return -1;
179
- if (a.date > b.date)
180
- return 1;
181
- if (a.date_usec < b.date_usec)
182
- return -1;
183
- if (a.date_usec > b.date_usec)
184
- return 1;
185
- return 0;
186
- });
187
- logger("parseData complete: ".concat(performance.now() - parseDataStart, "ms"));
188
- return data_;
189
- };
190
847
  NiconiComments.prototype.preRendering = function (rawData, drawAll) {
848
+ var _this = this;
191
849
  var preRenderingStart = performance.now();
192
850
  if (this.keepCA) {
193
851
  rawData = changeCALayer(rawData);
@@ -195,34 +853,35 @@
195
853
  var parsedData = this.getCommentPos(this.getCommentSize(this.getFont(rawData)));
196
854
  this.data = this.sortComment(parsedData);
197
855
  if (drawAll) {
198
- for (var i in parsedData) {
199
- this.getTextImage(Number(i), true);
200
- }
856
+ parsedData.forEach(function (_, key) { return _this.getTextImage(Number(key), true); });
201
857
  }
202
858
  logger("preRendering complete: ".concat(performance.now() - preRenderingStart, "ms"));
203
859
  };
204
860
  NiconiComments.prototype.getFont = function (parsedData) {
205
861
  var getFontStart = performance.now();
206
862
  var result = [];
207
- for (var i in parsedData) {
208
- var value = parsedData[i];
209
- if (!value)
210
- continue;
863
+ for (var _i = 0, parsedData_1 = parsedData; _i < parsedData_1.length; _i++) {
864
+ var value = parsedData_1[_i];
211
865
  value.content = value.content.replace(/\t/g, "\u2003\u2003");
212
- result[i] = this.parseCommandAndNicoscript(value);
866
+ result.push(this.parseCommandAndNicoscript(value));
213
867
  }
214
868
  logger("getFont complete: ".concat(performance.now() - getFontStart, "ms"));
215
869
  return result;
216
870
  };
217
871
  NiconiComments.prototype.getCommentSize = function (parsedData) {
218
872
  var getCommentSizeStart = performance.now();
219
- var tmpData = groupBy(parsedData, "font", "fontSize");
873
+ var groupedData = groupBy(parsedData);
220
874
  var result = [];
221
- for (var i in tmpData) {
222
- for (var j in tmpData[i]) {
223
- this.context.font = parseFont(i, j, this.useLegacy);
224
- for (var k in tmpData[i][j]) {
225
- var comment = tmpData[i][j][k];
875
+ for (var _i = 0, _a = Object.keys(groupedData); _i < _a.length; _i++) {
876
+ var font = _a[_i];
877
+ for (var _b = 0, _c = Object.keys(groupedData[font]); _b < _c.length; _b++) {
878
+ var fontSize_1 = _c[_b];
879
+ var value = groupedData[font][fontSize_1];
880
+ if (!value)
881
+ continue;
882
+ this.context.font = parseFont(font, fontSize_1, this.useLegacy);
883
+ for (var _d = 0, value_1 = value; _d < value_1.length; _d++) {
884
+ var comment = value_1[_d];
226
885
  if (comment.invisible) {
227
886
  continue;
228
887
  }
@@ -235,7 +894,7 @@
235
894
  size.lineHeight = measure.lineHeight;
236
895
  if (measure.resized) {
237
896
  size.fontSize = measure.fontSize;
238
- this.context.font = parseFont(i, j, this.useLegacy);
897
+ this.context.font = parseFont(font, fontSize_1, this.useLegacy);
239
898
  }
240
899
  result[comment.index] = size;
241
900
  }
@@ -244,207 +903,147 @@
244
903
  logger("getCommentSize complete: ".concat(performance.now() - getCommentSizeStart, "ms"));
245
904
  return result;
246
905
  };
247
- NiconiComments.prototype.getCommentPos = function (parsedData) {
906
+ NiconiComments.prototype.getCommentPos = function (data) {
907
+ var _this = this;
248
908
  var getCommentPosStart = performance.now();
249
- var data = parsedData;
250
- for (var i in data) {
251
- var comment = data[i];
252
- if (!comment || comment.invisible) {
253
- continue;
254
- }
255
- for (var j = 0; j < comment.long * 4 / 3 + 100; j++) {
256
- if (!this.timeline[comment.vpos + j]) {
257
- this.timeline[comment.vpos + j] = [];
258
- }
259
- if (!this.collision_right[comment.vpos + j]) {
260
- this.collision_right[comment.vpos + j] = [];
261
- }
262
- if (!this.collision_left[comment.vpos + j]) {
263
- this.collision_left[comment.vpos + j] = [];
264
- }
265
- if (!this.collision_ue[comment.vpos + j]) {
266
- this.collision_ue[comment.vpos + j] = [];
267
- }
268
- if (!this.collision_shita[comment.vpos + j]) {
269
- this.collision_shita[comment.vpos + j] = [];
270
- }
271
- }
909
+ data.forEach(function (comment, index) {
910
+ if (comment.invisible)
911
+ return;
272
912
  if (comment.loc === "naka") {
273
- var posY = 0, is_break = false, is_change = true, count = 0, beforeVpos = Math.round(-240 / ((1680 + comment.width_max) / (comment.long + 125))) - 100;
274
- if (1080 < comment.height) {
275
- posY = (comment.height - 1080) / -2;
913
+ var posY = 0;
914
+ var beforeVpos = Math.round(-288 / ((1632 + comment.width_max) / (comment.long + 125))) - 100;
915
+ if (canvasHeight < comment.height) {
916
+ posY = (comment.height - canvasHeight) / -2;
276
917
  }
277
918
  else {
278
- while (is_change && count < 10) {
279
- is_change = false;
919
+ var isBreak = false, isChanged = true, count = 0;
920
+ while (isChanged && count < 10) {
921
+ isChanged = false;
280
922
  count++;
281
- for (var j = beforeVpos; j < comment.long; j++) {
923
+ for (var j = beforeVpos; j < comment.long + 125; j++) {
282
924
  var vpos = comment.vpos + j;
283
- var left_pos = 1680 - (1680 + comment.width_max) / (comment.long + 125) * j;
284
- if (left_pos + comment.width_max >= 1880) {
285
- for (var k in this.collision_right[vpos]) {
286
- var l = this.collision_right[vpos][k];
287
- if ((posY < data[l].posY + data[l].height && posY + comment.height > data[l].posY) && data[l].owner === comment.owner && data[l].layer === comment.layer) {
288
- if (data[l].posY + data[l].height > posY) {
289
- posY = data[l].posY + data[l].height;
290
- is_change = true;
291
- }
292
- if (posY + comment.height > 1080) {
293
- if (1080 < comment.height) {
294
- posY = (comment.height - 1080) / -2;
295
- }
296
- else {
297
- posY = Math.floor(Math.random() * (1080 - comment.height));
298
- }
299
- is_break = true;
300
- break;
301
- }
302
- }
303
- }
304
- if (is_break) {
925
+ var left_pos = getPosX(comment.width_max, j, comment.long);
926
+ if (left_pos + comment.width_max >= collisionRange.right &&
927
+ left_pos <= collisionRange.right) {
928
+ var result = getPosY(posY, comment, _this.collision.right[vpos], data);
929
+ posY = result.currentPos;
930
+ isChanged = result.isChanged;
931
+ isBreak = result.isBreak;
932
+ if (isBreak)
305
933
  break;
306
- }
307
934
  }
308
- if (left_pos <= 40) {
309
- for (var k in this.collision_left[vpos]) {
310
- var l = this.collision_left[vpos][k];
311
- if ((posY < data[l].posY + data[l].height && posY + comment.height > data[l].posY) && data[l].owner === comment.owner && data[l].layer === comment.layer) {
312
- if (data[l].posY + data[l].height > posY) {
313
- posY = data[l].posY + data[l].height;
314
- is_change = true;
315
- }
316
- if (posY + comment.height > 1080) {
317
- if (1080 < comment.height) {
318
- posY = 0;
319
- }
320
- else {
321
- posY = Math.random() * (1080 - comment.height);
322
- }
323
- is_break = true;
324
- break;
325
- }
326
- }
327
- }
328
- if (is_break) {
935
+ if (left_pos + comment.width_max >= collisionRange.left &&
936
+ left_pos <= collisionRange.left) {
937
+ var result = getPosY(posY, comment, _this.collision.left[vpos], data);
938
+ posY = result.currentPos;
939
+ isChanged = result.isChanged;
940
+ isBreak = result.isBreak;
941
+ if (isBreak)
329
942
  break;
330
- }
331
943
  }
332
944
  }
333
- if (is_break) {
945
+ if (isBreak) {
334
946
  break;
335
947
  }
336
948
  }
337
949
  }
338
950
  for (var j = beforeVpos; j < comment.long + 125; j++) {
339
951
  var vpos = comment.vpos + j;
340
- var left_pos = 1680 - (1680 + comment.width_max) / (comment.long + 125) * j;
341
- arrayPush(this.timeline, vpos, i);
342
- if (left_pos + comment.width_max >= 1880) {
343
- arrayPush(this.collision_right, vpos, i);
952
+ var left_pos = getPosX(comment.width_max, j, comment.long);
953
+ arrayPush(_this.timeline, vpos, index);
954
+ if (left_pos + comment.width_max >= collisionRange.right) {
955
+ arrayPush(_this.collision.right, vpos, index);
344
956
  }
345
- if (left_pos <= 40) {
346
- arrayPush(this.collision_left, vpos, i);
957
+ if (left_pos <= collisionRange.left) {
958
+ arrayPush(_this.collision.left, vpos, index);
347
959
  }
348
960
  }
349
- parsedData[i].posY = posY;
961
+ comment.posY = posY;
350
962
  }
351
963
  else {
352
- var posY = 0, is_break = false, is_change = true, count = 0, collision = void 0;
964
+ var posY = 0, isChanged = true, count = 0, collision = void 0;
353
965
  if (comment.loc === "ue") {
354
- collision = this.collision_ue;
966
+ collision = _this.collision.ue;
355
967
  }
356
- else if (comment.loc === "shita") {
357
- collision = this.collision_shita;
968
+ else {
969
+ collision = _this.collision.shita;
358
970
  }
359
- while (is_change && count < 10) {
360
- is_change = false;
971
+ while (isChanged && count < 10) {
972
+ isChanged = false;
361
973
  count++;
362
974
  for (var j = 0; j < comment.long; j++) {
363
- var vpos = comment.vpos + j;
364
- for (var k in collision[vpos]) {
365
- var l = collision[vpos][k];
366
- if ((posY < data[l].posY + data[l].height && posY + comment.height > data[l].posY) && data[l].owner === comment.owner && data[l].layer === comment.layer) {
367
- if (data[l].posY + data[l].height > posY) {
368
- posY = data[l].posY + data[l].height;
369
- is_change = true;
370
- }
371
- if (posY + comment.height > 1080) {
372
- if (1000 <= comment.height) {
373
- posY = 0;
374
- }
375
- else {
376
- posY = Math.floor(Math.random() * (1080 - comment.height));
377
- }
378
- is_break = true;
379
- break;
380
- }
381
- }
382
- }
383
- if (is_break) {
975
+ var result = getPosY(posY, comment, collision[comment.vpos + j], data);
976
+ posY = result.currentPos;
977
+ isChanged = result.isChanged;
978
+ if (result.isBreak)
384
979
  break;
385
- }
386
980
  }
387
981
  }
388
982
  for (var j = 0; j < comment.long; j++) {
389
983
  var vpos = comment.vpos + j;
390
- arrayPush(this.timeline, vpos, i);
984
+ arrayPush(_this.timeline, vpos, index);
985
+ if (j > comment.long - 20)
986
+ continue;
391
987
  if (comment.loc === "ue") {
392
- arrayPush(this.collision_ue, vpos, i);
988
+ arrayPush(_this.collision.ue, vpos, index);
393
989
  }
394
990
  else {
395
- arrayPush(this.collision_shita, vpos, i);
991
+ arrayPush(_this.collision.shita, vpos, index);
396
992
  }
397
993
  }
398
- parsedData[i].posY = posY;
994
+ comment.posY = posY;
399
995
  }
400
- }
996
+ });
401
997
  logger("getCommentPos complete: ".concat(performance.now() - getCommentPosStart, "ms"));
402
- return parsedData;
998
+ return data;
403
999
  };
404
1000
  NiconiComments.prototype.sortComment = function (parsedData) {
1001
+ var _a;
405
1002
  var sortCommentStart = performance.now();
406
- for (var vpos in this.timeline) {
407
- if (!this.timeline[vpos])
1003
+ for (var _i = 0, _b = Object.keys(this.timeline); _i < _b.length; _i++) {
1004
+ var vpos = _b[_i];
1005
+ var item = this.timeline[Number(vpos)];
1006
+ if (!item)
408
1007
  continue;
409
1008
  var owner = [], user = [];
410
- for (var _i = 0, _a = this.timeline[vpos]; _i < _a.length; _i++) {
411
- var i = _a[_i];
412
- if (parsedData[i].owner) {
413
- owner.push(i);
1009
+ for (var _c = 0, item_1 = item; _c < item_1.length; _c++) {
1010
+ var index = item_1[_c];
1011
+ if ((_a = parsedData[index]) === null || _a === void 0 ? void 0 : _a.owner) {
1012
+ owner.push(index);
414
1013
  }
415
1014
  else {
416
- user.push(i);
1015
+ user.push(index);
417
1016
  }
418
1017
  }
419
- this.timeline[vpos] = owner.concat(user);
1018
+ this.timeline[Number(vpos)] = owner.concat(user);
420
1019
  }
421
1020
  logger("parseData complete: ".concat(performance.now() - sortCommentStart, "ms"));
422
1021
  return parsedData;
423
1022
  };
424
1023
  NiconiComments.prototype.measureText = function (comment) {
425
- var width, width_max, width_min, height, width_arr = [], lines = comment.content.split("\n");
1024
+ var width_arr = [], lines = comment.content.split("\n");
426
1025
  if (!comment.lineHeight)
427
- comment.lineHeight = this.lineHeight[comment.size].default;
1026
+ comment.lineHeight = lineHeight[comment.size].default;
428
1027
  if (!comment.resized && !comment.ender) {
429
1028
  if (comment.size === "big" && lines.length > 2) {
430
- comment.fontSize = this.fontSize.big.resized;
431
- comment.lineHeight = this.lineHeight.big.resized;
1029
+ comment.fontSize = fontSize.big.resized;
1030
+ comment.lineHeight = lineHeight.big.resized;
432
1031
  comment.resized = true;
433
- comment.tateresized = true;
1032
+ comment.resizedY = true;
434
1033
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
435
1034
  }
436
1035
  else if (comment.size === "medium" && lines.length > 4) {
437
- comment.fontSize = this.fontSize.medium.resized;
438
- comment.lineHeight = this.lineHeight.medium.resized;
1036
+ comment.fontSize = fontSize.medium.resized;
1037
+ comment.lineHeight = lineHeight.medium.resized;
439
1038
  comment.resized = true;
440
- comment.tateresized = true;
1039
+ comment.resizedY = true;
441
1040
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
442
1041
  }
443
1042
  else if (comment.size === "small" && lines.length > 6) {
444
- comment.fontSize = this.fontSize.small.resized;
445
- comment.lineHeight = this.lineHeight.small.resized;
1043
+ comment.fontSize = fontSize.small.resized;
1044
+ comment.lineHeight = lineHeight.small.resized;
446
1045
  comment.resized = true;
447
- comment.tateresized = true;
1046
+ comment.resizedY = true;
448
1047
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
449
1048
  }
450
1049
  }
@@ -452,84 +1051,106 @@
452
1051
  var measure = this.context.measureText(lines[i]);
453
1052
  width_arr.push(measure.width);
454
1053
  }
455
- width = width_arr.reduce(function (p, c) { return p + c; }, 0) / width_arr.length;
456
- width_max = Math.max.apply(Math, width_arr);
457
- width_min = Math.min.apply(Math, width_arr);
458
- height = (comment.fontSize * comment.lineHeight * (1 + this.commentYPaddingTop) * lines.length) + (this.commentYMarginBottom * comment.fontSize);
459
- if (comment.loc !== "naka" && !comment.tateresized) {
460
- if (comment.full && width_max > 1930) {
461
- comment.fontSize -= 2;
462
- comment.resized = true;
463
- comment.yokoResized = true;
464
- this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
465
- return this.measureText(comment);
466
- }
467
- else if (!comment.full && width_max > 1440) {
468
- comment.fontSize -= 1;
1054
+ var width = width_arr.reduce(function (p, c) { return p + c; }, 0) / width_arr.length;
1055
+ var width_max = Math.max.apply(Math, width_arr), width_min = Math.min.apply(Math, width_arr), height = comment.fontSize *
1056
+ comment.lineHeight *
1057
+ (1 + commentYPaddingTop) *
1058
+ lines.length +
1059
+ commentYMarginBottom * comment.fontSize;
1060
+ if (comment.loc !== "naka" && !comment.resizedY) {
1061
+ if ((comment.full && width_max > 1930) ||
1062
+ (!comment.full && width_max > 1440)) {
1063
+ while (width_max > (comment.full ? 1930 : 1440)) {
1064
+ width_max /= 1.1;
1065
+ width_max /= 1.1;
1066
+ comment.fontSize -= 2;
1067
+ }
469
1068
  comment.resized = true;
470
- comment.yokoResized = true;
1069
+ comment.resizedX = true;
471
1070
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
472
1071
  return this.measureText(comment);
473
1072
  }
474
1073
  }
475
- else if (comment.loc !== "naka" && comment.tateresized && (comment.full && width_max > 2120 || !comment.full && width_max > 1440) && !comment.yokoResized) {
476
- comment.fontSize = this.fontSize[comment.size].default;
477
- comment.lineHeight = this.lineHeight[comment.size].default * 1.05;
1074
+ else if (comment.loc !== "naka" &&
1075
+ comment.resizedY &&
1076
+ ((comment.full && width_max > 2120) ||
1077
+ (!comment.full && width_max > 1440)) &&
1078
+ !comment.resizedX) {
1079
+ comment.fontSize = fontSize[comment.size].default;
1080
+ comment.lineHeight = lineHeight[comment.size].default * 1.05;
478
1081
  comment.resized = true;
479
- comment.yokoResized = true;
1082
+ comment.resizedX = true;
480
1083
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
481
1084
  return this.measureText(comment);
482
1085
  }
483
- else if (comment.loc !== "naka" && comment.tateresized && comment.yokoResized) {
484
- if (comment.full && width_max > this.doubleResizeMaxWidth.full[this.useLegacy ? "legacy" : "default"]) {
485
- comment.fontSize -= 1;
1086
+ else if (comment.loc !== "naka" && comment.resizedY && comment.resizedX) {
1087
+ if (comment.full &&
1088
+ width_max >
1089
+ doubleResizeMaxWidth.full[this.useLegacy ? "legacy" : "default"]) {
1090
+ while (width_max >
1091
+ doubleResizeMaxWidth.full[this.useLegacy ? "legacy" : "default"]) {
1092
+ width_max /= 1.1;
1093
+ comment.fontSize -= 1;
1094
+ }
486
1095
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
487
1096
  return this.measureText(comment);
488
1097
  }
489
- else if (!comment.full && width_max > this.doubleResizeMaxWidth.normal[this.useLegacy ? "legacy" : "default"]) {
490
- comment.fontSize -= 1;
1098
+ else if (!comment.full &&
1099
+ width_max >
1100
+ doubleResizeMaxWidth.normal[this.useLegacy ? "legacy" : "default"]) {
1101
+ while (width_max >
1102
+ doubleResizeMaxWidth.normal[this.useLegacy ? "legacy" : "default"]) {
1103
+ width_max /= 1.1;
1104
+ comment.fontSize -= 1;
1105
+ }
491
1106
  this.context.font = parseFont(comment.font, comment.fontSize, this.useLegacy);
492
1107
  return this.measureText(comment);
493
1108
  }
494
1109
  }
495
1110
  return {
496
- "width": width,
497
- "width_max": width_max,
498
- "width_min": width_min,
499
- "height": height,
500
- "resized": comment.resized,
501
- "fontSize": comment.fontSize,
502
- "lineHeight": comment.lineHeight
1111
+ width: width,
1112
+ width_max: width_max,
1113
+ width_min: width_min,
1114
+ height: height,
1115
+ resized: !!comment.resized,
1116
+ fontSize: comment.fontSize,
1117
+ lineHeight: comment.lineHeight,
503
1118
  };
504
1119
  };
505
1120
  NiconiComments.prototype.drawText = function (comment, vpos) {
1121
+ var _this = this;
506
1122
  var reverse = false;
507
- for (var i in this.nicoScripts.reverse) {
508
- var range = this.nicoScripts.reverse[i];
509
- if ((range.target === "コメ" && comment.owner) || (range.target === "投コメ" && !comment.owner)) {
1123
+ for (var _i = 0, _a = this.nicoScripts.reverse; _i < _a.length; _i++) {
1124
+ var range = _a[_i];
1125
+ if ((range.target === "コメ" && comment.owner) ||
1126
+ (range.target === "投コメ" && !comment.owner))
510
1127
  break;
511
- }
512
1128
  if (range.start < vpos && vpos < range.end) {
513
1129
  reverse = true;
514
1130
  }
515
1131
  }
516
- for (var i in this.nicoScripts.ban) {
517
- var range = this.nicoScripts.ban[i];
518
- if (range.start < vpos && vpos < range.end) {
1132
+ for (var _b = 0, _c = this.nicoScripts.ban; _b < _c.length; _b++) {
1133
+ var range = _c[_b];
1134
+ if (range.start < vpos && vpos < range.end)
519
1135
  return;
520
- }
521
1136
  }
522
- var posX = (1920 - comment.width_max) / 2, posY = comment.posY;
1137
+ var posX = (canvasWidth - comment.width_max) / 2, posY = comment.posY;
523
1138
  if (comment.loc === "naka") {
524
1139
  if (reverse) {
525
- posX = 240 + (1680 + comment.width_max) / (comment.long + 125) * (vpos - comment.vpos + 100) - comment.width_max;
1140
+ posX =
1141
+ canvasWidth +
1142
+ comment.width_max -
1143
+ getPosX(comment.width_max, vpos - comment.vpos, comment.long);
526
1144
  }
527
1145
  else {
528
- posX = 1680 - (1680 + comment.width_max) / (comment.long + 125) * (vpos - comment.vpos + 100);
1146
+ posX = getPosX(comment.width_max, vpos - comment.vpos, comment.long);
1147
+ }
1148
+ if (posX > canvasWidth || posX + comment.width_max < 0) {
1149
+ return;
529
1150
  }
530
1151
  }
531
1152
  else if (comment.loc === "shita") {
532
- posY = 1080 - comment.posY - comment.height;
1153
+ posY = canvasHeight - comment.posY - comment.height;
533
1154
  }
534
1155
  if (comment.image && comment.image !== true) {
535
1156
  this.context.drawImage(comment.image, posX, posY);
@@ -538,19 +1159,48 @@
538
1159
  this.context.strokeStyle = "rgba(0,255,255,1)";
539
1160
  this.context.strokeRect(posX, posY, comment.width_max, comment.height);
540
1161
  var lines = comment.content.split("\n");
541
- for (var i in lines) {
542
- var linePosY = (Number(i) + 1) * (comment.fontSize * comment.lineHeight) * (1 + this.commentYPaddingTop);
543
- this.context.strokeStyle = "rgba(255,255,0,0.5)";
544
- this.context.strokeRect(posX, posY + linePosY, comment.width_max, comment.fontSize * comment.lineHeight * -1);
545
- }
1162
+ lines.forEach(function (_, index) {
1163
+ var linePosY = (Number(index) + 1) *
1164
+ (comment.fontSize * comment.lineHeight) *
1165
+ (1 + commentYPaddingTop);
1166
+ _this.context.strokeStyle = "rgba(255,255,0,0.5)";
1167
+ _this.context.strokeRect(posX, posY + linePosY, comment.width_max, comment.fontSize * comment.lineHeight * -1);
1168
+ });
1169
+ }
1170
+ if (isDebug) {
1171
+ var font = this.context.font;
1172
+ var fillStyle = this.context.fillStyle;
1173
+ this.context.font = parseFont("defont", 30, false);
1174
+ this.context.fillStyle = "#ff00ff";
1175
+ this.context.fillText(comment.mail.join(","), posX, posY + 30);
1176
+ this.context.font = font;
1177
+ this.context.fillStyle = fillStyle;
546
1178
  }
547
1179
  };
548
1180
  NiconiComments.prototype.getTextImage = function (i, preRendering) {
549
1181
  var _this = this;
1182
+ var _a;
550
1183
  if (preRendering === void 0) { preRendering = false; }
551
1184
  var value = this.data[i];
552
1185
  if (!value || value.invisible)
553
1186
  return;
1187
+ var cacheKey = value.content + "@@@" + __spreadArray([], value.mail, true).sort().join(","), cache = this.cacheIndex[cacheKey];
1188
+ if (cache) {
1189
+ var image_1 = (_a = this.data[cache]) === null || _a === void 0 ? void 0 : _a.image;
1190
+ if (image_1) {
1191
+ this.cacheIndex[cacheKey] = i;
1192
+ value.image = image_1;
1193
+ setTimeout(function () {
1194
+ if (value.image) {
1195
+ delete value.image;
1196
+ }
1197
+ if (_this.cacheIndex[cacheKey] === i) {
1198
+ delete _this.cacheIndex[cacheKey];
1199
+ }
1200
+ }, value.long * 10 + cacheAge);
1201
+ return;
1202
+ }
1203
+ }
554
1204
  var image = document.createElement("canvas");
555
1205
  image.width = value.width_max;
556
1206
  image.height = value.height;
@@ -573,225 +1223,159 @@
573
1223
  context.strokeStyle = "rgba(255,255,255,0.7)";
574
1224
  }
575
1225
  var lines = value.content.split("\n");
576
- for (var i_1 in lines) {
577
- var line = lines[i_1], posY = void 0;
578
- posY = (Number(i_1) + 1) * (value.fontSize * value.lineHeight) * (1 + this.commentYPaddingTop);
1226
+ lines.forEach(function (line, index) {
1227
+ var posY = (index + 1) *
1228
+ (value.fontSize * value.lineHeight) *
1229
+ (1 + commentYPaddingTop);
579
1230
  context.strokeText(line, 0, posY);
580
1231
  context.fillText(line, 0, posY);
581
- }
582
- this.data[i].image = image;
1232
+ });
1233
+ value.image = image;
1234
+ this.cacheIndex[cacheKey] = i;
583
1235
  if (preRendering)
584
1236
  return;
585
1237
  setTimeout(function () {
586
- if (_this.data[i].image)
587
- delete _this.data[i].image;
588
- }, 5000);
1238
+ if (value.image) {
1239
+ delete value.image;
1240
+ }
1241
+ if (_this.cacheIndex[cacheKey] === i) {
1242
+ delete _this.cacheIndex[cacheKey];
1243
+ }
1244
+ }, value.long * 10 + cacheAge);
589
1245
  };
590
1246
  NiconiComments.prototype.parseCommand = function (comment) {
591
- var metadata = comment.mail, loc = null, size = null, fontSize = null, color = null, font = null, full = false, ender = false, _live = false, invisible = false, long = null;
592
- for (var i in metadata) {
593
- var command = metadata[i].toLowerCase();
1247
+ var metadata = comment.mail;
1248
+ var result = {
1249
+ loc: undefined,
1250
+ size: undefined,
1251
+ fontSize: undefined,
1252
+ color: undefined,
1253
+ font: undefined,
1254
+ full: false,
1255
+ ender: false,
1256
+ _live: false,
1257
+ invisible: false,
1258
+ long: undefined,
1259
+ };
1260
+ for (var _i = 0, metadata_1 = metadata; _i < metadata_1.length; _i++) {
1261
+ var command = metadata_1[_i];
1262
+ command = command.toLowerCase();
594
1263
  var match = command.match(/^@([0-9.]+)/);
595
- if (match) {
596
- long = match[1];
1264
+ if (match && match[1]) {
1265
+ result.long = Number(match[1]);
597
1266
  }
598
- if (loc === null) {
599
- switch (command) {
600
- case "ue":
601
- loc = "ue";
602
- break;
603
- case "shita":
604
- loc = "shita";
605
- break;
606
- }
1267
+ else if (result.loc === undefined && typeGuard.comment.loc(command)) {
1268
+ result.loc = command;
607
1269
  }
608
- if (size === null) {
609
- switch (command) {
610
- case "big":
611
- size = "big";
612
- fontSize = this.fontSize.big.default;
613
- break;
614
- case "small":
615
- size = "small";
616
- fontSize = this.fontSize.small.default;
617
- break;
618
- }
1270
+ else if (result.size === undefined && typeGuard.comment.size(command)) {
1271
+ result.size = command;
1272
+ result.fontSize = fontSize[command].default;
619
1273
  }
620
- if (color === null) {
621
- switch (command) {
622
- case "white":
623
- color = "#FFFFFF";
624
- break;
625
- case "red":
626
- color = "#FF0000";
627
- break;
628
- case "pink":
629
- color = "#FF8080";
630
- break;
631
- case "orange":
632
- color = "#FFC000";
633
- break;
634
- case "yellow":
635
- color = "#FFFF00";
636
- break;
637
- case "green":
638
- color = "#00FF00";
639
- break;
640
- case "cyan":
641
- color = "#00FFFF";
642
- break;
643
- case "blue":
644
- color = "#0000FF";
645
- break;
646
- case "purple":
647
- color = "#C000FF";
648
- break;
649
- case "black":
650
- color = "#000000";
651
- break;
652
- case "white2":
653
- case "niconicowhite":
654
- color = "#CCCC99";
655
- break;
656
- case "red2":
657
- case "truered":
658
- color = "#CC0033";
659
- break;
660
- case "pink2":
661
- color = "#FF33CC";
662
- break;
663
- case "orange2":
664
- case "passionorange":
665
- color = "#FF6600";
666
- break;
667
- case "yellow2":
668
- case "madyellow":
669
- color = "#999900";
670
- break;
671
- case "green2":
672
- case "elementalgreen":
673
- color = "#00CC66";
674
- break;
675
- case "cyan2":
676
- color = "#00CCCC";
677
- break;
678
- case "blue2":
679
- case "marineblue":
680
- color = "#3399FF";
681
- break;
682
- case "purple2":
683
- case "nobleviolet":
684
- color = "#6633CC";
685
- break;
686
- case "black2":
687
- color = "#666666";
688
- break;
689
- default:
1274
+ else {
1275
+ if (result.color === undefined) {
1276
+ var color = colors[command];
1277
+ if (color) {
1278
+ result.color = color;
1279
+ continue;
1280
+ }
1281
+ else {
690
1282
  var match_1 = command.match(/#[0-9a-z]{3,6}/);
691
- if (match_1 && comment.premium) {
692
- color = match_1[0].toUpperCase();
1283
+ if (match_1 && match_1[0] && comment.premium) {
1284
+ result.color = match_1[0].toUpperCase();
1285
+ continue;
693
1286
  }
694
- break;
1287
+ }
695
1288
  }
696
- }
697
- if (font === null) {
698
- switch (command) {
699
- case "gothic":
700
- font = "gothic";
701
- break;
702
- case "mincho":
703
- font = "mincho";
704
- break;
1289
+ if (result.font === undefined && typeGuard.comment.font(command)) {
1290
+ result.font = command;
1291
+ }
1292
+ else if (typeGuard.comment.command.key(command)) {
1293
+ result[command] = true;
705
1294
  }
706
- }
707
- switch (command) {
708
- case "full":
709
- full = true;
710
- break;
711
- case "ender":
712
- ender = true;
713
- break;
714
- case "_live":
715
- _live = true;
716
- break;
717
- case "invisible":
718
- invisible = true;
719
- break;
720
1295
  }
721
1296
  }
722
- return { loc: loc, size: size, fontSize: fontSize, color: color, font: font, full: full, ender: ender, _live: _live, invisible: invisible, long: long };
1297
+ return result;
723
1298
  };
724
1299
  NiconiComments.prototype.parseCommandAndNicoscript = function (comment) {
725
- var data = this.parseCommand(comment), nicoscript = comment.content.match(/^@(デフォルト|置換|逆|コメント禁止|シーク禁止|ジャンプ)/);
1300
+ var data = this.parseCommand(comment), string = comment.content, nicoscript = string.match(/^(?:@|@)(デフォルト|置換|逆|コメント禁止|シーク禁止|ジャンプ)/);
726
1301
  if (nicoscript) {
1302
+ var reverse = comment.content.match(/^@逆 ?(全|コメ|投コメ)?/);
1303
+ var content = comment.content.split(""), result = [];
1304
+ var quote = "", last_i = "", string_1 = "";
727
1305
  switch (nicoscript[1]) {
728
1306
  case "デフォルト":
729
- this.nicoScripts.default.push({
1307
+ this.nicoScripts.default.unshift({
730
1308
  start: comment.vpos,
731
- long: data.long === null ? null : Math.floor(data.long * 100),
1309
+ long: data.long === undefined ? undefined : Math.floor(data.long * 100),
732
1310
  color: data.color,
733
1311
  size: data.size,
734
1312
  font: data.font,
735
- loc: data.loc
1313
+ loc: data.loc,
736
1314
  });
737
1315
  break;
738
1316
  case "逆":
739
- var reverse = comment.content.match(/^@逆 ?(全|コメ|投コメ)?/);
740
- if (!reverse)
741
- reverse = [];
742
- if (!reverse[1]) {
743
- reverse[1] = "全";
744
- }
745
- if (data.long === null) {
1317
+ if (!reverse ||
1318
+ !reverse[1] ||
1319
+ !typeGuard.nicoScript.range.target(reverse[1]))
1320
+ break;
1321
+ if (data.long === undefined) {
746
1322
  data.long = 30;
747
1323
  }
748
- this.nicoScripts.reverse.push({
1324
+ this.nicoScripts.reverse.unshift({
749
1325
  start: comment.vpos,
750
- end: comment.vpos + (data.long * 100),
751
- target: reverse[1]
1326
+ end: comment.vpos + data.long * 100,
1327
+ target: reverse[1],
752
1328
  });
753
1329
  break;
754
1330
  case "コメント禁止":
755
- if (data.long === null) {
1331
+ if (data.long === undefined) {
756
1332
  data.long = 30;
757
1333
  }
758
- this.nicoScripts.reverse.push({
1334
+ this.nicoScripts.ban.unshift({
759
1335
  start: comment.vpos,
760
- end: comment.vpos + (data.long * 100),
1336
+ end: comment.vpos + data.long * 100,
761
1337
  });
762
1338
  break;
763
1339
  case "置換":
764
- var content = comment.content.split(""), quote = "", last_i = "", string = "", result = [];
765
1340
  for (var _i = 0, _a = content.slice(4); _i < _a.length; _i++) {
766
1341
  var i = _a[_i];
767
1342
  if (i.match(/["'「]/) && quote === "") {
768
1343
  quote = i;
769
1344
  }
770
1345
  else if (i.match(/["']/) && quote === i && last_i !== "\\") {
771
- result.push(replaceAll(string, "\\n", "\n"));
1346
+ result.push(replaceAll(string_1, "\\n", "\n"));
772
1347
  quote = "";
773
- string = "";
1348
+ string_1 = "";
774
1349
  }
775
1350
  else if (i.match(/」/) && quote === "「") {
776
- result.push(string);
1351
+ result.push(string_1);
777
1352
  quote = "";
778
- string = "";
1353
+ string_1 = "";
779
1354
  }
780
- else if (quote === "" && i.match(/[\s ]/)) {
781
- if (string) {
782
- result.push(string);
783
- string = "";
1355
+ else if (quote === "" && i.match(/\s+/)) {
1356
+ if (string_1) {
1357
+ result.push(string_1);
1358
+ string_1 = "";
784
1359
  }
785
1360
  }
786
1361
  else {
787
- string += i;
1362
+ string_1 += i;
788
1363
  }
789
1364
  last_i = i;
790
1365
  }
791
- result.push(string);
792
- this.nicoScripts.replace.push({
1366
+ result.push(string_1);
1367
+ if (result[0] === undefined ||
1368
+ result[1] === undefined ||
1369
+ (result[2] !== undefined &&
1370
+ !typeGuard.nicoScript.replace.range(result[2])) ||
1371
+ (result[3] !== undefined &&
1372
+ !typeGuard.nicoScript.replace.target(result[3])) ||
1373
+ (result[4] !== undefined &&
1374
+ !typeGuard.nicoScript.replace.condition(result[4])))
1375
+ break;
1376
+ this.nicoScripts.replace.unshift({
793
1377
  start: comment.vpos,
794
- long: data.long === null ? null : Math.floor(data.long * 100),
1378
+ long: data.long === undefined ? undefined : Math.floor(data.long * 100),
795
1379
  keyword: result[0],
796
1380
  replace: result[1] || "",
797
1381
  range: result[2] || "単",
@@ -800,40 +1384,63 @@
800
1384
  color: data.color,
801
1385
  size: data.size,
802
1386
  font: data.font,
803
- loc: data.loc
1387
+ loc: data.loc,
1388
+ no: comment.id,
1389
+ });
1390
+ this.nicoScripts.replace.sort(function (a, b) {
1391
+ if (a.start < b.start)
1392
+ return -1;
1393
+ if (a.start > b.start)
1394
+ return 1;
1395
+ if (a.no < b.no)
1396
+ return -1;
1397
+ if (a.no > b.no)
1398
+ return 1;
1399
+ return 0;
804
1400
  });
805
1401
  break;
806
1402
  }
807
1403
  data.invisible = true;
808
1404
  }
809
- var color = "#FFFFFF", size = "medium", font = "defont", loc = "naka";
810
- for (var i in this.nicoScripts.default) {
811
- if (this.nicoScripts.default[i].long !== null && this.nicoScripts.default[i].start + this.nicoScripts.default[i].long < comment.vpos) {
1405
+ var color = undefined, size = undefined, font = undefined, loc = undefined;
1406
+ for (var i = 0; i < this.nicoScripts.default.length; i++) {
1407
+ var item = this.nicoScripts.default[i];
1408
+ if (!item)
1409
+ continue;
1410
+ if (item.long !== undefined && item.start + item.long < comment.vpos) {
812
1411
  this.nicoScripts.default = this.nicoScripts.default.splice(Number(i), 1);
813
1412
  continue;
814
1413
  }
815
- if (this.nicoScripts.default[i].loc) {
816
- loc = this.nicoScripts.default[i].loc;
1414
+ if (item.loc) {
1415
+ loc = item.loc;
817
1416
  }
818
- if (this.nicoScripts.default[i].color) {
819
- color = this.nicoScripts.default[i].color;
1417
+ if (item.color) {
1418
+ color = item.color;
820
1419
  }
821
- if (this.nicoScripts.default[i].size) {
822
- size = this.nicoScripts.default[i].size;
1420
+ if (item.size) {
1421
+ size = item.size;
823
1422
  }
824
- if (this.nicoScripts.default[i].font) {
825
- font = this.nicoScripts.default[i].font;
1423
+ if (item.font) {
1424
+ font = item.font;
826
1425
  }
1426
+ if (loc && color && size && font)
1427
+ break;
827
1428
  }
828
- for (var i in this.nicoScripts.replace) {
829
- if (this.nicoScripts.replace[i].long !== null && this.nicoScripts.replace[i].start + this.nicoScripts.replace[i].long < comment.vpos) {
1429
+ for (var i = 0; i < this.nicoScripts.replace.length; i++) {
1430
+ var item = this.nicoScripts.replace[i];
1431
+ if (!item)
1432
+ continue;
1433
+ if (item.long !== undefined && item.start + item.long < comment.vpos) {
830
1434
  this.nicoScripts.default = this.nicoScripts.default.splice(Number(i), 1);
831
1435
  continue;
832
1436
  }
833
- var item = this.nicoScripts.replace[i];
834
- if ((item.target === "コメ" && comment.owner) || (item.target === "投コメ" && !comment.owner) || (item.target === "含まない" && comment.owner))
1437
+ if ((item.target === "コメ" && comment.owner) ||
1438
+ (item.target === "投コメ" && !comment.owner) ||
1439
+ (item.target === "含まない" && comment.owner))
835
1440
  continue;
836
- if ((item.condition === "完全一致" && comment.content === item.keyword) || (item.condition === "部分一致" && comment.content.indexOf(item.keyword) !== -1)) {
1441
+ if ((item.condition === "完全一致" && comment.content === item.keyword) ||
1442
+ (item.condition === "部分一致" &&
1443
+ comment.content.indexOf(item.keyword) !== -1)) {
837
1444
  if (item.range === "単") {
838
1445
  comment.content = replaceAll(comment.content, item.keyword, item.replace);
839
1446
  }
@@ -841,31 +1448,31 @@
841
1448
  comment.content = item.replace;
842
1449
  }
843
1450
  if (item.loc) {
844
- loc = item.loc;
1451
+ data.loc = item.loc;
845
1452
  }
846
1453
  if (item.color) {
847
- color = item.color;
1454
+ data.color = item.color;
848
1455
  }
849
1456
  if (item.size) {
850
- size = item.size;
1457
+ data.size = item.size;
851
1458
  }
852
1459
  if (item.font) {
853
- font = item.font;
1460
+ data.font = item.font;
854
1461
  }
855
1462
  }
856
1463
  }
857
1464
  if (!data.loc) {
858
- data.loc = loc;
1465
+ data.loc = loc || "naka";
859
1466
  }
860
1467
  if (!data.color) {
861
- data.color = color;
1468
+ data.color = color || "#FFFFFF";
862
1469
  }
863
1470
  if (!data.size) {
864
- data.size = size;
865
- data.fontSize = this.fontSize[data.size].default;
1471
+ data.size = size || "medium";
1472
+ data.fontSize = fontSize[data.size].default;
866
1473
  }
867
1474
  if (!data.font) {
868
- data.font = font;
1475
+ data.font = font || "defont";
869
1476
  }
870
1477
  if (!data.long) {
871
1478
  data.long = 300;
@@ -875,28 +1482,30 @@
875
1482
  }
876
1483
  return _assign(_assign({}, comment), data);
877
1484
  };
878
- NiconiComments.prototype.drawCanvas = function (vpos) {
1485
+ NiconiComments.prototype.drawCanvas = function (vpos, forceRendering) {
1486
+ if (forceRendering === void 0) { forceRendering = false; }
879
1487
  var drawCanvasStart = performance.now();
880
- if (this.lastVpos === vpos)
1488
+ if (this.lastVpos === vpos && !forceRendering)
881
1489
  return;
882
1490
  this.lastVpos = vpos;
883
1491
  this.fpsCount++;
884
1492
  this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
885
1493
  if (this.video) {
886
- var offsetX = void 0, offsetY = void 0, scale = void 0, height = this.canvas.height / this.video.videoHeight, width = this.canvas.width / this.video.videoWidth;
1494
+ var scale = void 0;
1495
+ var height = this.canvas.height / this.video.videoHeight, width = this.canvas.width / this.video.videoWidth;
887
1496
  if (this.enableLegacyPiP ? height > width : height < width) {
888
1497
  scale = width;
889
1498
  }
890
1499
  else {
891
1500
  scale = height;
892
1501
  }
893
- offsetX = (this.canvas.width - this.video.videoWidth * scale) * 0.5;
894
- offsetY = (this.canvas.height - this.video.videoHeight * scale) * 0.5;
1502
+ var offsetX = (this.canvas.width - this.video.videoWidth * scale) * 0.5, offsetY = (this.canvas.height - this.video.videoHeight * scale) * 0.5;
895
1503
  this.context.drawImage(this.video, offsetX, offsetY, this.video.videoWidth * scale, this.video.videoHeight * scale);
896
1504
  }
897
- if (this.timeline[vpos]) {
898
- for (var i in this.timeline[vpos]) {
899
- var index = this.timeline[vpos][Number(i)];
1505
+ var timelineRange = this.timeline[vpos];
1506
+ if (timelineRange) {
1507
+ for (var _i = 0, timelineRange_1 = timelineRange; _i < timelineRange_1.length; _i++) {
1508
+ var index = timelineRange_1[_i];
900
1509
  var comment = this.data[index];
901
1510
  if (!comment || comment.invisible) {
902
1511
  continue;
@@ -916,16 +1525,16 @@
916
1525
  this.context.font = parseFont("defont", 60, this.useLegacy);
917
1526
  this.context.fillStyle = "#00FF00";
918
1527
  this.context.strokeStyle = "rgba(0,0,0,0.7)";
919
- this.context.strokeText("FPS:" + this.fps, 100, 100);
920
- this.context.fillText("FPS:" + this.fps, 100, 100);
1528
+ this.context.strokeText("FPS:".concat(this.fps), 100, 100);
1529
+ this.context.fillText("FPS:".concat(this.fps), 100, 100);
921
1530
  }
922
1531
  if (this.showCommentCount) {
923
1532
  this.context.font = parseFont("defont", 60, this.useLegacy);
924
1533
  this.context.fillStyle = "#00FF00";
925
1534
  this.context.strokeStyle = "rgba(0,0,0,0.7)";
926
- if (this.timeline[vpos]) {
927
- this.context.strokeText("Count:" + this.timeline[vpos].length, 100, 200);
928
- this.context.fillText("Count:" + this.timeline[vpos].length, 100, 200);
1535
+ if (timelineRange) {
1536
+ this.context.strokeText("Count:".concat(timelineRange.length), 100, 200);
1537
+ this.context.fillText("Count:".concat(timelineRange.length), 100, 200);
929
1538
  }
930
1539
  else {
931
1540
  this.context.strokeText("Count:0", 100, 200);
@@ -935,101 +1544,14 @@
935
1544
  logger("drawCanvas complete: ".concat(performance.now() - drawCanvasStart, "ms"));
936
1545
  };
937
1546
  NiconiComments.prototype.clear = function () {
938
- this.context.clearRect(0, 0, 1920, 1080);
1547
+ this.context.clearRect(0, 0, canvasWidth, canvasHeight);
939
1548
  };
940
1549
  return NiconiComments;
941
1550
  }());
942
- var groupBy = function (array, key, key2) {
943
- var data = {};
944
- for (var i in array) {
945
- if (!data[array[i][key]]) {
946
- data[array[i][key]] = {};
947
- }
948
- if (!data[array[i][key]][array[i][key2]]) {
949
- data[array[i][key]][array[i][key2]] = [];
950
- }
951
- array[i].index = i;
952
- data[array[i][key]][array[i][key2]].push(array[i]);
953
- }
954
- return data;
955
- };
956
- var parseFont = function (font, size, useLegacy) {
957
- switch (font) {
958
- case "gothic":
959
- return "normal 400 ".concat(size, "px \"\u6E38\u30B4\u30B7\u30C3\u30AF\u4F53\", \"\u6E38\u30B4\u30B7\u30C3\u30AF\", \"Yu Gothic\", YuGothic, yugothic, YuGo-Medium");
960
- case "mincho":
961
- return "normal 400 ".concat(size, "px \"\u6E38\u660E\u671D\u4F53\", \"\u6E38\u660E\u671D\", \"Yu Mincho\", YuMincho, yumincho, YuMin-Medium");
962
- default:
963
- if (useLegacy) {
964
- return "normal 600 ".concat(size, "px Arial, \"\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF\", \"MS PGothic\", MSPGothic, MS-PGothic");
965
- }
966
- else {
967
- return "normal 600 ".concat(size, "px sans-serif, Arial, \"\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF\", \"MS PGothic\", MSPGothic, MS-PGothic");
968
- }
969
- }
970
- };
971
- var arrayPush = function (array, key, push) {
972
- if (!array) {
973
- array = {};
974
- }
975
- if (!array[key]) {
976
- array[key] = [];
977
- }
978
- array[key].push(push);
979
- };
980
- var hex2rgb = function (hex) {
981
- if (hex.slice(0, 1) === "#")
982
- hex = hex.slice(1);
983
- if (hex.length === 3)
984
- hex = hex.slice(0, 1) + hex.slice(0, 1) + hex.slice(1, 2) + hex.slice(1, 2) + hex.slice(2, 3) + hex.slice(2, 3);
985
- return [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)].map(function (str) {
986
- return parseInt(str, 16);
987
- });
988
- };
989
- var replaceAll = function (string, target, replace) {
990
- var count = 0;
991
- while (string.indexOf(target) !== -1 && count < 100) {
992
- string = string.replace(target, replace);
993
- count++;
994
- }
995
- return string;
996
- };
997
- var isApiChat = function (item) {
998
- return item.no && item.vpos && item.content;
999
- };
1000
1551
  var logger = function (msg) {
1001
1552
  if (isDebug)
1002
1553
  console.debug(msg);
1003
1554
  };
1004
- var changeCALayer = function (rawData) {
1005
- var userList = {};
1006
- var data = [], index = [];
1007
- for (var i in rawData) {
1008
- var value = rawData[i];
1009
- if (value.user_id === undefined || value.user_id === -1)
1010
- continue;
1011
- if (userList[value.user_id] === undefined)
1012
- userList[value.user_id] = 0;
1013
- if (value.mail.indexOf("ca") > -1 || value.mail.indexOf("patissier") > -1 || value.mail.indexOf("ender") > -1) {
1014
- userList[value.user_id] += 5;
1015
- }
1016
- if ((value.content.match(/\r\n|\n|\r/g) || []).length > 2) {
1017
- userList[value.user_id] += 1;
1018
- }
1019
- var key = "".concat(value.content, "@@").concat(Array.from(new Set(value.mail.sort())).filter(function (e) { return !e.match(/@[\d.]+|184|device:.+|patissier|ca/); }).join(""), "@@").concat(Math.floor(value.vpos * 10));
1020
- if (index.indexOf(key) === -1) {
1021
- index.push(key);
1022
- data.push(value);
1023
- }
1024
- }
1025
- for (var i in data) {
1026
- var value = data[i];
1027
- if (userList[value.user_id] !== undefined && userList[value.user_id] >= 10) {
1028
- value.layer = value.user_id;
1029
- }
1030
- }
1031
- return data;
1032
- };
1033
1555
 
1034
1556
  return NiconiComments;
1035
1557