aboutsummaryrefslogtreecommitdiff
path: root/viewtube.js
blob: c4180f92a3f91d9702976e395abd2e34b0c8c4eb (plain)
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
/*
  Copyright (C) 2010 - 2017 Sebastian Luncan
  Copyright (C) 2017 Arun Isaac <arunisaac@systemreboot.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.

  Website: http://isebaro.com/viewtube
  Contact: http://isebaro.com/contact
*/

// ==========Variables========== //

(function() {
    // Userscript
    var userscript = 'ViewTube';

    // Page
    var page = {win: window, doc: document, body: document.body, url: window.location.href, title: document.title, site: window.location.hostname.match(/([^.]+)\.[^.]+$/)[1]};

    // Player
    var player = {};
    var option = {'definition': 'HD', 'container': 'MP4', 'dash': false, 'direct': false, 'widesize': false, 'fullsize': false};
    var mimetypes = {
	'MPEG': 'video/mpeg',
	'MP4': 'video/mp4',
	'WebM': 'video/webm',
	'FLV': 'video/x-flv',
	'MOV': 'video/quicktime',
	'M4V': 'video/x-m4v',
	'AVI': 'video/x-msvideo',
	'3GP': 'video/3gpp',
    };
    var sources = {};

    // Links
    var website = 'https://git.systemreboot.net/youtube-noscript-shim/about';
    var contact = 'mailto:arunisaac@systemreboot.net';

    // ==========Functions========== //

    function createMyElement(type, content, event, action, target) {
	function styleButton (button, state) {
	    styleMyElement(player[button], state ? {color: '#008080', textShadow: '0px 1px 1px #CCCCCC'} : {color: '#CCCCCC', textShadow: '0px 0px 0px'});
	}
	var obj = page.doc.createElement(type);
	if (content) {
	    switch (type) {
	    case 'div':
		obj.innerHTML = content;
		break;
	    case 'img':
		obj.src = content;
		break;
	    case 'option':
		obj.value = content;
		obj.innerHTML = content;
		break;
	    case 'video':
		obj.src = content;
		obj.controls = 'controls';
		obj.autoplay = 'autoplay';
		obj.volume = 0.8;
		obj.innerHTML = '<br><br>The video should be loading. If it doesn\'t load, make sure your browser supports HTML5\'s Video and this video codec. If you think it\'s a script issue, please report it <a href="' + contact + '" style="color:#00892C">here</a>.';
		break;
	    }
	}
	if (type == 'video') {
	    obj.type = mimetypes[player['videoPlay'].replace(/.*\s/, '')];
	    obj.id = 'vtVideo';
	}
	if (event == 'change') {
	    if (target == 'video') {
		obj.addEventListener('change', function() {
		    player['videoPlay'] = this.value;
		    playMyVideo();
		}, false);
	    }
	}
	else if (event == 'click') {
	    obj.addEventListener('click', function() {
		switch (action) {
		case 'close':
		    page.body.removeChild(target);
		    break;
		case 'dash':
		    option['dash'] = !option['dash'];
		    styleButton('buttonDASH', option['dash'])
		    setMyOptions('dash', option['dash']);
		    break;
		case 'widesize':
		    option['widesize'] = !option['widesize'];
		    setMyOptions('widesize', option['widesize']);
		    resizeMyPlayer('widesize');
		    break;
		case 'fullsize':
		    option['fullsize'] = !option['fullsize'];
		    setMyOptions('fullsize', option['fullsize']);
		    resizeMyPlayer('fullsize');
		    break;
		}
	    }, false);
	}
	return obj;
    }

    function modifyMyElement(obj, type, content, clear, hide) {
	if (content) {
	    if (type == 'div') obj.innerHTML = content;
	    else if (type == 'option') {
		obj.value = content;
		obj.innerHTML = content;
	    }
	    else if (type == 'object') obj.data = content;
	    else if (type == 'img' || type == 'video' || type == 'embed') obj.src = content;
	}
	if (clear) {
	    if (obj.hasChildNodes()) {
		while (obj.childNodes.length >= 1) {
		    obj.removeChild(obj.firstChild);
		}
	    }
	}
	if (hide) {
	    for (var i = 0; i < obj.children.length; i++) {
		styleMyElement(obj.children[i], {display: 'none'});
	    }
	}
    }

    function styleMyElement(obj, styles) {
	for (var property in styles) {
	    if (styles.hasOwnProperty(property)) obj.style[property] = styles[property];
	}
    }

    function createMyPlayer() {
	/* Get My Options */
	function onGot(item) {
	    Object.assign(option, item);

	    /* Player Settings */
	    player['panelHeight'] = 18;
	    player['panelPadding'] = 2;

	    /* The Panel */
	    var panelWidth = player['playerWidth'] - player['panelPadding'] * 2;
	    player['playerPanel'] = createMyElement('div', '', '', '', '');
	    styleMyElement(player['playerPanel'], {width: panelWidth + 'px', height: player['panelHeight'] + 'px', padding: player['panelPadding'] + 'px', backgroundColor: 'inherit', textAlign: 'center'});
	    player['playerWindow'].appendChild(player['playerPanel']);

	    /* Panel Items */
	    var panelItemBorder = 1;
	    var panelItemHeight = player['panelHeight'] - panelItemBorder * 2;

	    /* Panel Video Menu */
	    player['videoMenu'] = createMyElement('select', '', 'change', '', 'video');
	    player['videoMenu'].title = '{Videos: select the video format for playback}';
	    styleMyElement(player['videoMenu'], {width: '200px', height: panelItemHeight + 'px', border: '1px solid transparent', padding: '0px', display: 'inline', backgroundColor: 'inherit', color: '#336699', fontSize: '12px', textShadow: '0px 1px 1px #CCCCCC', verticalAlign: 'baseline', cursor: 'pointer'});
	    player['playerPanel'].appendChild(player['videoMenu']);
	    for (var videoCode in player['videoList']) {
		player['videoItem'] = createMyElement('option', videoCode, '', '', '');
		styleMyElement(player['videoItem'], {padding: '0px', display: 'block', color: '#336699', fontSize: '12px', textShadow: '0px 1px 1px #CCCCCC', cursor: 'pointer'});
		if (videoCode.indexOf('Video') != -1 || videoCode.indexOf('Audio') != -1) styleMyElement(player['videoItem'], {color: '#8F6B32'});
		if (player['videoList'][videoCode] == 'DASH') styleMyElement(player['videoItem'], {color: '#CF4913'});
		if (player['videoList'][videoCode] != 'DASH' || option['dash'])
		    player['videoMenu'].appendChild(player['videoItem']);
		else delete player['videoList'][videoCode];
		if (videoCode == 'Direct Video Link') styleMyElement(player['videoItem'], {color: '#00C0C0'});
	    }

	    /* Panel DASH Button */
	    player['buttonDASH'] = createMyElement('div', 'MD', 'click', 'dash', '');
	    player['buttonDASH'].title = '{MPEG-DASH: click to enable/disable DASH playback using the HTML5 video player (experimental)}';
	    styleMyElement(player['buttonDASH'], {height: panelItemHeight + 'px', border: '1px solid #CCCCCC', borderRadius: '3px', padding: '0px 5px', display: 'inline', color: '#CCCCCC', fontSize: '12px', cursor: 'pointer'});
	    if (option['dash']) styleMyElement(player['buttonDASH'], {color: '#008080', textShadow: '0px 1px 1px #CCCCCC'});
	    player['playerPanel'].appendChild(player['buttonDASH']);

	    /* Panel Widesize Button */
	    player['buttonWidesize'] = createMyElement('div', option['widesize'] ? '&lt;' : '&gt;', 'click', 'widesize', '');
	    player['buttonWidesize'].title = '{Widesize: click to enter player widesize or return to normal size}';
	    styleMyElement(player['buttonWidesize'], {height: panelItemHeight + 'px', border: '1px solid #CCCCCC', borderRadius: '3px', padding: '0px 5px', display: 'inline', color: '#C05800', fontSize: '12px', textShadow: '1px 1px 2px #CCCCCC', cursor: 'pointer'});
	    player['playerPanel'].appendChild(player['buttonWidesize']);

	    /* Panel Fullsize Button */
	    player['buttonFullsize'] = createMyElement('div', option['fullsize'] ? '-' : '+', 'click', 'fullsize', '');
	    player['buttonFullsize'].title = '{Fullsize: click to enter player fullsize or return to normal size}';
	    styleMyElement(player['buttonFullsize'], {height: panelItemHeight + 'px', border: '1px solid #CCCCCC', borderRadius: '3px', padding: '0px 5px', display: 'inline', color: '#C05800', fontSize: '12px', textShadow: '1px 1px 2px #CCCCCC', cursor: 'pointer'});
	    player['playerPanel'].appendChild(player['buttonFullsize']);

	    /* The Content */
	    player['contentWidth'] = player['playerWidth'];
	    player['contentHeight'] = player['playerHeight'] - player['panelHeight'] - player['panelPadding'] * 2;
	    player['playerContent'] = createMyElement('div', '', '', '', '');
	    styleMyElement(player['playerContent'], {width: player['contentWidth'] + 'px', height: player['contentHeight'] + 'px', position: 'relative', color: '#AD0000', backgroundColor: '#000000', fontSize: '14px', fontWeight: 'bold', textAlign: 'center'});
	    player['playerWindow'].appendChild(player['playerContent']);

	    /* Resize My Player */
	    if (option['widesize']) resizeMyPlayer('widesize');
	    if (option['fullsize']) resizeMyPlayer('fullsize');

	    /* Play My Video */
	    playMyVideo();
	}
	promise = browser.storage.local.get(Object.keys(option));
	promise.then(onGot, onGot);
    }

    function playDASHwithHTML5() {
	if (player['videoPlay'].indexOf('MP4') != -1) {
	    player['contentVideo'] = createMyElement('video', player['videoList'][player['videoPlay'].replace(/MP4/, 'Video MP4')], '', '', '');
	    if (player['videoList']['High Bitrate Audio Opus']) {
		player['contentAudio'] = createMyElement('video', player['videoList']['High Bitrate Audio Opus'], '', '', '');
	    }
	    else if (player['videoList']['Medium Bitrate Audio Opus']) {
		player['contentAudio'] = createMyElement('video', player['videoList']['Medium Bitrate Audio Opus'], '', '', '');
	    }
	    else {
		player['contentAudio'] = createMyElement('video', player['videoList']['Medium Bitrate Audio MP4'], '', '', '');
	    }
	}
	else {
	    player['contentVideo'] = createMyElement('video', player['videoList'][player['videoPlay'].replace(/WebM/, 'Video WebM')], '', '', '');
	    if (player['videoList']['High Bitrate Audio Opus']) {
		player['contentAudio'] = createMyElement('video', player['videoList']['High Bitrate Audio Opus'], '', '', '');
	    }
	    else if (player['videoList']['Medium Bitrate Audio Opus']) {
		player['contentAudio'] = createMyElement('video', player['videoList']['Medium Bitrate Audio Opus'], '', '', '');
	    }
	    else {
		player['contentAudio'] = createMyElement('video', player['videoList']['Medium Bitrate Audio WebM'], '', '', '');
	    }
	}
	player['contentAudio'].pause();
	player['contentVideo'].addEventListener('play', function() {
	    player['contentAudio'].play();
	}, false);
	player['contentVideo'].addEventListener('pause', function() {
	    player['contentAudio'].pause();
	}, false);
	player['contentVideo'].addEventListener('ended', function() {
	    player['contentVideo'].pause();
	    player['contentAudio'].pause();
	}, false);
	player['contentVideo'].addEventListener('timeupdate', function() {
	    if (player['contentAudio'].paused && !player['contentVideo'].paused) {
		player['contentAudio'].play();
	    }
	    if (Math.abs(player['contentVideo'].currentTime - player['contentAudio'].currentTime) >= 0.30) {
		player['contentAudio'].currentTime = player['contentVideo'].currentTime;
	    }
	}, false);
	styleMyElement(player['contentAudio'], {display: 'none'});
	player['contentVideo'].appendChild(player['contentAudio']);
    }

    function playMyVideo() {
	modifyMyElement(player['playerContent'], 'div', '', true);
	if (player['videoList'][player['videoPlay']] == 'DASH')
	    playDASHwithHTML5();
	else
	    player['contentVideo'] = createMyElement('video', player['videoList'][player['videoPlay']], '', '', '');
	player['contentVideo'].width = player['contentWidth'];
	player['contentVideo'].height = player['contentHeight'];
	player['contentVideo'].poster = player['videoThumb'];
	styleMyElement(player['contentVideo'], {position: 'relative', width: player['contentWidth'] + 'px', height: player['contentHeight'] + 'px'});
	player['playerContent'].appendChild(player['contentVideo']);
    }

    function resizeMyPlayer(size) {
	switch (size) {
	case 'widesize':
	    if (option['widesize']) {
		if (player['buttonWidesize']) modifyMyElement(player['buttonWidesize'], 'div', '&lt;', false);
		var playerWidth = player['playerWideWidth'];
		var playerHeight= player['playerWideHeight'];
		var sidebarMargin = player['sidebarMarginWide'];
	    }
	    else {
		if (player['buttonWidesize']) modifyMyElement(player['buttonWidesize'], 'div', '&gt;', false);
		var playerWidth = player['playerWidth'];
		var playerHeight= player['playerHeight'];
		var sidebarMargin = player['sidebarMarginNormal'];
	    }
	    break;
	case 'fullsize':
	    if (option['fullsize']) {
		var playerPosition = 'fixed';
		var playerWidth = page.win.innerWidth || page.doc.documentElement.clientWidth;
		var playerHeight = page.win.innerHeight || page.doc.documentElement.clientHeight;
		var playerIndex = '9999999999';
		if (!player['isFullsize']) {
		    styleMyElement(player['buttonWidesize'], {display: 'none'});
		    modifyMyElement(player['buttonFullsize'], 'div', '-', false);
		    page.body.appendChild(player['playerWindow']);
		    styleMyElement(page.body, {overflow: 'hidden'});
		    styleMyElement(page.body.parentNode, {overflow: 'hidden'});
		    if (!player['resizeListener']) player['resizeListener'] = function() {resizeMyPlayer('fullsize')};
		    page.win.addEventListener('resize', player['resizeListener'], false);
		    player['isFullsize'] = true;
		}
	    }
	    else {
		var playerPosition = 'relative';
		var playerWidth = (option['widesize']) ? player['playerWideWidth'] : player['playerWidth'];
		var playerHeight = (option['widesize']) ? player['playerWideHeight'] : player['playerHeight'];
		var playerIndex = 'auto';
		styleMyElement(player['buttonWidesize'], {display: 'inline'});
		modifyMyElement(player['buttonFullsize'], 'div', '+', false);
		player['playerSocket'].appendChild(player['playerWindow']);
		styleMyElement(page.body, {overflow: 'auto'});
		styleMyElement(page.body.parentNode, {overflow: 'auto'});
		page.win.removeEventListener('resize', player['resizeListener'], false);
		player['isFullsize'] = false;
	    }
	    break;
	}

	/* Resize The Player */
	if (size == 'widesize') {
	    if (player['sidebarWindow']) styleMyElement(player['sidebarWindow'], {marginTop: sidebarMargin + 'px'});
	    styleMyElement(player['playerWindow'], {width: playerWidth + 'px', height: playerHeight + 'px'});
	}
	else styleMyElement(player['playerWindow'], {position: playerPosition, top: '0px', left: '0px', width: playerWidth + 'px', height: playerHeight + 'px', zIndex: playerIndex});

	/* Resize The Panel */
	var panelWidth = playerWidth - player['panelPadding'] * 2;
	styleMyElement(player['playerPanel'], {width: panelWidth + 'px'});

	/* Resize The Content */
	player['contentWidth'] = playerWidth;
	player['contentHeight'] = playerHeight - player['panelHeight'] - player['panelPadding'] * 2;
	styleMyElement(player['playerContent'], {width: player['contentWidth'] + 'px', height: player['contentHeight'] + 'px'});
	player['contentVideo'].width = player['contentWidth'];
	player['contentVideo'].height = player['contentHeight'];
	styleMyElement(player['contentVideo'], {width: player['contentWidth'] + 'px', height: player['contentHeight'] + 'px'});
    }

    function cleanMyContent(content, unesc) {
	var myNewContent = content;
	if (unesc) myNewContent = unescape(myNewContent);
	myNewContent = myNewContent.replace(/\\u0025/g,'%');
	myNewContent = myNewContent.replace(/\\u0026/g,'&');
	myNewContent = myNewContent.replace(/\\/g,'');
	myNewContent = myNewContent.replace(/\n/g,'');
	return myNewContent;
    }

    function getMyContent(url, pattern, clean) {
	var myPageContent, myVideosParse, myVideosContent;
	var getMethod = (url != page.url) ? 'XHR' : 'DOM';
	if (!sources[url]) sources[url] = {};
	if (getMethod == 'DOM') {
	    if (!sources[url]['DOM'])
		sources[url]['DOM'] = document.querySelector("html").innerHTML;
	    myPageContent = sources[url]['DOM'];
	    if (clean) myPageContent = cleanMyContent(myPageContent, true);
	    myVideosParse = myPageContent.match(pattern);
	    myVideosContent = (myVideosParse) ? myVideosParse[1] : null;
	    if (myVideosContent) return myVideosContent;
	    else getMethod = 'XHR';
	}
	if (getMethod == 'XHR') {
	    if (!sources[url]['XHR']) sources[url]['XHR'] = {};
	    if ((pattern == 'XML' && !sources[url]['XHR']['XML']) || (pattern != 'XML' && !sources[url]['XHR']['TEXT'])) {
		var xmlHTTP = new XMLHttpRequest();
		xmlHTTP.open('GET', url, false);
		xmlHTTP.send();
		if (pattern == 'XML') sources[url]['XHR']['XML'] = xmlHTTP.responseXML;
		else sources[url]['XHR']['TEXT'] = xmlHTTP.responseText;
	    }
	    if (pattern == 'XML') {
		myVideosContent = sources[url]['XHR']['XML'];
	    }
	    else if (pattern == 'TEXT') {
		myVideosContent = sources[url]['XHR']['TEXT'];
	    }
	    else {
		myPageContent = sources[url]['XHR']['TEXT'];
		if (clean) myPageContent = cleanMyContent(myPageContent, true);
		myVideosParse = myPageContent.match(pattern);
		myVideosContent = (myVideosParse) ? myVideosParse[1] : null;
	    }
	    return myVideosContent;
	}
    }

    function setMyOptions(key, value) {
	var pair = {};
	pair[key] = value;
	browser.storage.local.set(pair);
    }

    function showMyMessage(cause, content) {
	var myScriptLogo = createMyElement('div', userscript, '', '', '');
	styleMyElement(myScriptLogo, {margin: '0px auto', padding: '10px', color: '#666666', fontSize: '24px', textAlign: 'center', textShadow: '#FFFFFF -1px -1px 2px'});
	var myScriptMess = createMyElement('div', '', '', '', '');
	styleMyElement(myScriptMess, {border: '1px solid #F4F4F4', margin: '5px auto 5px auto', padding: '10px', backgroundColor: '#FFFFFF', color: '#AD0000', textAlign: 'center'});
	if (cause == '!player') {
	    var myScriptAlert = createMyElement('div', '', '', '', '');
	    styleMyElement(myScriptAlert, {position: 'absolute', top: '30%', left: '35%', border: '1px solid #F4F4F4', borderRadius: '3px', padding: '10px', backgroundColor: '#FFFFFF', fontSize: '14px', textAlign: 'center', zIndex: '99999'});
	    myScriptAlert.appendChild(myScriptLogo);
	    var myNoPlayerMess = 'Couldn\'t get the player element. Please report it <a href="' + contact + '" style="color:#00892C">here</a>.';
	    modifyMyElement(myScriptMess, 'div', myNoPlayerMess, false);
	    myScriptAlert.appendChild(myScriptMess);
	    var myScriptAlertButton = createMyElement('div', 'OK', 'click', 'close', myScriptAlert);
	    styleMyElement(myScriptAlertButton, {width: '100px', border: '3px solid #EEEEEE', borderRadius: '5px', margin: '0px auto', backgroundColor: '#EEEEEE', color: '#666666', fontSize: '18px', textAlign: 'center', textShadow: '#FFFFFF -1px -1px 2px', cursor: 'pointer'});
	    myScriptAlert.appendChild(myScriptAlertButton);
	    page.body.appendChild(myScriptAlert);
	}
	else if (cause == '!thumb') {
	    var myNoThumbMess = '<br><br>Couldn\'t get the thumbnail for this video. Please report it <a href="' + contact + '" style="color:#00892C">here</a>.';
	    modifyMyElement(player['playerContent'], 'div', myNoThumbMess, false);
	}
	else {
	    myPlayerWindow.appendChild(myScriptLogo);
	    if (cause == '!content') {
		var myNoContentMess = 'Couldn\'t get the videos content. Please report it <a href="' + contact + '" style="color:#00892C">here</a>.';
		modifyMyElement(myScriptMess, 'div', myNoContentMess, false);
	    }
	    else if (cause == '!videos') {
		var myNoVideosMess = 'Couldn\'t get any video. Please report it <a href="' + contact + '" style="color:#00892C">here</a>.';
		modifyMyElement(myScriptMess, 'div', myNoVideosMess, false);
	    }
	    else if (cause == '!support') {
		var myNoSupportMess = 'This video uses the RTMP protocol and is not supported.';
		modifyMyElement(myScriptMess, 'div', myNoSupportMess, false);
	    }
	    else if (cause == 'embed') {
		var myEmbedMess = 'This is an embedded video. You can watch it <a href="' + content + '" style="color:#00892C">here</a>.';
		modifyMyElement(myScriptMess, 'div', myEmbedMess, false);
	    }
	    else if (cause == 'other') {
		modifyMyElement(myScriptMess, 'div', content, false);
	    }
	    myPlayerWindow.appendChild(myScriptMess);
	}
    }

    // =====YouTube===== //

    /* Redirect Categories */
    if (page.url.indexOf('gaming.youtube.com') != -1) {
	page.win.location.href = page.url.replace('gaming', 'www');
    }

    /* Video Availability */
    var ytVideoUnavailable = document.querySelector("#player-unavailable");
    if (ytVideoUnavailable) {
	if (ytVideoUnavailable.className.indexOf('hid') == -1) {
	    var ytAgeGateContent = document.querySelector("#watch7-player-age-gate-content");
	    if (!ytAgeGateContent) return;
	    else {
		if(ytAgeGateContent.indexOf('feature=private_video') != -1) return;
	    }
	}
    }

    /* Decrypt Signature */
    var ytScriptSrc;
    function ytDecryptSignature(s) {return null;}
    function ytDecryptFunction() {
	var ytSignFuncName, ytSignFuncBody, ytSwapFuncName, ytSwapFuncBody, ytFuncMatch;
	ytScriptSrc = ytScriptSrc.replace(/(\r\n|\n|\r)/gm, '');
	ytSignFuncName = ytScriptSrc.match(/"signature"\s*,\s*([^\)]*?)\(/);
	ytSignFuncName = (ytSignFuncName) ? ytSignFuncName[1] : null;
	if (ytSignFuncName) {
	    ytFuncMatch = ytSignFuncName.replace(/\$/, '\\$') + '\\s*=\\s*function\\s*' + '\\s*\\(\\w+\\)\\s*\\{(.*?)\\}';
	    ytSignFuncBody = ytScriptSrc.match(ytFuncMatch);
	    ytSignFuncBody = (ytSignFuncBody) ? ytSignFuncBody[1] : null;
	    if (ytSignFuncBody) {
		ytSwapFuncName = ytSignFuncBody.match(/((\$|_|\w)+)\.(\$|_|\w)+\(\w,[0-9]+\)/);
		ytSwapFuncName = (ytSwapFuncName) ? ytSwapFuncName[1] : null;
		if (ytSwapFuncName) {
		    ytFuncMatch = 'var\\s+' + ytSwapFuncName.replace(/\$/, '\\$') + '=\\s*\\{(.*?)\\};';
		    ytSwapFuncBody = ytScriptSrc.match(ytFuncMatch);
		    ytSwapFuncBody = (ytSwapFuncBody) ? ytSwapFuncBody[1] : null;
		}
		if (ytSwapFuncBody) ytSignFuncBody = 'var ' + ytSwapFuncName + '={' + ytSwapFuncBody + '};' + ytSignFuncBody;
		ytSignFuncBody = 'try {' + ytSignFuncBody + '} catch(e) {return null}';
		ytDecryptSignature = new Function('a', ytSignFuncBody);
	    }
	}
    }

    /* Player Size */
    var ytSidebarMarginNormal = 382;
    var ytSidebarWindow = document.querySelector("#watch7-sidebar");
    if (ytSidebarWindow) {
	var ytSidebarWindowStyle = ytSidebarWindow.currentStyle || window.getComputedStyle(ytSidebarWindow);
	if (ytSidebarWindowStyle) ytSidebarMarginNormal = -12 + parseInt(ytSidebarWindowStyle.marginTop.replace('px', ''));
	styleMyElement(ytSidebarWindow, {marginTop: ytSidebarMarginNormal + 'px'});
    }
    var ytPlayerWidth, ytPlayerHeight;
    var ytPlayerWideWidth, ytPlayerWideHeight;
    var ytSidebarMarginWide;
    var ytScreenWidth, ytScreenHeight;
    function ytSizes() {
	ytScreenWidth = page.win.innerWidth || page.doc.documentElement.clientWidth;
	ytScreenHeight = page.win.innerHeight || page.doc.documentElement.clientHeight;
	if (ytScreenWidth >= 1720 && ytScreenHeight >= 980) {
	    ytPlayerWidth = 1280;
	    ytPlayerHeight = 742;
	    ytPlayerWideWidth = 1706;
	    ytPlayerWideHeight = 982;
	}
	else if (ytScreenWidth >= 1294 && ytScreenHeight >= 630) {
	    ytPlayerWidth = 854;
	    ytPlayerHeight = 502;
	    ytPlayerWideWidth = 1280;
	    ytPlayerWideHeight = 742;
	}
	else {
	    ytPlayerWidth = 640;
	    ytPlayerHeight = 382;
	    ytPlayerWideWidth = 1066;
	    ytPlayerWideHeight = 622;
	}
	ytSidebarMarginWide = ytPlayerHeight + ytSidebarMarginNormal;
    }

    /* Get Player Window */
    var ytPlayerWindow = document.querySelector("#player");
    if (!ytPlayerWindow) {
	showMyMessage('!player');
    }
    else {
	/* Get Video Thumbnail */
	var ytVideoThumb = getMyContent(page.url, 'link\\s+itemprop="thumbnailUrl"\\s+href="(.*?)"', false);
	if (!ytVideoThumb) ytVideoThumb = getMyContent(page.url, 'meta\\s+property="og:image"\\s+content="(.*?)"', false);
	if (!ytVideoThumb) {
	    var ytVideoID = page.url.match(/(\?|&)v=(.*?)(&|$)/);
	    if (ytVideoID) ytVideoThumb = 'https://img.youtube.com/vi/' + ytVideoID[2] + '/0.jpg';
	}

	/* Get Videos Content */
	var ytVideosEncodedFmts, ytVideosAdaptiveFmts, ytVideosContent, ytHLSVideos, ytHLSContent;
	ytVideosEncodedFmts = getMyContent(page.url, '"url_encoded_fmt_stream_map":\\s*"(.*?)"', false);
	if (!ytVideosEncodedFmts) ytVideosEncodedFmts = getMyContent(page.url, '\\\\"url_encoded_fmt_stream_map\\\\":\\s*\\\\"(.*?)\\\\"', false);
	ytVideosAdaptiveFmts = getMyContent(page.url, '"adaptive_fmts":\\s*"(.*?)"', false);
	if (!ytVideosAdaptiveFmts) ytVideosAdaptiveFmts = getMyContent(page.url, '\\\\"adaptive_fmts\\\\":\\s*\\\\"(.*?)\\\\"', false);
	if (ytVideosEncodedFmts) {
	    ytVideosContent = ytVideosEncodedFmts;
	}
	else {
	    ytHLSVideos = getMyContent(page.url, '"hlsvp":\\s*"(.*?)"', false);
	    if (!ytHLSVideos) ytHLSVideos = getMyContent(page.url, '\\\\"hlsvp\\\\":\\s*\\\\"(.*?)\\\\"', false);
	    if (ytHLSVideos) {
		ytHLSVideos = cleanMyContent(ytHLSVideos, false);
		if (ytHLSVideos.indexOf('keepalive/yes/') != -1) ytHLSVideos = ytHLSVideos.replace('keepalive/yes/', '');
	    }
	    else {
		var ytVideoID = page.url.match(/(\?|&)v=(.*?)(&|$)/);
		ytVideoID = (ytVideoID) ? ytVideoID[2] : null;
		if (ytVideoID) {
		    var ytVideoSts = getMyContent(page.url.replace(/watch.*?v=/, 'embed/').replace(/&.*$/, ''), '"sts"\\s*:\\s*(\\d+)', false);
		    var ytVideosInfoURL = page.win.location.protocol + '//' + page.win.location.hostname + '/get_video_info?video_id=' + ytVideoID + '&eurl=https://youtube.googleapis.com/v/' + ytVideoID + '&sts=' + ytVideoSts;
		    var ytVideosInfo = getMyContent(ytVideosInfoURL, 'TEXT', false);
		    if (ytVideosInfo) {
			ytVideosEncodedFmts = ytVideosInfo.match(/url_encoded_fmt_stream_map=(.*?)&/);
			ytVideosEncodedFmts = (ytVideosEncodedFmts) ? ytVideosEncodedFmts[1] : null;
			if (ytVideosEncodedFmts) {
			    ytVideosEncodedFmts = cleanMyContent(ytVideosEncodedFmts, true);
			    ytVideosContent = ytVideosEncodedFmts;
			}
			if (!ytVideosAdaptiveFmts) {
			    ytVideosAdaptiveFmts = ytVideosInfo.match(/adaptive_fmts=(.*?)&/);
			    ytVideosAdaptiveFmts = (ytVideosAdaptiveFmts) ? ytVideosAdaptiveFmts[1] : null;
			    if (ytVideosAdaptiveFmts) ytVideosAdaptiveFmts = cleanMyContent(ytVideosAdaptiveFmts, true);
			}
		    }
		}
	    }
	}
	if (ytVideosAdaptiveFmts && !ytHLSVideos) {
	    if (ytVideosContent) ytVideosContent += ',' + ytVideosAdaptiveFmts;
	    else ytVideosContent = ytVideosAdaptiveFmts;
	}

	/* Get Sizes */
	ytSizes();

	function removeElements (selector) {
	    document.querySelectorAll(selector).forEach(function (element) { element.remove(); });
	}

	/* Remove Player Window */
	removeElements("#placeholder-player");

	/* Remove Sidebar Ads */
	removeElements("#watch7-sidebar-ads");

	/* Playlist */
	var ytPlaylist = document.querySelector("#player-playlist");
	if (ytPlaylist) {
	    styleMyElement(ytPlaylist, {marginLeft: '-' + ytPlayerWidth + 'px'});
	    var ytPlaceholderPlaylist = document.querySelector("#placeholder-playlist");
	    if (ytPlaceholderPlaylist) ytPlaceholderPlaylist.appendChild(ytPlaylist);
	}

	/* My Player Window */
	var myPlayerWindow = createMyElement('div', '', '', '', '');
	styleMyElement(myPlayerWindow, {position: 'relative', width: ytPlayerWidth + 'px', height: ytPlayerHeight + 'px', backgroundColor: '#FFFFFF'});
	modifyMyElement(ytPlayerWindow, 'div', '', false, true);
	ytPlayerWindow.appendChild(myPlayerWindow);

	/* Update Sizes */
	page.win.addEventListener('resize', function() {
	    ytSizes();
	    player['playerWidth'] = ytPlayerWidth;
	    player['playerHeight'] = ytPlayerHeight;
	    player['playerWideWidth'] = ytPlayerWideWidth;
	    player['playerWideHeight'] = ytPlayerWideHeight;
	    player['sidebarMarginWide'] = ytSidebarMarginWide;
	    resizeMyPlayer('widesize');
	    if (ytPlaylist) styleMyElement(ytPlaylist, {marginLeft: '-' + ytPlayerWidth + 'px'});
	}, false);

	/* Create Player */
	var ytDefaultVideo = 'Low Definition MP4';
	function ytPlayer() {
	    player = {
		'playerSocket': ytPlayerWindow,
		'playerWindow': myPlayerWindow,
		'videoList': ytVideoList,
		'videoPlay': ytDefaultVideo,
		'videoThumb': ytVideoThumb,
		'playerWidth': ytPlayerWidth,
		'playerHeight': ytPlayerHeight,
		'playerWideWidth': ytPlayerWideWidth,
		'playerWideHeight': ytPlayerWideHeight,
		'sidebarWindow': ytSidebarWindow,
		'sidebarMarginNormal': ytSidebarMarginNormal,
		'sidebarMarginWide': ytSidebarMarginWide
	    };
	    option['definitions'] = ['Ultra High Definition', 'Full High Definition', 'High Definition', 'Standard Definition', 'Low Definition', 'Very Low Definition'];
	    option['containers'] = ['MP4', 'WebM', 'FLV', '3GP', 'Any'];
	    createMyPlayer();
	}

	/* Parse Videos */
	function ytVideos() {
	    var ytVideoFormats = {
		'5': 'Very Low Definition FLV',
		'17': 'Very Low Definition 3GP',
		'18': 'Low Definition MP4',
		'22': 'High Definition MP4',
		'34': 'Low Definition FLV',
		'35': 'Standard Definition FLV',
		'36': 'Low Definition 3GP',
		'37': 'Full High Definition MP4',
		'38': 'Ultra High Definition MP4',
		'43': 'Low Definition WebM',
		'44': 'Standard Definition WebM',
		'45': 'High Definition WebM',
		'46': 'Full High Definition WebM',
		'82': 'Low Definition 3D MP4',
		'83': 'Standard Definition 3D MP4',
		'84': 'High Definition 3D MP4',
		'85': 'Full High Definition 3D MP4',
		'100': 'Low Definition 3D WebM',
		'101': 'Standard Definition 3D WebM',
		'102': 'High Definition 3D WebM',
		'135': 'Standard Definition Video MP4',
		'136': 'High Definition Video MP4',
		'137': 'Full High Definition Video MP4',
		'138': 'Ultra High Definition Video MP4',
		'139': 'Low Bitrate Audio MP4',
		'140': 'Medium Bitrate Audio MP4',
		'141': 'High Bitrate Audio MP4',
		'171': 'Medium Bitrate Audio WebM',
		'172': 'High Bitrate Audio WebM',
		'244': 'Standard Definition Video WebM',
		'247': 'High Definition Video WebM',
		'248': 'Full High Definition Video WebM',
		'249': 'Low Bitrate Audio Opus',
		'250': 'Medium Bitrate Audio Opus',
		'251': 'High Bitrate Audio Opus',
		'266': 'Ultra High Definition Video MP4',
		'272': 'Ultra High Definition Video WebM',
		'298': 'High Definition Video MP4',
		'299': 'Full High Definition Video MP4',
		'302': 'High Definition Video WebM',
		'303': 'Full High Definition Video WebM',
		'313': 'Ultra High Definition Video WebM'
	    };
	    var ytVideoFound = false;
	    var ytVideos = ytVideosContent.split(',');
	    var ytVideoParse, ytVideoCodeParse, ytVideoCode, myVideoCode, ytVideo;
	    for (var i = 0; i < ytVideos.length; i++) {
		if (!ytVideos[i].match(/^url/)) {
		    ytVideoParse = ytVideos[i].match(/(.*)(url=.*$)/);
		    if (ytVideoParse) ytVideos[i] = ytVideoParse[2] + '&' + ytVideoParse[1];
		}
		ytVideoCodeParse = ytVideos[i].match(/itag=(\d{1,3})/);
		ytVideoCode = (ytVideoCodeParse) ? ytVideoCodeParse[1] : null;
		if (ytVideoCode) {
		    myVideoCode = ytVideoFormats[ytVideoCode];
		    if (myVideoCode) {
			ytVideo = cleanMyContent(ytVideos[i], true);
			ytVideo = ytVideo.replace(/url=/, '').replace(/&$/, '');
			if (ytVideo.match(/itag=/) && ytVideo.match(/itag=/g).length > 1) {
			    if (ytVideo.match(/itag=\d{1,3}&/)) ytVideo = ytVideo.replace(/itag=\d{1,3}&/, '');
			    else if (ytVideo.match(/&itag=\d{1,3}/)) ytVideo = ytVideo.replace(/&itag=\d{1,3}/, '');
			}
			if (ytVideo.match(/clen=/) && ytVideo.match(/clen=/g).length > 1) {
			    if (ytVideo.match(/clen=\d+&/)) ytVideo = ytVideo.replace(/clen=\d+&/, '');
			    else if (ytVideo.match(/&clen=\d+/)) ytVideo = ytVideo.replace(/&clen=\d+/, '');
			}
			if (ytVideo.match(/lmt=/) && ytVideo.match(/lmt=/g).length > 1) {
			    if (ytVideo.match(/lmt=\d+&/)) ytVideo = ytVideo.replace(/lmt=\d+&/, '');
			    else if (ytVideo.match(/&lmt=\d+/)) ytVideo = ytVideo.replace(/&lmt=\d+/, '');
			}
			if (ytVideo.match(/type=(video|audio).*?&/)) ytVideo = ytVideo.replace(/type=(video|audio).*?&/, '');
			else ytVideo = ytVideo.replace(/&type=(video|audio).*$/, '');
			if (ytVideo.match(/xtags=[^%=]*&/)) ytVideo = ytVideo.replace(/xtags=[^%=]*?&/, '');
			else if (ytVideo.match(/&xtags=[^%=]*$/)) ytVideo = ytVideo.replace(/&xtags=[^%=]*$/, '');
			if (ytVideo.match(/&sig=/)) ytVideo = ytVideo.replace(/&sig=/, '&signature=');
			else if (ytVideo.match(/&s=/)) {
			    var ytSig = ytVideo.match(/&s=(.*?)(&|$)/);
			    if (ytSig) {
				var s = ytSig[1];
				s = ytDecryptSignature(s);
				if (s) ytVideo = ytVideo.replace(/&s=.*?(&|$)/, '&signature=' + s + '$1');
				else ytVideo = '';
			    }
			    else ytVideo = '';
			}
			ytVideo = cleanMyContent(ytVideo, true);
			if (ytVideo.indexOf('ratebypass') == -1) ytVideo += '&ratebypass=yes';
			if (ytVideo && ytVideo.indexOf('http') == 0) {
			    if (!ytVideoFound) ytVideoFound = true;
			    ytVideoList[myVideoCode] = ytVideo;
			}
		    }
		}
	    }

	    if (ytVideoFound) {
		/* DASH */
		if (!ytVideoList['Standard Definition MP4'] && ytVideoList['Standard Definition Video MP4']) ytVideoList['Standard Definition MP4'] = 'DASH';
		if (!ytVideoList['High Definition MP4'] && ytVideoList['High Definition Video MP4']) ytVideoList['High Definition MP4'] = 'DASH';
		if (!ytVideoList['Full High Definition MP4'] && ytVideoList['Full High Definition Video MP4']) ytVideoList['Full High Definition MP4'] = 'DASH';
		if (!ytVideoList['Ultra High Definition MP4'] && ytVideoList['Ultra High Definition Video MP4']) ytVideoList['Ultra High Definition MP4'] = 'DASH';
		if (!ytVideoList['Standard Definition WebM'] && ytVideoList['Standard Definition Video WebM']) ytVideoList['Standard Definition WebM'] = 'DASH';
		if (!ytVideoList['High Definition WebM'] && ytVideoList['High Definition Video WebM']) ytVideoList['High Definition WebM'] = 'DASH';
		if (!ytVideoList['Full High Definition WebM'] && ytVideoList['Full High Definition Video WebM']) ytVideoList['Full High Definition WebM'] = 'DASH';
		if (!ytVideoList['Ultra High Definition WebM'] && ytVideoList['Ultra High Definition Video WebM']) ytVideoList['Ultra High Definition WebM'] = 'DASH';

		/* DVL */
		ytVideoList['Direct Video Link'] = page.url;

		ytPlayer();
	    }
	    else {
		if (ytVideosContent.indexOf('conn=rtmp') != -1) showMyMessage('!support');
		else showMyMessage('!videos');
	    }
	}

	/* Parse HLS */
	function ytHLS() {
	    var ytHLSFormats = {
		'92': 'Very Low Definition MP4',
		'93': 'Low Definition MP4',
		'94': 'Standard Definition MP4',
		'95': 'High Definition MP4',
		'96': 'Full High Definition MP4'
	    };
	    ytVideoList["Any Definition MP4"] = ytHLSVideos;
	    if (ytHLSContent) {
		var ytHLSVideo, ytVideoCodeParse, ytVideoCode, myVideoCode;
		var ytHLSMatcher = new RegExp('(http.*?m3u8)', 'g');
		ytHLSVideos = ytHLSContent.match(ytHLSMatcher);
		if (ytHLSVideos) {
		    for (var i = 0; i < ytHLSVideos.length; i++) {
			ytHLSVideo = ytHLSVideos[i];
			ytVideoCodeParse = ytHLSVideo.match(/\/itag\/(\d{1,3})\//);
			ytVideoCode = (ytVideoCodeParse) ? ytVideoCodeParse[1] : null;
			if (ytVideoCode) {
			    myVideoCode = ytHLSFormats[ytVideoCode];
			    if (myVideoCode && ytHLSVideo) {
				ytVideoList[myVideoCode] = ytHLSVideo;
			    }
			}
		    }
		}
	    }

	    /* DVL */
	    ytVideoList['Direct Video Link'] = page.url;

	    ytDefaultVideo = 'Any Definition MP4';
	    ytPlayer();
	}

	/* Get Videos */
	var ytVideoList = {};
	if (ytVideosContent) {
	    if (ytVideosContent.match(/&s=/) || ytVideosContent.match(/,s=/) || ytVideosContent.match(/u0026s=/)) {
		var ytScriptURL = getMyContent(page.url, '"js":\\s*"(.*?)"', true);
		if (!ytScriptURL) ytScriptURL = getMyContent(page.url.replace(/watch.*?v=/, 'embed/').replace(/&.*$/, ''), '"js":\\s*"(.*?)"', true);
		if (ytScriptURL) {
		    ytScriptURL = page.win.location.protocol + ytScriptURL;
		    try {
			ytScriptSrc = getMyContent(ytScriptURL, 'TEXT', false);
			if (ytScriptSrc) ytDecryptFunction();
			ytVideos();
		    }
		    catch(e) {
			try {
			    var oReq = new XMLHttpRequest();
			    oReq.open("GET", ytScriptURL);
			    oReq.onload = function(response) {
				if (response.readyState === 4 && response.status === 200 && response.responseText) {
				    ytScriptSrc = response.responseText;
				    ytDecryptFunction();
				    ytVideos();
				}
				else {
				    showMyMessage('other', 'Couldn\'t get the signature content. Please report it <a href="' + contact + '" style="color:#00892C">here</a>.');
				}
			    };
			    oReq.onerror = function() {
				showMyMessage('other', 'Couldn\'t make the request. Make sure your browser user scripts extension supports cross-domain requests.');
			    };
			    oReq.send();
			}
			catch(e) {
			    showMyMessage('other', 'Couldn\'t make the request. Make sure your browser user scripts extension supports cross-domain requests.');
			}
		    }
		}
		else {
		    showMyMessage('other', 'Couldn\'t get the signature link. Please report it <a href="' + contact + '" style="color:#00892C">here</a>.');
		}
	    }
	    else {
		ytVideos();
	    }
	}
	else {
	    if (ytHLSVideos) {
		try {
		    ytHLSContent = getMyContent(ytHLSVideos, 'TEXT', false);
		    ytHLS();
		}
		catch(e) {
		    try {
			var oReq = new XMLHttpRequest();
			oReq.open("GET", ytHLSVideos);
			oReq.onload = function(response) {
			    if (response.readyState === 4 && response.status === 200 && response.responseText) {
				ytHLSContent = response.responseText;
			    }
			    ytHLS();
			};
			oReq.onerror = function() {
			    ytHLS();
			};
			oReq.send();
		    }
		    catch(e) {
			ytHLS();
		    }
		}
	    }
	    else {
		showMyMessage('!content');
	    }
	}
    }

})();