Roller.js
7.81 KB
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
(function () {
var adsManager;
var adsLoader;
var adDisplayContainer;
var intervalTimer;
var videoContent;
var adTagUrl;
var adContainerEl;
var loadingMessageEl;
window.roller = function (el, tagUrl) {
videoContent = el;
adTagUrl = tagUrl;
// Play nicely with ad blockers.
if (typeof google === 'undefined') {
return;
}
setUpIMA();
return {
playAds: playAds,
destroy: destroy,
};
};
function setUpIMA() {
// Create the ad display container.
createAdDisplayContainer();
// Create ads loader.
adsLoader = new google.ima.AdsLoader(adDisplayContainer);
// Enabled VPAID ads.
adsLoader.getSettings().setVpaidMode(google.ima.ImaSdkSettings.VpaidMode.ENABLED);
// Listen and respond to ads loaded and error events.
adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
onAdsManagerLoaded,
false);
adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdLoaderError,
false);
// Request video ads.
var adsRequest = new google.ima.AdsRequest();
adsRequest.adTagUrl = adTagUrl;
// Specify the linear and nonlinear slot sizes. This helps the SDK to
// select the correct creative if multiple are returned.
adsRequest.linearAdSlotWidth = parseInt(getComputedStyle(videoContent.el).width, 10);
adsRequest.linearAdSlotHeight = parseInt(getComputedStyle(videoContent.el).height, 10);
adsRequest.nonLinearAdSlotWidth = getComputedStyle(videoContent.el).width;
adsRequest.nonLinearAdSlotHeight = getComputedStyle(videoContent.el).height / 3;
adsLoader.requestAds(adsRequest);
}
function createAdDisplayContainer() {
adContainerEl = $('<div id="adContainer" style="background: #000; display:none; width: 100%; height: 100%; top: 0; left: 0; position: absolute;"></div>').insertAfter(videoContent.el)[0];
loadingMessageEl = $('<div style="position: absolute; bottom: 10px; left: 15px; z-index: 99999;">Loading Advertisement ...</div>').appendTo(adContainerEl);
adDisplayContainer = new google.ima.AdDisplayContainer(adContainerEl);
}
function playAds() {
if (!adsManager) {
return onContentResumeRequested();
}
// Initialize the container. Must be done via a user action on mobile devices.
adDisplayContainer.initialize();
// We want to show ad container now because it might take a while to
// load the ad while user thinks the player just stopped.
showAdsContainer();
try {
// Initialize the ads manager. Ad rules playlist will start at this time.
adsManager.init(parseInt(getComputedStyle(videoContent.el).width, 10), parseInt(getComputedStyle(videoContent.el).height, 10), google.ima.ViewMode.NORMAL);
// Call play to start showing the ad. Single video and overlay ads will
// start at this time; the call will be ignored for ad rules.
adsManager.start();
} catch (adError) {
onAdError(adError);
}
}
function onAdsManagerLoaded(adsManagerLoadedEvent) {
console.log('LOADED MANAGER');
// Get the ads manager.
var adsRenderingSettings = new google.ima.AdsRenderingSettings();
adsRenderingSettings.loadVideoTimeout = 10000;
// videoContent should be set to the content video element.
adsManager = adsManagerLoadedEvent.getAdsManager({
duration: 120,
currentTime: 0,
}, adsRenderingSettings
);
// Add listeners to the required events.
adsManager.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdError);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,
onContentPauseRequested);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,
onContentResumeRequested);
adsManager.addEventListener(
google.ima.AdEvent.Type.ALL_ADS_COMPLETED,
onAdEvent);
// Listen to any additional events, if necessary.
adsManager.addEventListener(
google.ima.AdEvent.Type.LOADED,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.STARTED,
onAdEvent);
adsManager.addEventListener(
google.ima.AdEvent.Type.COMPLETE,
onAdEvent);
}
function onAdEvent(adEvent) {
console.log(adEvent);
// Retrieve the ad from the event. Some events (e.g. ALL_ADS_COMPLETED)
// don't have ad object associated.
var ad = adEvent.getAd();
switch (adEvent.type) {
case google.ima.AdEvent.Type.LOADED:
// This is the first event sent for an ad - it is possible to
// determine whether the ad is a video ad or an overlay.
if (!ad.isLinear()) {
// Position AdDisplayContainer correctly for overlay.
// Use ad.width and ad.height.
videoContent.play();
hideAdsContainer();
}
break;
case google.ima.AdEvent.Type.STARTED:
loadingMessageEl.remove();
// This event indicates the ad has started - the video player
// can adjust the UI, for example display a pause button and
// remaining time.
if (ad.isLinear()) {
// For a linear ad, a timer can be started to poll for
// the remaining time.
intervalTimer = setInterval(
function () {
var remainingTime = adsManager.getRemainingTime();
},
300); // every 300ms
}
break;
case google.ima.AdEvent.Type.COMPLETE:
// This event indicates the ad has finished - the video player
// can perform appropriate UI actions, such as removing the timer for
// remaining time detection.
if (ad.isLinear()) {
clearInterval(intervalTimer);
}
break;
}
}
function onAdLoaderError(adErrorEvent) {
console.log(adErrorEvent.getError());
}
function onAdError(adErrorEvent) {
/**
* We use this handler for all errors and sometimes they are not
* google.ima.AdErrorEvent type and they don't have .getError() method.
*/
console.log((adErrorEvent.getError && adErrorEvent.getError()) || adErrorEvent);
onContentResumeRequested();
if (adsManager) {
adsManager.destroy();
}
}
function onContentPauseRequested() {
showAdsContainer();
videoContent.pause();
}
function onContentResumeRequested() {
hideAdsContainer();
videoContent.play();
}
function showAdsContainer() {
console.log('SHOW');
adContainerEl.style.display = 'block';
}
function hideAdsContainer() {
console.log('HIDE');
adContainerEl.style.display = 'none';
}
function destroy() {
if (adDisplayContainer) {
adDisplayContainer.destroy();
}
if (adsLoader) {
adsLoader.destroy();
}
if (adsManager) {
adsManager.destroy();
adsManager = undefined;
}
if (adContainerEl.parentNode) {
adContainerEl.parentNode.removeChild(adContainerEl);
}
}
}());