@smoud/playable-sdk 1.0.14 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -330,20 +330,20 @@ sdk.off('eventName', [callback], [context]);
330
330
 
331
331
  #### Available Events
332
332
 
333
- | Event | Parameters | Description | Typical Usage |
334
- | ------------- | --------------- | ------------------------------------------ | ------------------------ |
335
- | `init` | - | SDK initialization started | Setup loading screen |
336
- | `boot` | - | Ad container is ready `pre` init callback | Initialize core systems |
337
- | `ready` | - | Ad container is ready `post` init callback | Start resource loading |
338
- | `start` | - | Playable experience started | Begin gameplay/animation |
339
- | `interaction` | `count` | User interaction occurred | Track engagement |
340
- | `resize` | `width, height` | Container size changed | Update layout |
341
- | `pause` | - | Playable entered pause state | Pause gameplay |
342
- | `resume` | - | Playable resumed from pause | Resume gameplay |
343
- | `volume` | `level` | Volume level changed (0-1) | Adjust audio |
344
- | `retry` | - | Retry/restart triggered | Reset game state |
345
- | `finish` | - | Playable marked as complete | Show end screen |
346
- | `install` | - | Install action triggered | Track conversion |
333
+ | Event | Parameters | Description | Typical Usage |
334
+ | ------------- | --------------- | ------------------------------------------------- | ------------------------ |
335
+ | `init` | - | DOM Content loaded and SDK initialization started | Setup loading screen |
336
+ | `boot` | - | Ad container is ready `pre` init callback | Initialize core systems |
337
+ | `ready` | - | Ad container is ready `post` init callback | Start resource loading |
338
+ | `start` | - | Playable experience started | Begin gameplay/animation |
339
+ | `interaction` | `count` | User interaction occurred | Track engagement |
340
+ | `resize` | `width, height` | Container size changed | Update layout |
341
+ | `pause` | - | Playable entered pause state | Pause gameplay |
342
+ | `resume` | - | Playable resumed from pause | Resume gameplay |
343
+ | `volume` | `level` | Volume level changed (0-1) | Adjust audio |
344
+ | `retry` | - | Retry/restart triggered | Reset game state |
345
+ | `finish` | - | Playable marked as complete | Show end screen |
346
+ | `install` | - | Install action triggered | Track conversion |
347
347
 
348
348
  ### Properties
349
349
 
package/dist/esm/index.js CHANGED
@@ -160,150 +160,152 @@ function handleResume() {
160
160
  sdk.isPaused = false;
161
161
  emitEvent("resume");
162
162
  }
