-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathbiliTwinBabelCompiled.user.js
8947 lines (7697 loc) · 531 KB
/
biliTwinBabelCompiled.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name (Babel)bilibili merged flv+mp4+ass+enhance
// @namespace http://qli5.tk/
// @homepageURL https://github.com/liqi0816/bilitwin/
// @description (国产浏览器专用)bilibili/哔哩哔哩:超清FLV下载,FLV合并,原生MP4下载,弹幕ASS下载,MKV打包,播放体验增强,原生appsecret,不借助其他网站
// @match *://www.bilibili.com/video/av*
// @match *://bangumi.bilibili.com/anime/*/play*
// @match *://www.bilibili.com/bangumi/play/ep*
// @match *://www.bilibili.com/bangumi/play/ss*
// @match *://www.bilibili.com/watchlater/
// @version 1.14
// @author qli5
// @copyright qli5, 2014+, 田生, grepmusic, zheng qian, ryiwamoto
// @license Mozilla Public License 2.0; http://www.mozilla.org/MPL/2.0/
// @grant none
// @run-at document-start
// ==/UserScript==
/***
*
* @author qli5 <goodlq11[at](163|gmail).com>
*
* BiliTwin consists of two parts - BiliMonkey and BiliPolyfill.
* They are bundled because I am too lazy to write two user interfaces.
*
* So what is the difference between BiliMonkey and BiliPolyfill?
*
* BiliMonkey deals with network. It is a (naIve) Service Worker.
* This is also why it uses IndexedDB instead of localStorage.
* BiliPolyfill deals with experience. It is more a "user script".
* Everything it can do can be done by hand.
*
* BiliPolyfill will be pointless in the long run - I believe bilibili
* will finally provide these functions themselves.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an “as is” basis,
* without warranty of any kind, either expressed, implied, or statutory,
* including, without limitation, warranties that the Covered Software
* is free of defects, merchantable, fit for a particular purpose or
* non-infringing. The entire risk as to the quality and performance of
* the Covered Software is with You. Should any Covered Software prove
* defective in any respect, You (not any Contributor) assume the cost
* of any necessary servicing, repair, or correction. This disclaimer
* of warranty constitutes an essential part of this License. No use of
* any Covered Software is authorized under this License except under
* this disclaimer.
*
* Under no circumstances and under no legal theory, whether tort
* (including negligence), contract, or otherwise, shall any Contributor,
* or anyone who distributes Covered Software as permitted above, be
* liable to You for any direct, indirect, special, incidental, or
* consequential damages of any character including, without limitation,
* damages for lost profits, loss of goodwill, work stoppage, computer
* failure or malfunction, or any and all other commercial damages or
* losses, even if such party shall have been informed of the possibility
* of such damages. This limitation of liability shall not apply to
* liability for death or personal injury resulting from such party’s
* negligence to the extent applicable law prohibits such limitation.
* Some jurisdictions do not allow the exclusion or limitation of
* incidental or consequential damages, so this exclusion and limitation
* may not apply to You.
*/
/***
* This is a bundled code. While it is not uglified, it may still be too
* complex for reviewing. Please refer to
* https://github.com/liqi0816/bilitwin/
* for source code.
*/
if (document.readyState == 'loading') {
var h = () => {
load();
document.removeEventListener('DOMContentLoaded', h);
};
document.addEventListener('DOMContentLoaded', h);
}
else {
load();
}
function load() {
if (typeof _babelPolyfill === 'undefined') {
new Promise(function (resolve) {
var req = new XMLHttpRequest();
req.onload = function () { resolve(req.responseText); };
req.open('get', 'https://cdn.bootcss.com/babel-polyfill/7.0.0-beta.42/polyfill.min.js');
req.send();
}).then(function (script) {
top.eval(script);
}).then(function () {
script();
});
}
else {
script();
}
}
function script() {
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _extendableBuiltin(cls) {
function ExtendableBuiltin() {
var instance = Reflect.construct(cls, Array.from(arguments));
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true
}
});
if (Object.setPrototypeOf) {
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}
return ExtendableBuiltin;
}
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// ==UserScript==
// @name bilibili merged flv+mp4+ass+enhance
// @namespace http://qli5.tk/
// @homepageURL https://github.com/liqi0816/bilitwin/
// @description bilibili/哔哩哔哩:超清FLV下载,FLV合并,原生MP4下载,弹幕ASS下载,MKV打包,播放体验增强,原生appsecret,不借助其他网站
// @match *://www.bilibili.com/video/av*
// @match *://bangumi.bilibili.com/anime/*/play*
// @match *://www.bilibili.com/bangumi/play/ep*
// @match *://www.bilibili.com/bangumi/play/ss*
// @match *://www.bilibili.com/watchlater/
// @version 1.14
// @author qli5
// @copyright qli5, 2014+, 田生, grepmusic, zheng qian, ryiwamoto
// @license Mozilla Public License 2.0; http://www.mozilla.org/MPL/2.0/
// @grant none
// @run-at document-start
// ==/UserScript==
/***
*
* @author qli5 <goodlq11[at](163|gmail).com>
*
* BiliTwin consists of two parts - BiliMonkey and BiliPolyfill.
* They are bundled because I am too lazy to write two user interfaces.
*
* So what is the difference between BiliMonkey and BiliPolyfill?
*
* BiliMonkey deals with network. It is a (naIve) Service Worker.
* This is also why it uses IndexedDB instead of localStorage.
* BiliPolyfill deals with experience. It is more a "user script".
* Everything it can do can be done by hand.
*
* BiliPolyfill will be pointless in the long run - I believe bilibili
* will finally provide these functions themselves.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Covered Software is provided under this License on an “as is” basis,
* without warranty of any kind, either expressed, implied, or statutory,
* including, without limitation, warranties that the Covered Software
* is free of defects, merchantable, fit for a particular purpose or
* non-infringing. The entire risk as to the quality and performance of
* the Covered Software is with You. Should any Covered Software prove
* defective in any respect, You (not any Contributor) assume the cost
* of any necessary servicing, repair, or correction. This disclaimer
* of warranty constitutes an essential part of this License. No use of
* any Covered Software is authorized under this License except under
* this disclaimer.
*
* Under no circumstances and under no legal theory, whether tort
* (including negligence), contract, or otherwise, shall any Contributor,
* or anyone who distributes Covered Software as permitted above, be
* liable to You for any direct, indirect, special, incidental, or
* consequential damages of any character including, without limitation,
* damages for lost profits, loss of goodwill, work stoppage, computer
* failure or malfunction, or any and all other commercial damages or
* losses, even if such party shall have been informed of the possibility
* of such damages. This limitation of liability shall not apply to
* liability for death or personal injury resulting from such party’s
* negligence to the extent applicable law prohibits such limitation.
* Some jurisdictions do not allow the exclusion or limitation of
* incidental or consequential damages, so this exclusion and limitation
* may not apply to You.
*/
/***
* This is a bundled code. While it is not uglified, it may still be too
* complex for reviewing. Please refer to
* https://github.com/liqi0816/bilitwin/
* for source code.
*/
/***
* Copyright (C) 2018 Qli5. All Rights Reserved.
*
* @author qli5 <goodlq11[at](163|gmail).com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/**
* Basically a Promise that exposes its resolve and reject callbacks
*/
var AsyncContainer = function () {
/***
* The thing is, if we cannot cancel a promise, we should at least be able to
* explicitly mark a promise as garbage collectible.
*
* Yes, this is something like cancelable Promise. But I insist they are different.
*/
function AsyncContainer(callback) {
var _this = this;
_classCallCheck(this, AsyncContainer);
// 1. primary promise
this.primaryPromise = new Promise(function (s, j) {
_this.resolve = function (arg) {
s(arg);return arg;
};
_this.reject = function (arg) {
j(arg);return arg;
};
});
// 2. hang promise
this.hangReturn = Symbol();
this.hangPromise = new Promise(function (s) {
return _this.hang = function () {
return s(_this.hangReturn);
};
});
this.destroiedThen = this.hangPromise.then.bind(this.hangPromise);
this.primaryPromise.then(function () {
return _this.state = 'fulfilled';
});
this.primaryPromise.catch(function () {
return _this.state = 'rejected';
});
this.hangPromise.then(function () {
return _this.state = 'hanged';
});
// 4. race
this.promise = Promise.race([this.primaryPromise, this.hangPromise]).then(function (s) {
return s == _this.hangReturn ? new Promise(function () {}) : s;
});
// 5. inherit
this.then = this.promise.then.bind(this.promise);
this.catch = this.promise.catch.bind(this.promise);
this.finally = this.promise.finally.bind(this.promise);
// 6. optional callback
if (typeof callback == 'function') callback(this.resolve, this.reject);
}
/***
* Memory leak notice:
*
* The V8 implementation of Promise requires
* 1. the resolve handler of a Promise
* 2. the reject handler of a Promise
* 3. !! the Promise object itself !!
* to be garbage collectible to correctly free Promise runtime contextes
*
* This piece of code will work
* void (async () => {
* const buf = new Uint8Array(1024 * 1024 * 1024);
* for (let i = 0; i < buf.length; i++) buf[i] = i;
* await new Promise(() => { });
* return buf;
* })();
* if (typeof gc == 'function') gc();
*
* This piece of code will cause a Promise context mem leak
* const deadPromise = new Promise(() => { });
* void (async () => {
* const buf = new Uint8Array(1024 * 1024 * 1024);
* for (let i = 0; i < buf.length; i++) buf[i] = i;
* await deadPromise;
* return buf;
* })();
* if (typeof gc == 'function') gc();
*
* In other words, do NOT directly inherit from promise. You will need to
* dereference it on destroying.
*/
_createClass(AsyncContainer, [{
key: 'destroy',
value: function destroy() {
this.hang();
this.resolve = function () {};
this.reject = this.resolve;
this.hang = this.resolve;
this.primaryPromise = null;
this.hangPromise = null;
this.promise = null;
this.then = this.resolve;
this.catch = this.resolve;
this.finally = this.resolve;
this.destroiedThen = function (f) {
return f();
};
/***
* For ease of debug, do not dereference hangReturn
*
* If run from console, mysteriously this tiny symbol will help correct gc
* before a console.clear
*/
//this.hangReturn = null;
}
}], [{
key: '_UNIT_TEST',
value: function _UNIT_TEST() {
var foo = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var buf, i, ac;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
buf = new Uint8Array(600 * 1024 * 1024);
for (i = 0; i < buf.length; i++) {
buf[i] = i;
}ac = new AsyncContainer();
ac.destroiedThen(function () {
return console.log('asyncContainer destroied');
});
containers.push(ac);
_context.next = 7;
return ac;
case 7:
return _context.abrupt('return', buf);
case 8:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
return function foo() {
return _ref.apply(this, arguments);
};
}();
var containers = [];
var foos = [foo(), foo(), foo()];
containers.forEach(function (e) {
return e.destroy();
});
console.warn('Check your RAM usage. I allocated 1.8GB in three dead-end promises.');
return [foos, containers];
}
}]);
return AsyncContainer;
}();
/***
* Copyright (C) 2018 Qli5. All Rights Reserved.
*
* @author qli5 <goodlq11[at](163|gmail).com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/**
* Provides common util for all bilibili user scripts
*/
var BiliUserJS = function () {
function BiliUserJS() {
_classCallCheck(this, BiliUserJS);
}
_createClass(BiliUserJS, null, [{
key: 'getIframeWin',
value: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (!document.querySelector('#bofqi > iframe').contentDocument.getElementById('bilibiliPlayer')) {
_context2.next = 4;
break;
}
return _context2.abrupt('return', document.querySelector('#bofqi > iframe').contentWindow);
case 4:
return _context2.abrupt('return', new Promise(function (resolve) {
document.querySelector('#bofqi > iframe').addEventListener('load', function () {
resolve(document.querySelector('#bofqi > iframe').contentWindow);
}, { once: true });
}));
case 5:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function getIframeWin() {
return _ref2.apply(this, arguments);
}
return getIframeWin;
}()
}, {
key: 'getPlayerWin',
value: function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!location.href.includes('/watchlater/#/list')) {
_context3.next = 3;
break;
}
_context3.next = 3;
return new Promise(function (resolve) {
window.addEventListener('hashchange', function () {
return resolve(location.href);
}, { once: true });
});
case 3:
if (!location.href.includes('/watchlater/#/')) {
_context3.next = 7;
break;
}
if (document.getElementById('bofqi')) {
_context3.next = 7;
break;
}
_context3.next = 7;
return new Promise(function (resolve) {
var observer = new MutationObserver(function () {
if (document.getElementById('bofqi')) {
resolve(document.getElementById('bofqi'));
observer.disconnect();
}
});
observer.observe(document, { childList: true, subtree: true });
});
case 7:
if (!document.getElementById('bilibiliPlayer')) {
_context3.next = 11;
break;
}
return _context3.abrupt('return', window);
case 11:
if (!document.querySelector('#bofqi > iframe')) {
_context3.next = 15;
break;
}
return _context3.abrupt('return', BiliUserJS.getIframeWin());
case 15:
if (!document.querySelector('#bofqi > object')) {
_context3.next = 19;
break;
}
throw 'Need H5 Player';
case 19:
return _context3.abrupt('return', new Promise(function (resolve) {
var observer = new MutationObserver(function () {
if (document.getElementById('bilibiliPlayer')) {
observer.disconnect();
resolve(window);
} else if (document.querySelector('#bofqi > iframe')) {
observer.disconnect();
resolve(BiliUserJS.getIframeWin());
} else if (document.querySelector('#bofqi > object')) {
observer.disconnect();
throw 'Need H5 Player';
}
});
observer.observe(document.getElementById('bofqi'), { childList: true });
}));
case 20:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function getPlayerWin() {
return _ref3.apply(this, arguments);
}
return getPlayerWin;
}()
}, {
key: 'tryGetPlayerWinSync',
value: function tryGetPlayerWinSync() {
if (document.getElementById('bilibiliPlayer')) {
return window;
} else if (document.querySelector('#bofqi > object')) {
throw 'Need H5 Player';
}
}
}, {
key: 'getCidRefreshPromise',
value: function getCidRefreshPromise(playerWin) {
/***********
* !!!Race condition!!!
* We must finish everything within one microtask queue!
*
* bilibili script:
* videoElement.remove() -> setTimeout(0) -> [[microtask]] -> load playurl
* \- synchronous macrotask -/ || \- synchronous
* ||
* the only position to inject monkey.sniffDefaultFormat
*/
var cidRefresh = new AsyncContainer();
// 1. no active video element in document => cid refresh
var observer = new MutationObserver(function () {
if (!playerWin.document.getElementsByTagName('video')[0]) {
observer.disconnect();
cidRefresh.resolve();
}
});
observer.observe(playerWin.document.getElementsByClassName('bilibili-player-video')[0], { childList: true });
// 2. playerWin unload => cid refresh
playerWin.addEventListener('unload', function () {
return Promise.resolve().then(function () {
return cidRefresh.resolve();
});
});
return cidRefresh;
}
}, {
key: 'domContentLoadedThen',
value: function () {
var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(func) {
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
if (!(document.readyState == 'loading')) {
_context4.next = 4;
break;
}
return _context4.abrupt('return', new Promise(function (resolve) {
document.addEventListener('DOMContentLoaded', function () {
return resolve(func());
}, { once: true });
}));
case 4:
return _context4.abrupt('return', func());
case 5:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function domContentLoadedThen(_x) {
return _ref4.apply(this, arguments);
}
return domContentLoadedThen;
}()
}]);
return BiliUserJS;
}();
/***
* Copyright (C) 2018 Qli5. All Rights Reserved.
*
* @author qli5 <goodlq11[at](163|gmail).com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/**
* A promisified indexedDB with large file(>100MB) support
*/
var CacheDB = function () {
function CacheDB() {
var dbName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'biliMonkey';
var osName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'flv';
var keyPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'name';
var maxItemSize = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 100 * 1024 * 1024;
_classCallCheck(this, CacheDB);
// Neither Chrome or Firefox can handle item size > 100M
this.dbName = dbName;
this.osName = osName;
this.keyPath = keyPath;
this.maxItemSize = maxItemSize;
this.db = null;
}
_createClass(CacheDB, [{
key: 'getDB',
value: function () {
var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
var _this2 = this;
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
if (!this.db) {
_context5.next = 2;
break;
}
return _context5.abrupt('return', this.db);
case 2:
this.db = new Promise(function (resolve, reject) {
var openRequest = indexedDB.open(_this2.dbName);
openRequest.onupgradeneeded = function (e) {
var db = e.target.result;
if (!db.objectStoreNames.contains(_this2.osName)) {
db.createObjectStore(_this2.osName, { keyPath: _this2.keyPath });
}
};
openRequest.onsuccess = function (e) {
return resolve(_this2.db = e.target.result);
};
openRequest.onerror = reject;
});
return _context5.abrupt('return', this.db);
case 4:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function getDB() {
return _ref5.apply(this, arguments);
}
return getDB;
}()
}, {
key: 'addData',
value: function () {
var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(item) {
var _this3 = this;
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : item.name;
var data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : item.data || item;
var itemChunks, numChunks, i, reqCascade;
return regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
if (!(!data instanceof Blob)) {
_context7.next = 2;
break;
}
throw 'CacheDB: data must be a Blob';
case 2:
itemChunks = [];
numChunks = Math.ceil(data.size / this.maxItemSize);
for (i = 0; i < numChunks; i++) {
itemChunks.push({
name: name + '/part_' + i,
numChunks: numChunks,
data: data.slice(i * this.maxItemSize, (i + 1) * this.maxItemSize)
});
}
reqCascade = new Promise(function () {
var _ref7 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(resolve, reject) {
var db, objectStore, onsuccess;
return regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return _this3.getDB();
case 2:
db = _context6.sent;
objectStore = db.transaction([_this3.osName], 'readwrite').objectStore(_this3.osName);
onsuccess = function onsuccess(e) {
var chunk = itemChunks.pop();
if (!chunk) return resolve(e);
var req = objectStore.add(chunk);
req.onerror = reject;
req.onsuccess = onsuccess;
};
onsuccess();
case 6:
case 'end':
return _context6.stop();
}
}
}, _callee6, _this3);
}));
return function (_x9, _x10) {
return _ref7.apply(this, arguments);
};
}());
return _context7.abrupt('return', reqCascade);
case 7:
case 'end':
return _context7.stop();
}
}
}, _callee7, this);
}));
function addData(_x8) {
return _ref6.apply(this, arguments);
}
return addData;
}()
}, {
key: 'putData',
value: function () {
var _ref8 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9(item) {
var _this4 = this;
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : item.name;
var data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : item.data || item;
var itemChunks, numChunks, i, reqCascade;
return regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
if (!(!data instanceof Blob)) {
_context9.next = 2;
break;
}
throw 'CacheDB: data must be a Blob';
case 2:
itemChunks = [];
numChunks = Math.ceil(data.size / this.maxItemSize);
for (i = 0; i < numChunks; i++) {
itemChunks.push({
name: name + '/part_' + i,
numChunks: numChunks,
data: data.slice(i * this.maxItemSize, (i + 1) * this.maxItemSize)
});
}
reqCascade = new Promise(function () {
var _ref9 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(resolve, reject) {
var db, objectStore, onsuccess;
return regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return _this4.getDB();
case 2:
db = _context8.sent;
objectStore = db.transaction([_this4.osName], 'readwrite').objectStore(_this4.osName);
onsuccess = function onsuccess(e) {
var chunk = itemChunks.pop();
if (!chunk) return resolve(e);
var req = objectStore.put(chunk);
req.onerror = reject;
req.onsuccess = onsuccess;
};
onsuccess();
case 6:
case 'end':
return _context8.stop();
}
}
}, _callee8, _this4);
}));
return function (_x14, _x15) {
return _ref9.apply(this, arguments);
};
}());
return _context9.abrupt('return', reqCascade);
case 7:
case 'end':
return _context9.stop();
}
}
}, _callee9, this);
}));
function putData(_x13) {
return _ref8.apply(this, arguments);
}
return putData;
}()
}, {
key: 'getData',
value: function () {
var _ref10 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11(name) {
var _this5 = this;
var reqCascade, dataChunks;
return regeneratorRuntime.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
reqCascade = new Promise(function () {
var _ref11 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10(resolve, reject) {
var dataChunks, db, objectStore, probe;
return regeneratorRuntime.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
dataChunks = [];
_context10.next = 3;
return _this5.getDB();
case 3:
db = _context10.sent;
objectStore = db.transaction([_this5.osName], 'readwrite').objectStore(_this5.osName);
probe = objectStore.get(name + '/part_0');
probe.onerror = reject;
probe.onsuccess = function (e) {
// 1. Probe fails => key does not exist
if (!probe.result) return resolve(null);
// 2. How many chunks to retrieve?
var numChunks = probe.result.numChunks;
// 3. Cascade on the remaining chunks
var onsuccess = function onsuccess(e) {
dataChunks.push(e.target.result.data);
if (dataChunks.length == numChunks) return resolve(dataChunks);
var req = objectStore.get(name + '/part_' + dataChunks.length);
req.onerror = reject;
req.onsuccess = onsuccess;
};
onsuccess(e);
};
case 8:
case 'end':
return _context10.stop();
}
}
}, _callee10, _this5);
}));
return function (_x17, _x18) {
return _ref11.apply(this, arguments);
};
}());
_context11.next = 3;
return reqCascade;
case 3:
dataChunks = _context11.sent;
return _context11.abrupt('return', dataChunks ? { name: name, data: new Blob(dataChunks) } : null);
case 5:
case 'end':
return _context11.stop();
}
}
}, _callee11, this);
}));
function getData(_x16) {
return _ref10.apply(this, arguments);
}
return getData;
}()
}, {
key: 'deleteData',
value: function () {
var _ref12 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee13(name) {
var _this6 = this;
var reqCascade;
return regeneratorRuntime.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
reqCascade = new Promise(function () {
var _ref13 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee12(resolve, reject) {
var currentChunkNum, db, objectStore, probe;
return regeneratorRuntime.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
currentChunkNum = 0;
_context12.next = 3;
return _this6.getDB();
case 3:
db = _context12.sent;
objectStore = db.transaction([_this6.osName], 'readwrite').objectStore(_this6.osName);
probe = objectStore.get(name + '/part_0');
probe.onerror = reject;
probe.onsuccess = function (e) {
// 1. Probe fails => key does not exist
if (!probe.result) return resolve(null);
// 2. How many chunks to delete?
var numChunks = probe.result.numChunks;
// 3. Cascade on the remaining chunks