· 7 years ago · Jan 18, 2019, 07:30 PM
1 // ---------------------------------------------------------------------------------------------------
2 // Ads
3 // ---------------------------------------------------------------------------------------------------
4
5 /**
6 * Injects an OK Ads Widget to a game's page
7 *
8 * @param {string} [frameId] optional frame element id. If not present "ok-ads-frame" id will be used
9 * @param {function} [secretKey] callbackFunction used for all ad methods. Takes a single object input parameter
10 */
11 function injectAdsWidget(frameId, callbackFunction) {
12 if (ads_state.frame_element) {
13 return;
14 }
15 var frame = document.createElement("iframe");
16 var framesCount = window.frames.length;
17 frame.id = frameId || "ok-ads-frame";
18
19 frame.src = getAdsWidgetSrc();
20 for (var prop in ads_widget_style) {
21 frame.style[prop] = ads_widget_style[prop];
22 }
23 frame.style.display = "none";
24 document.body.appendChild(frame);
25 ads_state.frame_element = frame;
26 ads_state.window_frame = window.frames[framesCount];
27
28 var callback = callbackFunction || defaultAdCallback;
29 window.addEventListener('message', callback);
30 }
31
32 /**
33 * Requests an ad to be shown for a user from ad providers
34 */
35 function prepareMidroll() {
36 if (!ads_state.window_frame) {
37 console.log("Ads are not initialized. Please initialize them first");
38 return;
39 }
40 ads_state.window_frame.postMessage(JSON.stringify({method: 'prepare', arguments: ['midroll']}), '*');
41 }
42
43 /**
44 * Shows previously prepared ad to a user
45 */
46 function showMidroll() {
47 if (!ads_state.window_frame) {
48 console.log("Ads are not initialized. Please initialize them first");
49 return;
50 }
51 ads_state.frame_element.style.display = '';
52 setTimeout(function(){
53 ads_state.window_frame.postMessage(JSON.stringify({method: 'show'}), '*');
54 }, 10);
55 }
56
57 /**
58 * Removed an Ok Ads Widget from page source and completely resets ads status
59 */
60 function removeAdsWidget() {
61 if (ads_state.frame_element) {
62 ads_state.frame_element.parentNode.removeChild(ads_state.frame_element);
63 ads_state = {
64 init: false,
65 ready: false
66 };
67 }
68 }
69
70 /**
71 * Generates an URL for OK Ads Widget
72 */
73 function getAdsWidgetSrc() {
74 var sig = md5("call_id=1" + state.sessionSecretKey).toString();
75 var widgetSrc = state.widgetServer + "dk?st.cmd=WidgetVideoAdv&st.app=" + state.app_id + "&st.sig=" + sig + "&st.call_id=1&st.session_key=" + state.sessionKey;
76 return widgetSrc;
77 }
78
79 /**
80 * Default callback function used for OK Ads Widget
81 */
82 function defaultAdCallback(message) {
83 var data = JSON.parse(message.data);
84
85 if (!data.call || !data.call.method) {
86 return;
87 }
88
89 switch (data.call.method) {
90 case "init":
91 if (data.result.status === "ok") {
92 console.log("OK Ads initialization complete");
93 ads_state.init = true;
94 } else {
95 console.log("OK Ads failed to initialize");
96 ads_state.init = false;
97 }
98 break;
99 case "prepare":
100 if (data.result.status === "ok") {
101 if (data.result.code === "ready") {
102 console.log("Ad is ready to be shown");
103 ads_state.ready = true;
104 }
105 } else {
106 console.log("Ad is not ready to be shown. Status: " + data.result.status + ". Code: " + data.result.code);
107 ads_state.ready = false;
108 }
109 break;
110 case "show":
111 ads_state.frame_element.style.display = "none";
112 if (data.result.status === "ok") {
113 if (data.result.code === "complete") {
114 console.log("Ad is successfully shown");
115 ads_state.ready = false;
116 }
117 } else {
118 console.log("An add can't be shown. Status: " + data.result.status + ". Code: " + data.result.code)
119 ads_state.ready = false;
120 }
121 break;
122 }
123 }