This repository has been archived by the owner on Jul 14, 2019. It is now read-only.
forked from fluid-player/fluid-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluidplayer.js
4790 lines (3856 loc) · 184 KB
/
fluidplayer.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
var fluidPlayerScriptLocation = function() {
var currentSrc = '';
if (document.currentScript) {
currentSrc = document.currentScript.src;
} else {
//IE
currentSrc = (function() {
var scripts = document.getElementsByTagName('script'),
script = scripts[scripts.length - 1];
if (script.getAttribute.length !== undefined) {
return script.src;
}
return script.getAttribute('src', -1)
}());
}
if (currentSrc) {
return currentSrc.substring(0, currentSrc.lastIndexOf('/') + 1);
}
return '';
}();
//Object.assign polyfill
if (typeof Object.assign != 'function') {
// Must be writable: true, enumerable: false, configurable: true
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
//CustomEvent polyfill
(function () {
if ( typeof window.CustomEvent === "function" ) return false;
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
})();
fluidPlayer = function(idVideoPlayer, options) {
var inArray = function(needle, haystack) {
var length = haystack.length;
for (var i = 0; i < length; i++) {
if (haystack[i] == needle) {
return true;
}
}
return false;
};
var copy = fluidPlayerClass.constructor();
for (var attr in fluidPlayerClass) {
if (fluidPlayerClass.hasOwnProperty(attr) && !inArray(attr, fluidPlayerClass.notCloned)) {
copy[attr] = fluidPlayerClass[attr];
}
}
fluidPlayerClass.instances.push(copy);
copy.init(idVideoPlayer, options);
return copy;
};
var fluidPlayerClass = {
vttParserScript: '/scripts/webvtt.min.js',
instances: [],
notCloned: ['notCloned', 'vttParserScript', 'instances', 'getInstanceById',
'requestStylesheet', 'reqiestScript', 'isTouchDevice', 'vastOptions',
'displayOptions', 'getEventOffsetX', 'getEventOffsetY', 'getTranslateX',
'toggleElementText', 'getMobileOs', 'findClosestParent', 'activeVideoPlayerId',
'getInstanceIdByWrapperId', 'timer', 'timerPool', 'adList', 'adPool',
'isUserActive', 'isCurrentlyPlayingAd', 'initialAnimationSet'],
version: '2.4.2',
homepage: 'https://www.fluidplayer.com/',
activeVideoPlayerId: null,
getInstanceById: function(playerId) {
for (var i = 0; i < this.instances.length; i++) {
if (this.instances[i].videoPlayerId === playerId) {
return this.instances[i];
}
}
return null;
},
getInstanceIdByWrapperId: function(wrapperId) {
return typeof wrapperId != "undefined" ? wrapperId.replace('fluid_video_wrapper_', '') : null;
},
requestStylesheet: function(cssId, url) {
if (!document.getElementById(cssId)) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = url;
link.media = 'all';
head.appendChild(link);
}
},
requestScript: function(url, callback) {
// Adding the script tag to the head
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
// Fire the loading
head.appendChild(script);
},
isTouchDevice: function() {
return !!('ontouchstart' in window // works on most browsers
|| navigator.maxTouchPoints); // works on IE10/11 and Surface
},
/**
* Distinguises iOS from Android devices and the OS version.
*
* @returns object
*/
getMobileOs: function() {
var ua = navigator.userAgent;
var uaindex;
var result = {device: false, userOs: false, userOsVer: false, userOsMajor: false};
// determine OS
if (ua.match(/iPad/i)) {
result.device = 'iPad';
result.userOs = 'iOS';
uaindex = ua.indexOf('OS ');
} else if (ua.match(/iPhone/i)) {
result.device = 'iPhone';
result.userOs = 'iOS';
uaindex = ua.indexOf('OS ');
} else if (ua.match(/Android/i)) {
result.userOs = 'Android';
uaindex = ua.indexOf('Android ');
} else {
result.userOs = false;
}
// determine version
if (result.userOs === 'iOS' && (uaindex > -1)) {
var userOsTemp = ua.substr(uaindex + 3);
var indexOfEndOfVersion = userOsTemp.indexOf(' ');
if (indexOfEndOfVersion !== -1) {
result.userOsVer = userOsTemp.substring(0, userOsTemp.indexOf(' ')).replace(/_/g, '.');
result.userOsMajor = parseInt(result.userOsVer);
}
} else if (result.userOs === 'Android' && (uaindex > -1)) {
result.userOsVer = ua.substr(uaindex + 8, 3);
} else {
result.userOsVer = false;
}
return result;
},
/**
* Browser detection
*
* @returns object
*/
getBrowserVersion: function() {
var ua = navigator.userAgent;
var result = {browserName: false, fullVersion: false, majorVersion: false, userOsMajor: false};
var idx, uaindex;
try {
result.browserName = navigator.appName;
if ((idx = ua.indexOf("OPR/")) != -1) {
result.browserName = "Opera";
result.fullVersion = ua.substring(idx + 4);
}
else if ((idx = ua.indexOf("Opera")) != -1) {
result.browserName = "Opera";
result.fullVersion = ua.substring(idx + 6);
if ((idx = ua.indexOf("Version")) != -1)
result.fullVersion = ua.substring(idx + 8);
}
else if ((idx = ua.indexOf("MSIE")) != -1) {
result.browserName = "Microsoft Internet Explorer";
result.fullVersion = ua.substring(idx + 5);
}
else if ((idx = ua.indexOf("Chrome")) != -1) {
result.browserName = "Google Chrome";
result.fullVersion = ua.substring(idx + 7);
}
else if ((idx = ua.indexOf("Safari")) != -1) {
result.browserName = "Safari";
result.fullVersion = ua.substring(idx + 7);
if ((idx = ua.indexOf("Version")) != -1)
result.fullVersion = ua.substring(idx + 8);
}
else if ((idx = ua.indexOf("Firefox")) != -1) {
result.browserName = "Mozilla Firefox";
result.fullVersion = ua.substring(idx + 8);
}
// Others "name/version" is at the end of userAgent
else if ((uaindex = ua.lastIndexOf(' ') + 1) < (idx = ua.lastIndexOf('/'))) {
result.browserName = ua.substring(uaindex, idx);
result.fullVersion = ua.substring(idx + 1);
if (result.browserName.toLowerCase() == result.browserName.toUpperCase()) {
result.browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((uaindex = result.fullVersion.indexOf(';')) != -1) {
result.fullVersion = result.fullVersion.substring(0, uaindex);
}
if ((uaindex = result.fullVersion.indexOf(' ')) != -1) {
result.fullVersion = result.fullVersion.substring(0, uaindex);
}
result.majorVersion = parseInt('' + result.fullVersion, 10);
if (isNaN(result.majorVersion)) {
result.fullVersion = '' + parseFloat(navigator.appVersion);
result.majorVersion = parseInt(navigator.appVersion, 10);
}
} catch (e) {
//Return default obj.
}
return result;
},
getCurrentVideoDuration: function() {
var videoPlayerTag = document.getElementById(this.videoPlayerId);
if (videoPlayerTag) {
return videoPlayerTag.duration;
}
return 0;
},
getClickThroughUrlFromLinear: function(linear) {
var videoClicks = linear.getElementsByTagName('VideoClicks');
if (videoClicks.length) {//There should be exactly 1 node
var clickThroughs = videoClicks[0].getElementsByTagName('ClickThrough');
if (clickThroughs.length) {
return this.extractNodeData(clickThroughs[0]);
}
}
return false;
},
getVastAdTagUriFromWrapper: function(xmlResponse) {
var wrapper = xmlResponse.getElementsByTagName('Wrapper');
if (typeof wrapper !== 'undefined' && wrapper.length) {
var vastAdTagURI = wrapper[0].getElementsByTagName('VASTAdTagURI');
if (vastAdTagURI.length) {
return this.extractNodeData(vastAdTagURI[0]);
}
}
return false;
},
hasInLine: function (xmlResponse) {
var inLine = xmlResponse.getElementsByTagName('InLine');
return ((typeof inLine !== 'undefined') && inLine.length);
},
hasVastAdTagUri: function (xmlResponse) {
var vastAdTagURI = xmlResponse.getElementsByTagName('VASTAdTagURI');
return ((typeof vastAdTagURI !== 'undefined') && vastAdTagURI.length);
},
hasVastAdTagUriFromWrapper: function(creative) {
var player = this;
if ((typeof creative !== 'undefined') && creative.length) {
var arrayCreativeLinears = creative[0].getElementsByTagName('Linear');
if ((typeof arrayCreativeLinears !== 'undefined') && (arrayCreativeLinears !== null) && arrayCreativeLinears.length) {
return player.getMediaFileFromLinear(arrayCreativeLinears[0]);
}
}
return false;
},
getClickThroughUrlFromNonLinear: function (nonLinear) {
var result = '';
var nonLinears = nonLinear.getElementsByTagName('NonLinear');
if (nonLinears.length) {//There should be exactly 1 node
var nonLinearClickThrough = nonLinear.getElementsByTagName('NonLinearClickThrough');
if (nonLinearClickThrough.length) {
result = this.extractNodeData(nonLinearClickThrough[0]);
}
}
return result;
},
getTrackingFromLinear: function(linear) {
var trackingEvents = linear.getElementsByTagName('TrackingEvents');
if (trackingEvents.length) {//There should be no more than one node
return trackingEvents[0].getElementsByTagName('Tracking');
}
return [];
},
getDurationFromLinear: function(linear) {
var duration = linear.getElementsByTagName('Duration');
if (duration.length && (typeof duration[0].childNodes[0] !== 'undefined')) {
var nodeDuration = this.extractNodeData(duration[0]);
return this.convertTimeStringToSeconds(nodeDuration);
}
return false;
},
getDurationFromNonLinear: function(tag) {
var result = 0;
var nonLinear = tag.getElementsByTagName('NonLinear');
if (nonLinear.length && (typeof nonLinear[0].getAttribute('minSuggestedDuration') !== 'undefined')) {
result = this.convertTimeStringToSeconds(nonLinear[0].getAttribute('minSuggestedDuration'));
}
return result;
},
getDimensionFromNonLinear: function (tag) {
var result = {'width': null, 'height': null};
var nonLinear = tag.getElementsByTagName('NonLinear');
if (nonLinear.length) {
if (typeof nonLinear[0].getAttribute('width') !== 'undefined') {
result.width = nonLinear[0].getAttribute('width');
}
if (typeof nonLinear[0].getAttribute('height') !== 'undefined') {
result.height = nonLinear[0].getAttribute('height');
}
}
return result;
},
getCreativeTypeFromStaticResources: function (tag) {
var result = '';
var nonLinears = tag.getElementsByTagName('NonLinear');
if (nonLinears.length && (typeof nonLinears[0].childNodes[0] !== 'undefined')) {//There should be exactly 1 StaticResource node
result = nonLinears[0].getElementsByTagName('StaticResource')[0].getAttribute('creativeType');
}
return result.toLowerCase();
},
getMediaFilesFromLinear: function(linear) {
var mediaFiles = linear.getElementsByTagName('MediaFiles');
if (mediaFiles.length) {//There should be exactly 1 MediaFiles node
return mediaFiles[0].getElementsByTagName('MediaFile');
}
return [];
},
getStaticResourcesFromNonLinear: function(linear) {
var result = [];
var nonLinears = linear.getElementsByTagName('NonLinear');
if (nonLinears.length) {//There should be exactly 1 StaticResource node
result = nonLinears[0].getElementsByTagName('StaticResource');
}
return result;
},
extractNodeData: function(parentNode) {
var contentAsString = "";
for(var n = 0; n < parentNode.childNodes.length; n ++)
{
var child = parentNode.childNodes[n];
if (child.nodeType === 8 || (child.nodeType === 3 && /^\s*$/.test(child.nodeValue))) {
// Comments or text with no content
} else {
contentAsString += child.nodeValue;
}
}
var tidyString = contentAsString.replace(/(^\s+|\s+$)/g,'');
return tidyString;
},
getMediaFileFromLinear: function(linear) {
var mediaFile;
var mediaFiles = this.getMediaFilesFromLinear(linear);
if (mediaFiles.length) {
mediaFile = this.extractNodeData(mediaFiles[0]);
}
return mediaFile;
},
getIconClickThroughFromLinear: function (linear) {
var iconClickThrough = linear.getElementsByTagName('IconClickThrough');
if (iconClickThrough.length) {
return this.extractNodeData(iconClickThrough[0]);
} else {
this.displayOptions.vastOptions.adCTAText = false;
}
return '';
},
getStaticResourceFromNonLinear: function(linear) {
var fallbackStaticResource;
var staticResources = this.getStaticResourcesFromNonLinear(linear);
for (var i = 0; i < staticResources.length; i++) {
if (!staticResources[i].getAttribute('type')) {
fallbackStaticResource = this.extractNodeData(staticResources[i]);
}
if (staticResources[i].getAttribute('type') === this.displayOptions.staticResource) {
return this.extractNodeData(staticResources[i]);
}
}
return fallbackStaticResource;
},
registerTrackingEvents: function(creativeLinear, tmpOptions) {
var trackingEvents = this.getTrackingFromLinear(creativeLinear);
var eventType = '';
var oneEventOffset = 0;
for (var i = 0; i < trackingEvents.length; i++) {
eventType = trackingEvents[i].getAttribute('event');
switch (eventType) {
case 'start':
case 'firstQuartile':
case 'midpoint':
case 'thirdQuartile':
case 'complete':
if (typeof tmpOptions.tracking[eventType] === 'undefined') {
tmpOptions.tracking[eventType] = [];
}
if (typeof tmpOptions.stopTracking[eventType] === 'undefined') {
tmpOptions.stopTracking[eventType] = [];
}
tmpOptions.tracking[eventType].push(trackingEvents[i].childNodes[0].nodeValue);
tmpOptions.stopTracking[eventType] = false;
break;
case 'progress':
if (typeof tmpOptions.tracking[eventType] === 'undefined') {
tmpOptions.tracking[eventType] = [];
}
oneEventOffset = this.convertTimeStringToSeconds(trackingEvents[i].getAttribute('offset'));
if (typeof tmpOptions.tracking[eventType][oneEventOffset] === 'undefined') {
tmpOptions.tracking[eventType][oneEventOffset] = {
elements: [],
stopTracking: false
};
}
tmpOptions.tracking[eventType][oneEventOffset].elements.push(trackingEvents[i].childNodes[0].nodeValue);
break;
default:
break;
}
}
},
registerClickTracking: function(clickTrackingTag, tmpOptions) {
if(clickTrackingTag.length) {
for (var i = 0; i < clickTrackingTag.length; i++) {
if(clickTrackingTag[i] != ''){
tmpOptions.clicktracking.push(clickTrackingTag[i]);
}
}
}
},
registerImpressionEvents: function(impressionTags, tmpOptions) {
if (impressionTags.length) {
for (var i = 0; i < impressionTags.length; i++) {
var impressionEvent = this.extractNodeData(impressionTags[i]);
tmpOptions.impression.push(impressionEvent);
}
}
},
registerErrorEvents: function(errorTags, tmpOptions) {
if (
(typeof errorTags !== 'undefined') &&
(errorTags !== null) &&
(errorTags.length === 1) && //Only 1 Error tag is expected
(errorTags[0].childNodes.length === 1)
) {
tmpOptions.errorUrl = errorTags[0].childNodes[0].nodeValue;
}
},
announceError: function(code) {
if (
(typeof this.vastOptions.errorUrl === 'undefined') ||
!this.vastOptions.errorUrl
) {
return;
}
if (typeof(code) !== 'undefined') {
code = parseInt(code);
} else {
//Set a default code (900 Unidentified error)
code = 900;
}
var errorUrl = this.vastOptions.errorUrl.replace('[ERRORCODE]' , code);
//Send the error request
this.callUris([errorUrl]);
},
announceLocalError: function (code, msg) {
if (typeof(code) !== 'undefined') {
code = parseInt(code);
} else {
//Set a default code (900 Unidentified error)
code = 900;
}
message = '[Error] (' + code + '): ';
message += (!msg) ? 'Failed to load Vast' : msg;
console.log(message);
},
getClickTrackingEvents: function(linear) {
var result = [];
var videoClicks = linear.getElementsByTagName('VideoClicks');
if (videoClicks.length) {//There should be exactly 1 node
var clickTracking = videoClicks[0].getElementsByTagName('ClickTracking');
if (clickTracking.length) {
for (var i = 0; i < clickTracking.length; i++) {
var clickTrackingEvent = this.extractNodeData(clickTracking[i]);
result.push(clickTrackingEvent);
}
}
}
return result;
},
getNonLinearClickTrackingEvents: function (nonLinear) {
var result = [];
var nonLinears = nonLinear.getElementsByTagName('NonLinear');
if (nonLinears.length) {
var clickTracking = nonLinear.getElementsByTagName('NonLinearClickTracking');
if (clickTracking.length) {
for (var i = 0; i < clickTracking.length; i++) {
var NonLinearClickTracking = this.extractNodeData(clickTracking[i]);
result.push(NonLinearClickTracking);
}
}
}
return result;
},
callUris: function(uris) {
for (var i = 0; i < uris.length; i++) {
new Image().src = uris[i];
}
},
recalculateAdDimensions: function(idVideoPlayer) {
if ((!idVideoPlayer) && (typeof this.videoPlayerId !== 'undefined')) {
var idVideoPlayer = this.videoPlayerId;
}
var videoPlayer = document.getElementById(idVideoPlayer);
var divClickThrough = document.getElementById('vast_clickthrough_layer_' + idVideoPlayer);
if (divClickThrough) {
divClickThrough.style.width = videoPlayer.offsetWidth + 'px';
divClickThrough.style.height = videoPlayer.offsetHeight + 'px';
}
var requestFullscreenFunctionNames = this.checkFullscreenSupport('fluid_video_wrapper_' + idVideoPlayer);
var fullscreenButton = document.getElementById(idVideoPlayer + '_fluid_control_fullscreen');
var menuOptionFullscreen = document.getElementById(idVideoPlayer + 'context_option_fullscreen');
if (requestFullscreenFunctionNames) {
// this will go other way around because we alredy exited full screen
if (document[requestFullscreenFunctionNames.isFullscreen] === null) {
// Exit fullscreen
this.fullscreenOff(fullscreenButton, menuOptionFullscreen);
} else {
// Go fullscreen
this.fullscreenOn(fullscreenButton, menuOptionFullscreen);
}
} else {
//The browser does not support the Fullscreen API, so a pseudo-fullscreen implementation is used
if (fullscreenTag.className.search(/\bpseudo_fullscreen\b/g) !== -1) {
fullscreenTag.className += ' pseudo_fullscreen';
this.fullscreenOn(fullscreenButton, menuOptionFullscreen);
} else {
fullscreenTag.className = fullscreenTag.className.replace(/\bpseudo_fullscreen\b/g, '');
this.fullscreenOff(fullscreenButton, menuOptionFullscreen);
}
}
},
prepareVast: function (roll) {
var player = this;
var videoPlayerTag = document.getElementById(this.videoPlayerId);
var list = [];
list.length = 0;
list = player.findRoll(roll);
for (var i = 0; i < list.length; i++) {
var adListId = list[i];
if (player.adList[adListId].vastLoaded !== true && player.adList[adListId].error !== true) {
player.processUrl(player.adList[adListId].vastTag, adListId);
videoPlayerTag.addEventListener('adId_' + adListId, player[roll]);
}
}
},
toggleLoader: function(showLoader) {
var player = this;
if (showLoader) {
player.isLoading = true;
} else {
player.isLoading = false;
}
if (this.displayOptions.layoutControls.layout=== 'browser') {
//The browser handles all the layout of the video tag
return;
}
var loaderDiv = document.getElementById('vast_video_loading_' + this.videoPlayerId);
if (showLoader) {
loaderDiv.style.display = 'table';
} else {
loaderDiv.style.display = 'none';
}
},
sendRequest: function(url, withCredentials, timeout, functionReadyStateChange) {
var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.onreadystatechange = functionReadyStateChange;
xmlHttpReq.open('GET', url, true);
xmlHttpReq.withCredentials = withCredentials;
xmlHttpReq.timeout = timeout;
xmlHttpReq.send();
},
playMainVideoWhenVastFails: function(errorCode) {
var player = this;
var videoPlayerTag = document.getElementById(player.videoPlayerId);
videoPlayerTag.removeEventListener('loadedmetadata', player.switchPlayerToVastMode);
videoPlayerTag.pause();
player.toggleLoader(false);
player.displayOptions.vastOptions.vastAdvanced.noVastVideoCallback();
if (!player.vastOptions || typeof this.vastOptions.errorUrl === 'undefined') {
player.announceLocalError(errorCode);
} else {
player.announceError(errorCode);
}
player.switchToMainVideo();
},
switchPlayerToVastMode: function() {},
/**
* Process the XML response
*
* @param xmlResponse
* @param adListId
* @param tmpOptions
*/
processVastXml: function (xmlResponse, adListId, tmpOptions) {
var player = this;
if (!xmlResponse) {
player.stopProcessAndReportError(adListId);
return;
}
//Get impression tag
var impression = xmlResponse.getElementsByTagName('Impression');
if (impression !== null) {
player.registerImpressionEvents(impression, tmpOptions);
}
//Get the error tag, if any
var errorTags = xmlResponse.getElementsByTagName('Error');
if (errorTags !== null) {
player.registerErrorEvents(errorTags, tmpOptions);
}
//Get Creative
var creative = xmlResponse.getElementsByTagName('Creative');
//Currently only 1 creative and 1 linear is supported
if ((typeof creative !== 'undefined') && creative.length) {
var arrayCreativeLinears = creative[0].getElementsByTagName('Linear');
if ((typeof arrayCreativeLinears !== 'undefined') && (arrayCreativeLinears !== null) && arrayCreativeLinears.length) {
var creativeLinear = arrayCreativeLinears[0];
player.registerTrackingEvents(creativeLinear, tmpOptions);
var clickTracks = player.getClickTrackingEvents(creativeLinear);
player.registerClickTracking(clickTracks, tmpOptions);
//Extract the Ad data if it is actually the Ad (!wrapper)
if (!player.hasVastAdTagUri(xmlResponse) && player.hasInLine(xmlResponse)) {
//Set initial values
tmpOptions.adFinished = false;
tmpOptions.adType = 'linear';
//Extract the necessary data from the Linear node
tmpOptions.skipoffset = player.convertTimeStringToSeconds(creativeLinear.getAttribute('skipoffset'));
tmpOptions.clickthroughUrl = player.getClickThroughUrlFromLinear(creativeLinear);
tmpOptions.duration = player.getDurationFromLinear(creativeLinear);
tmpOptions.mediaFile = player.getMediaFileFromLinear(creativeLinear);
tmpOptions.iconClick = player.getIconClickThroughFromLinear(creativeLinear);
}
}
if ((typeof tmpOptions.iconClick !== 'undefined') && (tmpOptions.iconClick !== null) && tmpOptions.iconClick.length) {
player.adList[adListId].landingPage = tmpOptions.iconClick;
}
var arrayCreativeNonLinears = creative[0].getElementsByTagName('NonLinearAds');
if ((typeof arrayCreativeNonLinears !== 'undefined') && (arrayCreativeNonLinears !== null) && arrayCreativeNonLinears.length) {
var creativeNonLinear = arrayCreativeNonLinears[0];
player.registerTrackingEvents(creativeNonLinear, tmpOptions);
var clickTracks = player.getNonLinearClickTrackingEvents(creativeNonLinear);
player.registerClickTracking(clickTracks, tmpOptions);
//Extract the Ad data if it is actually the Ad (!wrapper)
if (!player.hasVastAdTagUri(xmlResponse) && player.hasInLine(xmlResponse)) {
//Set initial values
tmpOptions.adType = 'nonLinear';
//Extract the necessary data from the NonLinear node
tmpOptions.clickthroughUrl = player.getClickThroughUrlFromNonLinear(creativeNonLinear);
tmpOptions.duration = player.getDurationFromNonLinear(creativeNonLinear); // VAST version < 4.0
tmpOptions.dimension = player.getDimensionFromNonLinear(creativeNonLinear); // VAST version < 4.0
tmpOptions.staticResource = player.getStaticResourceFromNonLinear(creativeNonLinear);
tmpOptions.creativeType = player.getCreativeTypeFromStaticResources(creativeNonLinear);
}
}
//Extract the Ad data if it is actually the Ad (!wrapper)
if (!player.hasVastAdTagUri(xmlResponse) && player.hasInLine(xmlResponse)) {
player.adList[adListId].adType = tmpOptions.adType ? tmpOptions.adType : 'unknown';
if (typeof tmpOptions.mediaFile !== 'undefined' || typeof tmpOptions.staticResource !== 'undefined') {
player.adList[adListId].vastLoaded = true;
player.adPool[adListId] = Object.assign({}, tmpOptions);
var event = document.createEvent('Event');
event.initEvent('adId_' + adListId, false, true);
document.getElementById(player.videoPlayerId).dispatchEvent(event);
player.displayOptions.vastOptions.vastAdvanced.vastLoadedCallback();
} else {
player.stopProcessAndReportError(adListId);
}
}
} else {
player.stopProcessAndReportError(adListId);
}
},
/**
* Parse the VAST Tag
*
* @param vastTag
* @param adListId
*/
processUrl: function (vastTag, adListId) {
var player = this;
var numberOfRedirects = 0;
var tmpOptions = {
tracking: [],
stopTracking: [],
impression: [],
clicktracking: [],
vastLoaded: false
};
player.resolveVastTag(
vastTag,
numberOfRedirects,
adListId,
tmpOptions
);
},
resolveVastTag: function (vastTag, numberOfRedirects, adListId, tmpOptions) {
var player = this;
if(!vastTag || vastTag == '') {
player.stopProcessAndReportError(adListId);
return;
}
var handleXmlHttpReq = function () {
var xmlHttpReq = this;
if (xmlHttpReq.readyState === 4 && xmlHttpReq.status === 404) {
player.stopProcessAndReportError(adListId);
return;
}
if (xmlHttpReq.readyState === 4 && xmlHttpReq.status === 0) {
player.stopProcessAndReportError(adListId); //Most likely that Ad Blocker exists
return;
}
if (!((xmlHttpReq.readyState === 4) && (xmlHttpReq.status === 200))) {
return;
}
if ((xmlHttpReq.readyState === 4) && (xmlHttpReq.status !== 200)) {
player.stopProcessAndReportError(adListId);
return;
}
try {
var xmlResponse = xmlHttpReq.responseXML;
} catch (e) {
player.stopProcessAndReportError(adListId);
return;
}
if (!xmlResponse) {
player.stopProcessAndReportError(adListId);
return;
}
player.inLineFound = player.hasInLine(xmlResponse);
if (!player.inLineFound && player.hasVastAdTagUri(xmlResponse)) {
var vastAdTagUri = player.getVastAdTagUriFromWrapper(xmlResponse);
if (vastAdTagUri) {
player.resolveVastTag(vastAdTagUri, numberOfRedirects, adListId, tmpOptions);
} else {
player.stopProcessAndReportError(adListId);
return;
}
}
if (numberOfRedirects > player.displayOptions.vastOptions.maxAllowedVastTagRedirects && !player.inLineFound) {
player.stopProcessAndReportError(adListId);
return;
}
player.processVastXml(xmlResponse, adListId, tmpOptions);
};
if (numberOfRedirects <= player.displayOptions.vastOptions.maxAllowedVastTagRedirects) {
player.sendRequest(
vastTag,
true,
player.displayOptions.vastOptions.vastTimeout,
handleXmlHttpReq
);
}
numberOfRedirects++;
},
/**
* Helper function to stop processing
*
* @param adListId
*/
stopProcessAndReportError: function(adListId) {
var player = this;
//Set the error flag for the Ad
player.adList[adListId].error = true;
//The response returned an error. Proceeding with the main video.