-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
script.js
278 lines (258 loc) · 10.9 KB
/
script.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
//preloader
$(document).ready(function() {
preloaderFadeOutTime = 500;
function hidePreloader() {
var preloader = $('.spinner-wrapper');
preloader.fadeOut(preloaderFadeOutTime);
}
hidePreloader();
});
const form = document.querySelector(".input_video_here");
const fileInput = document.querySelector(".file-input");
const uploadedArea = document.querySelector(".uploaded-area");
const file_input_text = document.querySelector(".input_type");
const show_video_btn = document.querySelectorAll(".field");
const ad_times = document.querySelector("#ad_times_shown");
const play_replay_btn = document.getElementById("play_button");
var main_video;
var ad_video;
var no_of_times_ads_to_play = 0;
var video_count = 0;
var play_ad_at = 0;
var string_of_ad_time = [];
var ad_played = 0;
var is_listener_enabled = false;
var is_last_ad_done = false;
var is_replayed_button = false;
function get_ad_positions(video_duration, no_ads) {
var ads_positions = [];
while (ads_positions.length < no_ads) {
var random_number = Math.floor(Math.random()*video_duration);
if (ads_positions.indexOf(random_number) == -1) {
ads_positions.push(random_number);
}
};
return ads_positions;
};
function compare(a, b) {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
}
ad_times.addEventListener('input', function(e) {
if (e.target.value == 0) {
document.querySelector(".submit_btn").innerText = "Skip";
} else if (e.target.value.length > 0) {
document.querySelector(".submit_btn").innerText = "Continue";
} else {
document.querySelector(".submit_btn").innerText = "Skip";
}
});
ad_times.addEventListener('keypress', function(e) {
if (e.keyCode == 13) document.querySelector(".submit_btn").click();
});
form.addEventListener("click", () =>{
fileInput.click();
});
fileInput.onchange = ({target})=>{
let file = target.files[0];
if(file){
let fileName = file.name;
if(fileName.length >= 12){
let splitName = fileName.split('.');
fileName = splitName[0].substring(0, 13) + "... ." + splitName[1];
}
video_count++;
file_input_text.innerHTML = "Upload The Advertisement Video";
if (video_count >= 2) {
ad_video = URL.createObjectURL(target.files[0]);
form.remove();
show_video_btn[1].classList.remove("hide");
play_replay_btn.classList.remove("hide");
file_input_text.innerHTML = "Click on the Play Button to get the video.";
} else if (video_count == 1) {
main_video = URL.createObjectURL(target.files[0]);
}
}
}
document.querySelector(".submit_btn").addEventListener("click", function() {
show_video_btn[0].classList.add("hide");
show_video_btn[1].classList.add("hide");
if (ad_times.value == null || ad_times.value == '') {
no_of_times_ads_to_play = 0;
} else if (ad_times.value < 0) {
no_of_times_ads_to_play = 0;
} else {
no_of_times_ads_to_play = ad_times.value;
}
document.querySelector(".submit_btn").classList.add("hide");
document.querySelector(".wrapper").classList.remove("hide");
active_play_button();
});
function next_ad_time() {
ad_played = ad_played+1;
return string_of_ad_time[ad_played-1];
}
function previous_ad_time() {
return string_of_ad_time[ad_played-1];
}
//var play_main_video =
function play_main_video() {
return new Promise(function(resolve,reject){
document.getElementById("video_html5_api").src = main_video;
videojs("video").currentTime(previous_ad_time());
document.querySelector(".vjs-control-bar").style.display = "flex";
var Z = setInterval(function(){
if (document.getElementById("video_html5_api").readyState > 0) {
clearInterval(Z);
resolve("Ok");
}
}, 100);
})
};
function play_ad_video (){
return new Promise(function(resolve,reject){
document.getElementById("video_html5_api").src = ad_video;
document.querySelector(".vjs-control-bar").style.display = "none";
var X = setInterval(function(){
if (document.getElementById("video_html5_api").readyState > 0) {
clearInterval(X);
resolve("Ok");
}
}, 100);
})
};
function enable_listener() {
if (is_listener_enabled == false){
document.getElementById("video_html5_api").addEventListener('ended', function() {
if ((ad_played < parseInt(no_of_times_ads_to_play)) == true) {
//console.log(ad_played,no_of_times_ads_to_play,(ad_played < parseInt(no_of_times_ads_to_play)));
play_main_video().then(res => {
videojs("video").play();
file_input_text.innerHTML = `Main video is resumed.`;
});
} else {
document.getElementById("video_html5_api").removeEventListener('ended', function() {
if ((ad_played < parseInt(no_of_times_ads_to_play)) == true) {
//console.log("ji",ad_played,no_of_times_ads_to_play,(ad_played < parseInt(no_of_times_ads_to_play)));
play_main_video().then(res => {
videojs("video").play();
file_input_text.innerHTML = `Main video is resumed.`;
});
}
});
if (is_last_ad_done == false) {
play_main_video().then(res => {
videojs("video").play();
file_input_text.innerHTML = `Main video is resumed.`;
is_last_ad_done = true;
});
}
}
});
is_listener_enabled = true;
}
}
// vjs-play-control vjs-control vjs-button vjs-paused vjs-ended
function show_ad(play_ad_at) {
return new Promise(function(resolve,reject){
var video = document.getElementById("video_html5_api");
//console.log(play_ad_at);
var played_video = setInterval(function(){
if (videojs("video").currentTime() >= play_ad_at) {
videojs("video").pause();
file_input_text.innerHTML = `Advertisement video is being played`;
play_ad_video().then(resp => {
videojs("video").play();
});
enable_listener();
clearInterval(played_video);
resolve("Ok");
} else if ((play_ad_at - videojs("video").currentTime()) <= 10) {
file_input_text.innerHTML = `Advertisement video will begin in ${Math.floor(play_ad_at - videojs("video").currentTime())}`
}
}, 500);
});
}
function active_play_button() {
play_replay_btn.addEventListener("click", function() {
is_last_ad_done = false;
play_replay_btn.innerText = "Replay";
//show_video_btn[1].classList.remove("hide");
show_video_btn[1].classList.add("hide");
file_input_text.innerHTML = "Enjoy the Video.";
var video = document.querySelector("source");
video.src = main_video;
var s = document.createElement('script');
s.src = 'https://vjs.zencdn.net/7.18.1/video.min.js';
document.body.appendChild(s);
document.getElementById("video").style.display = "block";
var k = setInterval(function(){
var player = videojs('video');
if (document.getElementById("video_html5_api").readyState > 0) {
videojs("video").play();
var main_video_length = document.getElementById("video_html5_api").duration;
string_of_ad_time = get_ad_positions(main_video_length,no_of_times_ads_to_play);
string_of_ad_time.sort(compare);
//console.log(string_of_ad_time);
clearInterval(k);
videojs("video");
string_of_ad_time.forEach(play_ad_at => {
show_ad(play_ad_at).then(res => {
//console.log("hh");
ad_played++;
});
});
};
}, 200);
});
}
var checking = setInterval(function(){
if (document.querySelectorAll(".vjs-ended")[1] != null) {
is_replayed_button = false;
document.querySelectorAll(".vjs-ended")[1].addEventListener("click", function() {
if (document.querySelectorAll(".vjs-ended")[1] != null && document.querySelectorAll(".vjs-ended")[1].title == 'Replay') {
if (is_replayed_button == false) {
is_replayed_button = true;
is_last_ad_done = false;
ad_played = 0;
play_ad_at = 0;
string_of_ad_time = [];
is_listener_enabled = false;
play_replay_btn.innerText = "Replay";
//show_video_btn[1].classList.remove("hide");
show_video_btn[1].classList.add("hide");
file_input_text.innerHTML = "Video is replaying from the starting with new Ad timing.";
var video = document.querySelector("source");
video.src = main_video;
var s = document.createElement('script');
s.src = 'https://vjs.zencdn.net/7.18.1/video.min.js';
document.body.appendChild(s);
document.getElementById("video").style.display = "block";
var k = setInterval(function(){
var player = videojs('video');
if (document.getElementById("video_html5_api").readyState > 0) {
videojs("video").play();
var main_video_length = document.getElementById("video_html5_api").duration;
string_of_ad_time = get_ad_positions(main_video_length,no_of_times_ads_to_play);
string_of_ad_time.sort(compare);
//console.log(string_of_ad_time);
clearInterval(k);
videojs("video");
string_of_ad_time.forEach(play_ad_at => {
show_ad(play_ad_at).then(res => {
//console.log("hh");
ad_played++;
});
});
};
}, 200);
}
}
})
}
}, 1000)