163
- function mraidIsViewable(viewable) {
163
+ function fireResizeEvent(width, height) {
164
+ handleResize(width, height);
165
+ }
166
+ function startMraidProtocol() {
164
167
  if ("mraid" !== AD_PROTOCOL) return;
165
- if (viewable) {
166
- if (sdk.isReady) {
167
- handleResume();
168
- } else {
169
- if (AD_NETWORK == "ironsource") {
168
+ if (isProtocolInitialized) return;
169
+ mraid.removeEventListener("ready", startMraidProtocol);
170
+ function mraidStateChanged(viewable) {
171
+ if (viewable) {
172
+ if (sdk.isReady) {
173
+ handleResume();
174
+ } else {
170
175
  const maxSize = mraid.getMaxSize();
171
176
  sdk.maxWidth = Math.floor(maxSize.width);
172
177
  sdk.maxHeight = Math.floor(maxSize.height);
178
+ bootAd();
173
179
  }
174
- bootAd();
180
+ } else {
181
+ handlePause();
175
182
  }
176
- } else {
177
- handlePause();
178
183
  }
179
- }
180
- function dapiIsViewable(event) {
181
- if ("dapi" !== AD_PROTOCOL) return;
182
- if (event.isViewable) {
183
- if (sdk.isReady) {
184
- handleResume();
184
+ function isViewable() {
185
+ return mraid.isViewable() && "hidden" !== mraid.getState();
186
+ }
187
+ function stateChanged() {
188
+ mraidStateChanged(isViewable());
189
+ }
190
+ mraid.addEventListener("viewableChange", stateChanged);
191
+ mraid.addEventListener("stateChange", stateChanged);
192
+ if (isViewable()) {
193
+ mraidStateChanged(true);
194
+ }
195
+ if (mraid.getAudioVolume) {
196
+ const isAudioEnabled = mraid.getAudioVolume();
197
+ if (isAudioEnabled) {
198
+ handleVolumeChange(1);
185
199
  } else {
186
- const screenSize = dapi.getScreenSize();
187
- screenSize.width = Math.floor(screenSize.width);
188
- screenSize.height = Math.floor(screenSize.height);
189
- bootAd();
200
+ handleVolumeChange(0);
190
201
  }
191
- } else {
192
- handlePause();
193
202
  }
194
- }
195
- function fireResizeEvent(width, height) {
196
- handleResize(width, height);
197
- }
198
- function startMraidProtocol() {
199
- if ("mraid" !== AD_PROTOCOL) return;
200
- if (!isProtocolInitialized) {
201
- mraid.removeEventListener("ready", startMraidProtocol);
202
- mraid.addEventListener("viewableChange", function() {
203
- const isViewable = mraid.isViewable();
204
- mraidIsViewable(isViewable);
205
- });
206
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
207
- mraidIsViewable(true);
208
- }
209
- if (mraid.getAudioVolume) {
210
- const isAudioEnabled = mraid.getAudioVolume();
211
- if (isAudioEnabled) {
203
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
204
+ if (newVolume !== null) {
205
+ if (newVolume > 0) {
212
206
  handleVolumeChange(1);
213
207
  } else {
214
208
  handleVolumeChange(0);
215
209
  }
216
210
  }
217
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
218
- if (newVolume !== null) {
219
- if (newVolume > 0) {
220
- handleVolumeChange(1);
221
- } else {
222
- handleVolumeChange(0);
223
- }
224
- }
225
- });
226
- mraid.addEventListener("error", function(t, e) {
227
- console.log("mraid error: " + t + " action: " + e);
228
- });
229
- mraid.addEventListener("sizeChange", function() {
230
- const maxSize = mraid.getMaxSize();
231
- fireResizeEvent(maxSize.width, maxSize.height);
232
- });
233
- isProtocolInitialized = true;
234
- }
211
+ });
212
+ mraid.addEventListener("error", function(t, e) {
213
+ console.log("mraid error: " + t + " action: " + e);
214
+ });
215
+ mraid.addEventListener("sizeChange", function() {
216
+ const maxSize = mraid.getMaxSize();
217
+ fireResizeEvent(maxSize.width, maxSize.height);
218
+ });
219
+ isProtocolInitialized = true;
235
220
  }
236
221
  function startDapiProtocol() {
237
222
  if ("dapi" !== AD_PROTOCOL) return;
238
- if (!isProtocolInitialized) {
239
- dapi.removeEventListener("ready", startDapiProtocol);
240
- const isAudioEnabled = dapi.getAudioVolume();
241
- if (isAudioEnabled) {
223
+ if (isProtocolInitialized) return;
224
+ function dapiIsViewable(event) {
225
+ if (event.isViewable) {
226
+ if (sdk.isReady) {
227
+ handleResume();
228
+ } else {
229
+ const screenSize = dapi.getScreenSize();
230
+ screenSize.width = Math.floor(screenSize.width);
231
+ screenSize.height = Math.floor(screenSize.height);
232
+ bootAd();
233
+ }
234
+ } else {
235
+ handlePause();
236
+ }
237
+ }
238
+ dapi.removeEventListener("ready", startDapiProtocol);
239
+ const isAudioEnabled = dapi.getAudioVolume();
240
+ if (isAudioEnabled) {
241
+ handleVolumeChange(1);
242
+ } else {
243
+ handleVolumeChange(0);
244
+ }
245
+ dapi.addEventListener("audioVolumeChange", function(volume) {
246
+ const isAudioEnabled2 = !!volume;
247
+ if (isAudioEnabled2) {
242
248
  handleVolumeChange(1);
243
249
  } else {
244
250
  handleVolumeChange(0);
245
251
  }
246
- dapi.addEventListener("audioVolumeChange", function(volume) {
247
- const isAudioEnabled2 = !!volume;
248
- if (isAudioEnabled2) {
249
- handleVolumeChange(1);
250
- } else {
251
- handleVolumeChange(0);
252
- }
253
- });
254
- dapi.addEventListener("adResized", function(event) {
255
- const maxSize = dapi.getScreenSize();
256
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
257
- });
258
- if (dapi.isViewable()) {
259
- dapiIsViewable({ isViewable: true });
260
- }
261
- dapi.addEventListener("viewableChange", dapiIsViewable);
262
- isProtocolInitialized = true;
252
+ });
253
+ dapi.addEventListener("adResized", function(event) {
254
+ const maxSize = dapi.getScreenSize();
255
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
256
+ });
257
+ if (dapi.isViewable()) {
258
+ dapiIsViewable({ isViewable: true });
263
259
  }
260
+ dapi.addEventListener("viewableChange", dapiIsViewable);
261
+ isProtocolInitialized = true;
264
262
  }
265
263
  function startDefaultProtocol() {
266
- if (!isProtocolInitialized) {
267
- if ("mintegral" === AD_NETWORK) {
268
- window.mintGameStart = function() {
269
- handleResume();
270
- handleResize(sdk.maxWidth, sdk.maxHeight);
271
- };
272
- window.mintGameClose = function() {
273
- handlePause();
274
- };
275
- }
276
- document.addEventListener("visibilitychange", () => {
277
- if (document.visibilityState === "visible") {
264
+ if (isProtocolInitialized) return;
265
+ if ("mintegral" === AD_NETWORK) {
266
+ window.mintGameStart = function() {
267
+ handleResume();
268
+ handleResize(sdk.maxWidth, sdk.maxHeight);
269
+ };
270
+ window.mintGameClose = function() {
271
+ handlePause();
272
+ };
273
+ }
274
+ document.addEventListener("visibilitychange", () => {
275
+ if (document.visibilityState === "visible") {
276
+ if (sdk.isReady) {
278
277
  handleResume();
279
278
  } else {
280
- handlePause();
279
+ bootAd();
281
280
  }
282
- });
283
- if (document.readyState === "complete") {
284
- bootAd();
285
281
  } else {
286
- window.addEventListener("load", () => {
287
- bootAd();
288
- });
282
+ handlePause();
289
283
  }
290
- window.addEventListener("resize", function() {
291
- handleResize();
292
- });
293
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
294
- const tapjoyApi = {
295
- skipAd: function() {
296
- try {
297
- sdk.finish();
298
- } catch (e) {
299
- console.warn("Could not skip ad! | " + e);
300
- }
284
+ });
285
+ function documentIsReady() {
286
+ if (document.visibilityState === "visible") bootAd();
287
+ }
288
+ if (document.readyState === "complete") {
289
+ documentIsReady();
290
+ } else {
291
+ window.addEventListener("load", documentIsReady);
292
+ }
293
+ window.addEventListener("resize", function() {
294
+ handleResize();
295
+ });
296
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
297
+ const tapjoyApi = {
298
+ skipAd: function() {
299
+ try {
300
+ sdk.finish();
301
+ } catch (e) {
302
+ console.warn("Could not skip ad! | " + e);
301
303
  }
302
- };
303
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
304
- }
305
- isProtocolInitialized = true;
304
+ }
305
+ };
306
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
306
307
  }
308
+ isProtocolInitialized = true;
307
309
  }
308
310
  function finishTapjoy() {
309
311
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -333,6 +335,18 @@ function registerTouchHandlers() {
333
335
  isTouchEventRegistered = true;
334
336
  }
335
337
  }
338
+ function initSDK() {
339
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
340
+ ensureProtocol();
341
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
342
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
343
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
344
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
345
+ } else {
346
+ startDefaultProtocol();
347
+ }
348
+ emitEvent("init");
349
+ }
336
350
  var _sdk = class _sdk {
337
351
  /**
338
352
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -354,23 +368,8 @@ var _sdk = class _sdk {
354
368
  */
355
369
  static init(callback) {
356
370
  if (isSDKInitialized) return;
357
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
358
- ensureProtocol();
359
371
  if (callback) initCallback = callback;
360
- if ("mraid" === AD_PROTOCOL && isMraid()) {
361
- if (mraid.getState() !== "ready") {
362
- mraid.addEventListener("ready", startMraidProtocol);
363
- } else {
364
- startMraidProtocol();
365
- }
366
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
367
- window.addEventListener("load", function() {
368
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
369
- });
370
- } else {
371
- startDefaultProtocol();
372
- }
373
- emitEvent("init");
372
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
374
373
  isSDKInitialized = true;
375
374
  }
376
375
  /**
@@ -595,7 +594,7 @@ var _sdk = class _sdk {
595
594
  }
596
595
  };
597
596
  /** Current version of the SDK */
598
- _sdk.version = "1.0.14";
597
+ _sdk.version = "1.0.16";
599
598
  /** Current maximum width of the playable ad container in pixels */
600
599
  _sdk.maxWidth = Math.floor(window.innerWidth);
601
600
  /** Current maximum height of the playable ad container in pixels */
@@ -162,150 +162,152 @@
162
162
  sdk.isPaused = false;
163
163
  emitEvent("resume");
164
164
  }
165
- function mraidIsViewable(viewable) {
165
+ function fireResizeEvent(width, height) {
166
+ handleResize(width, height);
167
+ }
168
+ function startMraidProtocol() {
166
169
  if ("mraid" !== AD_PROTOCOL) return;
167
- if (viewable) {
168
- if (sdk.isReady) {
169
- handleResume();
170
- } else {
171
- if (AD_NETWORK == "ironsource") {
170
+ if (isProtocolInitialized) return;
171
+ mraid.removeEventListener("ready", startMraidProtocol);
172
+ function mraidStateChanged(viewable) {
173
+ if (viewable) {
174
+ if (sdk.isReady) {
175
+ handleResume();
176
+ } else {
172
177
  const maxSize = mraid.getMaxSize();
173
178
  sdk.maxWidth = Math.floor(maxSize.width);
174
179
  sdk.maxHeight = Math.floor(maxSize.height);
180
+ bootAd();
175
181
  }
176
- bootAd();
182
+ } else {
183
+ handlePause();
177
184
  }
178
- } else {
179
- handlePause();
180
185
  }
181
- }
182
- function dapiIsViewable(event) {
183
- if ("dapi" !== AD_PROTOCOL) return;
184
- if (event.isViewable) {
185
- if (sdk.isReady) {
186
- handleResume();
186
+ function isViewable() {
187
+ return mraid.isViewable() && "hidden" !== mraid.getState();
188
+ }
189
+ function stateChanged() {
190
+ mraidStateChanged(isViewable());
191
+ }
192
+ mraid.addEventListener("viewableChange", stateChanged);
193
+ mraid.addEventListener("stateChange", stateChanged);
194
+ if (isViewable()) {
195
+ mraidStateChanged(true);
196
+ }
197
+ if (mraid.getAudioVolume) {
198
+ const isAudioEnabled = mraid.getAudioVolume();
199
+ if (isAudioEnabled) {
200
+ handleVolumeChange(1);
187
201
  } else {
188
- const screenSize = dapi.getScreenSize();
189
- screenSize.width = Math.floor(screenSize.width);
190
- screenSize.height = Math.floor(screenSize.height);
191
- bootAd();
202
+ handleVolumeChange(0);
192
203
  }
193
- } else {
194
- handlePause();
195
204
  }
196
- }
197
- function fireResizeEvent(width, height) {
198
- handleResize(width, height);
199
- }
200
- function startMraidProtocol() {
201
- if ("mraid" !== AD_PROTOCOL) return;
202
- if (!isProtocolInitialized) {
203
- mraid.removeEventListener("ready", startMraidProtocol);
204
- mraid.addEventListener("viewableChange", function() {
205
- const isViewable = mraid.isViewable();
206
- mraidIsViewable(isViewable);
207
- });
208
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
209
- mraidIsViewable(true);
210
- }
211
- if (mraid.getAudioVolume) {
212
- const isAudioEnabled = mraid.getAudioVolume();
213
- if (isAudioEnabled) {
205
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
206
+ if (newVolume !== null) {
207
+ if (newVolume > 0) {
214
208
  handleVolumeChange(1);
215
209
  } else {
216
210
  handleVolumeChange(0);
217
211
  }
218
212
  }
219
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
220
- if (newVolume !== null) {
221
- if (newVolume > 0) {
222
- handleVolumeChange(1);
223
- } else {
224
- handleVolumeChange(0);
225
- }
226
- }
227
- });
228
- mraid.addEventListener("error", function(t, e) {
229
- console.log("mraid error: " + t + " action: " + e);
230
- });
231
- mraid.addEventListener("sizeChange", function() {
232
- const maxSize = mraid.getMaxSize();
233
- fireResizeEvent(maxSize.width, maxSize.height);
234
- });
235
- isProtocolInitialized = true;
236
- }
213
+ });
214
+ mraid.addEventListener("error", function(t, e) {
215
+ console.log("mraid error: " + t + " action: " + e);
216
+ });
217
+ mraid.addEventListener("sizeChange", function() {
218
+ const maxSize = mraid.getMaxSize();
219
+ fireResizeEvent(maxSize.width, maxSize.height);
220
+ });
221
+ isProtocolInitialized = true;
237
222
  }
238
223
  function startDapiProtocol() {
239
224
  if ("dapi" !== AD_PROTOCOL) return;
240
- if (!isProtocolInitialized) {
241
- dapi.removeEventListener("ready", startDapiProtocol);
242
- const isAudioEnabled = dapi.getAudioVolume();
243
- if (isAudioEnabled) {
225
+ if (isProtocolInitialized) return;
226
+ function dapiIsViewable(event) {
227
+ if (event.isViewable) {
228
+ if (sdk.isReady) {
229
+ handleResume();
230
+ } else {
231
+ const screenSize = dapi.getScreenSize();
232
+ screenSize.width = Math.floor(screenSize.width);
233
+ screenSize.height = Math.floor(screenSize.height);
234
+ bootAd();
235
+ }
236
+ } else {
237
+ handlePause();
238
+ }
239
+ }
240
+ dapi.removeEventListener("ready", startDapiProtocol);
241
+ const isAudioEnabled = dapi.getAudioVolume();
242
+ if (isAudioEnabled) {
243
+ handleVolumeChange(1);
244
+ } else {
245
+ handleVolumeChange(0);
246
+ }
247
+ dapi.addEventListener("audioVolumeChange", function(volume) {
248
+ const isAudioEnabled2 = !!volume;
249
+ if (isAudioEnabled2) {
244
250
  handleVolumeChange(1);
245
251
  } else {
246
252
  handleVolumeChange(0);
247
253
  }
248
- dapi.addEventListener("audioVolumeChange", function(volume) {
249
- const isAudioEnabled2 = !!volume;
250
- if (isAudioEnabled2) {
251
- handleVolumeChange(1);
252
- } else {
253
- handleVolumeChange(0);
254
- }
255
- });
256
- dapi.addEventListener("adResized", function(event) {
257
- const maxSize = dapi.getScreenSize();
258
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
259
- });
260
- if (dapi.isViewable()) {
261
- dapiIsViewable({ isViewable: true });
262
- }
263
- dapi.addEventListener("viewableChange", dapiIsViewable);
264
- isProtocolInitialized = true;
254
+ });
255
+ dapi.addEventListener("adResized", function(event) {
256
+ const maxSize = dapi.getScreenSize();
257
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
258
+ });
259
+ if (dapi.isViewable()) {
260
+ dapiIsViewable({ isViewable: true });
265
261
  }
262
+ dapi.addEventListener("viewableChange", dapiIsViewable);
263
+ isProtocolInitialized = true;
266
264
  }
267
265
  function startDefaultProtocol() {
268
- if (!isProtocolInitialized) {
269
- if ("mintegral" === AD_NETWORK) {
270
- window.mintGameStart = function() {
271
- handleResume();
272
- handleResize(sdk.maxWidth, sdk.maxHeight);
273
- };
274
- window.mintGameClose = function() {
275
- handlePause();
276
- };
277
- }
278
- document.addEventListener("visibilitychange", () => {
279
- if (document.visibilityState === "visible") {
266
+ if (isProtocolInitialized) return;
267
+ if ("mintegral" === AD_NETWORK) {
268
+ window.mintGameStart = function() {
269
+ handleResume();
270
+ handleResize(sdk.maxWidth, sdk.maxHeight);
271
+ };
272
+ window.mintGameClose = function() {
273
+ handlePause();
274
+ };
275
+ }
276
+ document.addEventListener("visibilitychange", () => {
277
+ if (document.visibilityState === "visible") {
278
+ if (sdk.isReady) {
280
279
  handleResume();
281
280
  } else {
282
- handlePause();
281
+ bootAd();
283
282
  }
284
- });
285
- if (document.readyState === "complete") {
286
- bootAd();
287
283
  } else {
288
- window.addEventListener("load", () => {
289
- bootAd();
290
- });
284
+ handlePause();
291
285
  }
292
- window.addEventListener("resize", function() {
293
- handleResize();
294
- });
295
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
296
- const tapjoyApi = {
297
- skipAd: function() {
298
- try {
299
- sdk.finish();
300
- } catch (e) {
301
- console.warn("Could not skip ad! | " + e);
302
- }
286
+ });
287
+ function documentIsReady() {
288
+ if (document.visibilityState === "visible") bootAd();
289
+ }
290
+ if (document.readyState === "complete") {
291
+ documentIsReady();
292
+ } else {
293
+ window.addEventListener("load", documentIsReady);
294
+ }
295
+ window.addEventListener("resize", function() {
296
+ handleResize();
297
+ });
298
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
299
+ const tapjoyApi = {
300
+ skipAd: function() {
301
+ try {
302
+ sdk.finish();
303
+ } catch (e) {
304
+ console.warn("Could not skip ad! | " + e);
303
305
  }
304
- };
305
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
306
- }
307
- isProtocolInitialized = true;
306
+ }
307
+ };
308
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
308
309
  }
310
+ isProtocolInitialized = true;
309
311
  }
310
312
  function finishTapjoy() {
311
313
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -335,6 +337,18 @@
335
337
  isTouchEventRegistered = true;
336
338
  }
337
339
  }
340
+ function initSDK() {
341
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
342
+ ensureProtocol();
343
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
344
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
345
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
346
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
347
+ } else {
348
+ startDefaultProtocol();
349
+ }
350
+ emitEvent("init");
351
+ }
338
352
  var _sdk = class _sdk {
339
353
  /**
340
354
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -356,23 +370,8 @@
356
370
  */
357
371
  static init(callback) {
358
372
  if (isSDKInitialized) return;
359
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
360
- ensureProtocol();
361
373
  if (callback) initCallback = callback;
362
- if ("mraid" === AD_PROTOCOL && isMraid()) {
363
- if (mraid.getState() !== "ready") {
364
- mraid.addEventListener("ready", startMraidProtocol);
365
- } else {
366
- startMraidProtocol();
367
- }
368
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
369
- window.addEventListener("load", function() {
370
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
371
- });
372
- } else {
373
- startDefaultProtocol();
374
- }
375
- emitEvent("init");
374
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
376
375
  isSDKInitialized = true;
377
376
  }
378
377
  /**
@@ -597,7 +596,7 @@
597
596
  }
598
597
  };
599
598
  /** Current version of the SDK */
600
- _sdk.version = "1.0.14";
599
+ _sdk.version = "1.0.16";
601
600
  /** Current maximum width of the playable ad container in pixels */
602
601
  _sdk.maxWidth = Math.floor(window.innerWidth);
603
602
  /** Current maximum height of the playable ad container in pixels */
package/dist/index.js CHANGED
@@ -187,150 +187,152 @@ function handleResume() {
187
187
  sdk.isPaused = false;
188
188
  emitEvent("resume");
189
189
  }
190
- function mraidIsViewable(viewable) {
190
+ function fireResizeEvent(width, height) {
191
+ handleResize(width, height);
192
+ }
193
+ function startMraidProtocol() {
191
194
  if ("mraid" !== AD_PROTOCOL) return;
192
- if (viewable) {
193
- if (sdk.isReady) {
194
- handleResume();
195
- } else {
196
- if (AD_NETWORK == "ironsource") {
195
+ if (isProtocolInitialized) return;
196
+ mraid.removeEventListener("ready", startMraidProtocol);
197
+ function mraidStateChanged(viewable) {
198
+ if (viewable) {
199
+ if (sdk.isReady) {
200
+ handleResume();
201
+ } else {
197
202
  const maxSize = mraid.getMaxSize();
198
203
  sdk.maxWidth = Math.floor(maxSize.width);
199
204
  sdk.maxHeight = Math.floor(maxSize.height);
205
+ bootAd();
200
206
  }
201
- bootAd();
207
+ } else {
208
+ handlePause();
202
209
  }
203
- } else {
204
- handlePause();
205
210
  }
206
- }
207
- function dapiIsViewable(event) {
208
- if ("dapi" !== AD_PROTOCOL) return;
209
- if (event.isViewable) {
210
- if (sdk.isReady) {
211
- handleResume();
211
+ function isViewable() {
212
+ return mraid.isViewable() && "hidden" !== mraid.getState();
213
+ }
214
+ function stateChanged() {
215
+ mraidStateChanged(isViewable());
216
+ }
217
+ mraid.addEventListener("viewableChange", stateChanged);
218
+ mraid.addEventListener("stateChange", stateChanged);
219
+ if (isViewable()) {
220
+ mraidStateChanged(true);
221
+ }
222
+ if (mraid.getAudioVolume) {
223
+ const isAudioEnabled = mraid.getAudioVolume();
224
+ if (isAudioEnabled) {
225
+ handleVolumeChange(1);
212
226
  } else {
213
- const screenSize = dapi.getScreenSize();
214
- screenSize.width = Math.floor(screenSize.width);
215
- screenSize.height = Math.floor(screenSize.height);
216
- bootAd();
227
+ handleVolumeChange(0);
217
228
  }
218
- } else {
219
- handlePause();
220
229
  }
221
- }
222
- function fireResizeEvent(width, height) {
223
- handleResize(width, height);
224
- }
225
- function startMraidProtocol() {
226
- if ("mraid" !== AD_PROTOCOL) return;
227
- if (!isProtocolInitialized) {
228
- mraid.removeEventListener("ready", startMraidProtocol);
229
- mraid.addEventListener("viewableChange", function() {
230
- const isViewable = mraid.isViewable();
231
- mraidIsViewable(isViewable);
232
- });
233
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
234
- mraidIsViewable(true);
235
- }
236
- if (mraid.getAudioVolume) {
237
- const isAudioEnabled = mraid.getAudioVolume();
238
- if (isAudioEnabled) {
230
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
231
+ if (newVolume !== null) {
232
+ if (newVolume > 0) {
239
233
  handleVolumeChange(1);
240
234
  } else {
241
235
  handleVolumeChange(0);
242
236
  }
243
237
  }
244
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
245
- if (newVolume !== null) {
246
- if (newVolume > 0) {
247
- handleVolumeChange(1);
248
- } else {
249
- handleVolumeChange(0);
250
- }
251
- }
252
- });
253
- mraid.addEventListener("error", function(t, e) {
254
- console.log("mraid error: " + t + " action: " + e);
255
- });
256
- mraid.addEventListener("sizeChange", function() {
257
- const maxSize = mraid.getMaxSize();
258
- fireResizeEvent(maxSize.width, maxSize.height);
259
- });
260
- isProtocolInitialized = true;
261
- }
238
+ });
239
+ mraid.addEventListener("error", function(t, e) {
240
+ console.log("mraid error: " + t + " action: " + e);
241
+ });
242
+ mraid.addEventListener("sizeChange", function() {
243
+ const maxSize = mraid.getMaxSize();
244
+ fireResizeEvent(maxSize.width, maxSize.height);
245
+ });
246
+ isProtocolInitialized = true;
262
247
  }
263
248
  function startDapiProtocol() {
264
249
  if ("dapi" !== AD_PROTOCOL) return;
265
- if (!isProtocolInitialized) {
266
- dapi.removeEventListener("ready", startDapiProtocol);
267
- const isAudioEnabled = dapi.getAudioVolume();
268
- if (isAudioEnabled) {
250
+ if (isProtocolInitialized) return;
251
+ function dapiIsViewable(event) {
252
+ if (event.isViewable) {
253
+ if (sdk.isReady) {
254
+ handleResume();
255
+ } else {
256
+ const screenSize = dapi.getScreenSize();
257
+ screenSize.width = Math.floor(screenSize.width);
258
+ screenSize.height = Math.floor(screenSize.height);
259
+ bootAd();
260
+ }
261
+ } else {
262
+ handlePause();
263
+ }
264
+ }
265
+ dapi.removeEventListener("ready", startDapiProtocol);
266
+ const isAudioEnabled = dapi.getAudioVolume();
267
+ if (isAudioEnabled) {
268
+ handleVolumeChange(1);
269
+ } else {
270
+ handleVolumeChange(0);
271
+ }
272
+ dapi.addEventListener("audioVolumeChange", function(volume) {
273
+ const isAudioEnabled2 = !!volume;
274
+ if (isAudioEnabled2) {
269
275
  handleVolumeChange(1);
270
276
  } else {
271
277
  handleVolumeChange(0);
272
278
  }
273
- dapi.addEventListener("audioVolumeChange", function(volume) {
274
- const isAudioEnabled2 = !!volume;
275
- if (isAudioEnabled2) {
276
- handleVolumeChange(1);
277
- } else {
278
- handleVolumeChange(0);
279
- }
280
- });
281
- dapi.addEventListener("adResized", function(event) {
282
- const maxSize = dapi.getScreenSize();
283
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
284
- });
285
- if (dapi.isViewable()) {
286
- dapiIsViewable({ isViewable: true });
287
- }
288
- dapi.addEventListener("viewableChange", dapiIsViewable);
289
- isProtocolInitialized = true;
279
+ });
280
+ dapi.addEventListener("adResized", function(event) {
281
+ const maxSize = dapi.getScreenSize();
282
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
283
+ });
284
+ if (dapi.isViewable()) {
285
+ dapiIsViewable({ isViewable: true });
290
286
  }
287
+ dapi.addEventListener("viewableChange", dapiIsViewable);
288
+ isProtocolInitialized = true;
291
289
  }
292
290
  function startDefaultProtocol() {
293
- if (!isProtocolInitialized) {
294
- if ("mintegral" === AD_NETWORK) {
295
- window.mintGameStart = function() {
296
- handleResume();
297
- handleResize(sdk.maxWidth, sdk.maxHeight);
298
- };
299
- window.mintGameClose = function() {
300
- handlePause();
301
- };
302
- }
303
- document.addEventListener("visibilitychange", () => {
304
- if (document.visibilityState === "visible") {
291
+ if (isProtocolInitialized) return;
292
+ if ("mintegral" === AD_NETWORK) {
293
+ window.mintGameStart = function() {
294
+ handleResume();
295
+ handleResize(sdk.maxWidth, sdk.maxHeight);
296
+ };
297
+ window.mintGameClose = function() {
298
+ handlePause();
299
+ };
300
+ }
301
+ document.addEventListener("visibilitychange", () => {
302
+ if (document.visibilityState === "visible") {
303
+ if (sdk.isReady) {
305
304
  handleResume();
306
305
  } else {
307
- handlePause();
306
+ bootAd();
308
307
  }
309
- });
310
- if (document.readyState === "complete") {
311
- bootAd();
312
308
  } else {
313
- window.addEventListener("load", () => {
314
- bootAd();
315
- });
309
+ handlePause();
316
310
  }
317
- window.addEventListener("resize", function() {
318
- handleResize();
319
- });
320
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
321
- const tapjoyApi = {
322
- skipAd: function() {
323
- try {
324
- sdk.finish();
325
- } catch (e) {
326
- console.warn("Could not skip ad! | " + e);
327
- }
311
+ });
312
+ function documentIsReady() {
313
+ if (document.visibilityState === "visible") bootAd();
314
+ }
315
+ if (document.readyState === "complete") {
316
+ documentIsReady();
317
+ } else {
318
+ window.addEventListener("load", documentIsReady);
319
+ }
320
+ window.addEventListener("resize", function() {
321
+ handleResize();
322
+ });
323
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
324
+ const tapjoyApi = {
325
+ skipAd: function() {
326
+ try {
327
+ sdk.finish();
328
+ } catch (e) {
329
+ console.warn("Could not skip ad! | " + e);
328
330
  }
329
- };
330
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
331
- }
332
- isProtocolInitialized = true;
331
+ }
332
+ };
333
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
333
334
  }
335
+ isProtocolInitialized = true;
334
336
  }
335
337
  function finishTapjoy() {
336
338
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -360,6 +362,18 @@ function registerTouchHandlers() {
360
362
  isTouchEventRegistered = true;
361
363
  }
362
364
  }
365
+ function initSDK() {
366
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
367
+ ensureProtocol();
368
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
369
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
370
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
371
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
372
+ } else {
373
+ startDefaultProtocol();
374
+ }
375
+ emitEvent("init");
376
+ }
363
377
  var _sdk = class _sdk {
364
378
  /**
365
379
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -381,23 +395,8 @@ var _sdk = class _sdk {
381
395
  */
382
396
  static init(callback) {
383
397
  if (isSDKInitialized) return;
384
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
385
- ensureProtocol();
386
398
  if (callback) initCallback = callback;
387
- if ("mraid" === AD_PROTOCOL && isMraid()) {
388
- if (mraid.getState() !== "ready") {
389
- mraid.addEventListener("ready", startMraidProtocol);
390
- } else {
391
- startMraidProtocol();
392
- }
393
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
394
- window.addEventListener("load", function() {
395
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
396
- });
397
- } else {
398
- startDefaultProtocol();
399
- }
400
- emitEvent("init");
399
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
401
400
  isSDKInitialized = true;
402
401
  }
403
402
  /**
@@ -622,7 +621,7 @@ var _sdk = class _sdk {
622
621
  }
623
622
  };
624
623
  /** Current version of the SDK */
625
- _sdk.version = "1.0.14";
624
+ _sdk.version = "1.0.16";
626
625
  /** Current maximum width of the playable ad container in pixels */
627
626
  _sdk.maxWidth = Math.floor(window.innerWidth);
628
627
  /** Current maximum height of the playable ad container in pixels */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoud/playable-sdk",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "description": "It's unified Playable SDK that supports MRAID, Google, Facebook, Vungle, and many more Ad Networks, simplifying playable ad development with a standardized interface for seamless cross-network compatibility.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",