Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ---------------------------------------------------------------------------------------------------
- // Ads
- // ---------------------------------------------------------------------------------------------------
- /**
- * Injects an OK Ads Widget to a game's page
- *
- * @param {string} [frameId] optional frame element id. If not present "ok-ads-frame" id will be used
- * @param {function} [secretKey] callbackFunction used for all ad methods. Takes a single object input parameter
- */
- function injectAdsWidget(frameId, callbackFunction) {
- if (ads_state.frame_element) {
- return;
- }
- var frame = document.createElement("iframe");
- var framesCount = window.frames.length;
- frame.id = frameId || "ok-ads-frame";
- frame.src = getAdsWidgetSrc();
- for (var prop in ads_widget_style) {
- frame.style[prop] = ads_widget_style[prop];
- }
- frame.style.display = "none";
- document.body.appendChild(frame);
- ads_state.frame_element = frame;
- ads_state.window_frame = window.frames[framesCount];
- var callback = callbackFunction || defaultAdCallback;
- window.addEventListener('message', callback);
- }
- /**
- * Requests an ad to be shown for a user from ad providers
- */
- function prepareMidroll() {
- if (!ads_state.window_frame) {
- console.log("Ads are not initialized. Please initialize them first");
- return;
- }
- ads_state.window_frame.postMessage(JSON.stringify({method: 'prepare', arguments: ['midroll']}), '*');
- }
- /**
- * Shows previously prepared ad to a user
- */
- function showMidroll() {
- if (!ads_state.window_frame) {
- console.log("Ads are not initialized. Please initialize them first");
- return;
- }
- ads_state.frame_element.style.display = '';
- setTimeout(function(){
- ads_state.window_frame.postMessage(JSON.stringify({method: 'show'}), '*');
- }, 10);
- }
- /**
- * Removed an Ok Ads Widget from page source and completely resets ads status
- */
- function removeAdsWidget() {
- if (ads_state.frame_element) {
- ads_state.frame_element.parentNode.removeChild(ads_state.frame_element);
- ads_state = {
- init: false,
- ready: false
- };
- }
- }
- /**
- * Generates an URL for OK Ads Widget
- */
- function getAdsWidgetSrc() {
- var sig = md5("call_id=1" + state.sessionSecretKey).toString();
- 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;
- return widgetSrc;
- }
- /**
- * Default callback function used for OK Ads Widget
- */
- function defaultAdCallback(message) {
- var data = JSON.parse(message.data);
- if (!data.call || !data.call.method) {
- return;
- }
- switch (data.call.method) {
- case "init":
- if (data.result.status === "ok") {
- console.log("OK Ads initialization complete");
- ads_state.init = true;
- } else {
- console.log("OK Ads failed to initialize");
- ads_state.init = false;
- }
- break;
- case "prepare":
- if (data.result.status === "ok") {
- if (data.result.code === "ready") {
- console.log("Ad is ready to be shown");
- ads_state.ready = true;
- }
- } else {
- console.log("Ad is not ready to be shown. Status: " + data.result.status + ". Code: " + data.result.code);
- ads_state.ready = false;
- }
- break;
- case "show":
- ads_state.frame_element.style.display = "none";
- if (data.result.status === "ok") {
- if (data.result.code === "complete") {
- console.log("Ad is successfully shown");
- ads_state.ready = false;
- }
- } else {
- console.log("An add can't be shown. Status: " + data.result.status + ". Code: " + data.result.code)
- ads_state.ready = false;
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement