@smoud/playable-sdk 1.0.0

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.
@@ -0,0 +1,603 @@
1
+ "use strict";
2
+ (() => {
3
+ // src/events.ts
4
+ var registeredEvents = {};
5
+ function onEvent(event, fn, context, once) {
6
+ let listeners = registeredEvents[event];
7
+ if (!listeners) {
8
+ listeners = registeredEvents[event] = {
9
+ list: []
10
+ };
11
+ }
12
+ listeners.list.push(fn, context || null, once || false);
13
+ }
14
+ function offEvent(event, fn, context) {
15
+ const listeners = registeredEvents[event];
16
+ if (!listeners) return;
17
+ var fnArray = listeners.list;
18
+ if (!fn) {
19
+ fnArray.length = 0;
20
+ } else {
21
+ let i = fnArray.length;
22
+ while (i > 0) {
23
+ i -= 3;
24
+ if (fnArray[i] == fn && (context !== void 0 || fnArray[i + 1] == context)) {
25
+ fnArray.splice(i, 3);
26
+ }
27
+ }
28
+ }
29
+ if (fnArray.length == 0) {
30
+ delete registeredEvents[event];
31
+ }
32
+ }
33
+ function emitEvent(event, a1, a2, a3) {
34
+ var listeners = registeredEvents[event];
35
+ if (!listeners) return;
36
+ const fnArray = listeners.list;
37
+ const len = arguments.length;
38
+ let fn, ctx;
39
+ for (var i = 0; i < fnArray.length; i += 3) {
40
+ fn = fnArray[i];
41
+ ctx = fnArray[i + 1];
42
+ if (fnArray[i + 2]) {
43
+ fnArray.splice(i, 3);
44
+ i -= 3;
45
+ }
46
+ if (len <= 1) fn.call(ctx);
47
+ else if (len == 2) fn.call(ctx, a1);
48
+ else if (len == 3) fn.call(ctx, a1, a2);
49
+ else fn.call(ctx, a1, a2, a3);
50
+ }
51
+ if (fnArray.length == 0) {
52
+ delete registeredEvents[event];
53
+ }
54
+ }
55
+
56
+ // src/protocols.ts
57
+ var uid = 0;
58
+ var NONE = uid++;
59
+ var MRAID = uid++;
60
+ var DAPI = uid++;
61
+ var NUCLEO = uid++;
62
+ var FACEBOOK = uid++;
63
+ var GOOGLE = uid++;
64
+ var MINTEGRAL = uid++;
65
+ var TAPJOY = uid++;
66
+ var TIKTOK = uid++;
67
+ var actualProtocol = NONE;
68
+ function isMraid() {
69
+ return actualProtocol === MRAID;
70
+ }
71
+ function isDapi() {
72
+ return actualProtocol === DAPI;
73
+ }
74
+ function isNucleo() {
75
+ return actualProtocol === NUCLEO;
76
+ }
77
+ function isFacebook() {
78
+ return actualProtocol === FACEBOOK;
79
+ }
80
+ function isGoogle() {
81
+ return actualProtocol === GOOGLE;
82
+ }
83
+ function isMintegral() {
84
+ return actualProtocol === MINTEGRAL;
85
+ }
86
+ function isTapjoy() {
87
+ return actualProtocol === TAPJOY;
88
+ }
89
+ function isTikTok() {
90
+ return actualProtocol === TIKTOK;
91
+ }
92
+ function ensureProtocol() {
93
+ if ("mraid" === AD_PROTOCOL) {
94
+ try {
95
+ mraid.getState();
96
+ actualProtocol = MRAID;
97
+ } catch (error) {
98
+ }
99
+ } else if ("dapi" === AD_PROTOCOL) {
100
+ try {
101
+ dapi.isReady();
102
+ actualProtocol = DAPI;
103
+ } catch (error) {
104
+ }
105
+ } else if ("facebook" === AD_NETWORK) {
106
+ try {
107
+ if (FbPlayableAd) actualProtocol = FACEBOOK;
108
+ } catch (error) {
109
+ }
110
+ } else if ("google" === AD_NETWORK) {
111
+ try {
112
+ if (ExitApi) actualProtocol = GOOGLE;
113
+ } catch (error) {
114
+ }
115
+ } else if ("mintegral" === AD_NETWORK) {
116
+ if (window.gameReady) actualProtocol = MINTEGRAL;
117
+ } else if ("tapjoy" === AD_NETWORK) {
118
+ if (window.TJ_API) actualProtocol = TAPJOY;
119
+ } else if ("tiktok" === AD_NETWORK) {
120
+ if (window.openAppStore) actualProtocol = TIKTOK;
121
+ }
122
+ }
123
+
124
+ // src/core.ts
125
+ var destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
126
+ var isSDKInitialized = false;
127
+ var isProtocolInitialized = false;
128
+ var isForcePaused = false;
129
+ var isInstallClicked = false;
130
+ var initCallback = () => {
131
+ };
132
+ function bootAd() {
133
+ if (sdk.isReady) return;
134
+ emitEvent("boot");
135
+ document.body.oncontextmenu = function() {
136
+ return false;
137
+ };
138
+ initCallback(sdk.maxWidth, sdk.maxHeight);
139
+ emitEvent("ready");
140
+ sdk.isReady = true;
141
+ }
142
+ function changeVolume(value) {
143
+ emitEvent("volume", value);
144
+ }
145
+ function handlePause() {
146
+ changeVolume(0);
147
+ sdk.isPaused = true;
148
+ emitEvent("pause");
149
+ }
150
+ function handleResume() {
151
+ if (isForcePaused) return;
152
+ changeVolume(sdk.volume);
153
+ sdk.isPaused = false;
154
+ emitEvent("resume");
155
+ }
156
+ function mraidIsViewable(viewable) {
157
+ if ("mraid" !== AD_PROTOCOL) return;
158
+ if (viewable) {
159
+ if (sdk.isReady) {
160
+ handleResume();
161
+ } else {
162
+ if (AD_NETWORK == "ironsource") {
163
+ const maxSize = mraid.getMaxSize();
164
+ sdk.maxWidth = Math.floor(maxSize.width);
165
+ sdk.maxHeight = Math.floor(maxSize.height);
166
+ }
167
+ bootAd();
168
+ }
169
+ } else {
170
+ handlePause();
171
+ }
172
+ }
173
+ function dapiIsViewable(event) {
174
+ if ("dapi" !== AD_PROTOCOL) return;
175
+ if (event.isViewable) {
176
+ if (sdk.isReady) {
177
+ handleResume();
178
+ } else {
179
+ var screenSize = dapi.getScreenSize();
180
+ screenSize.width = Math.floor(screenSize.width);
181
+ screenSize.height = Math.floor(screenSize.height);
182
+ bootAd();
183
+ }
184
+ } else {
185
+ handlePause();
186
+ }
187
+ }
188
+ function fireResizeEvent(width, height) {
189
+ handleResize(width, height);
190
+ }
191
+ function startMraidProtocol() {
192
+ if ("mraid" !== AD_PROTOCOL) return;
193
+ if (!isProtocolInitialized) {
194
+ mraid.removeEventListener("ready", startMraidProtocol);
195
+ mraid.addEventListener("viewableChange", function() {
196
+ var isViewable = mraid.isViewable();
197
+ mraidIsViewable(isViewable);
198
+ });
199
+ if (mraid.isViewable() || AD_NETWORK == "adcolony") {
200
+ mraidIsViewable(true);
201
+ }
202
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
203
+ if (newVolume !== null) {
204
+ if (newVolume > 0) {
205
+ changeVolume(sdk.volume);
206
+ } else {
207
+ changeVolume(0);
208
+ }
209
+ }
210
+ });
211
+ mraid.addEventListener("error", function(t, e) {
212
+ console.log("mraid error: " + t + " action: " + e);
213
+ });
214
+ mraid.addEventListener("sizeChange", function() {
215
+ var maxSize = mraid.getMaxSize();
216
+ fireResizeEvent(maxSize.width, maxSize.height);
217
+ });
218
+ isProtocolInitialized = true;
219
+ }
220
+ }
221
+ function startDapiProtocol() {
222
+ if ("dapi" !== AD_PROTOCOL) return;
223
+ if (!isProtocolInitialized) {
224
+ dapi.removeEventListener("ready", startDapiProtocol);
225
+ var isAudioEnabled = !!dapi.getAudioVolume();
226
+ dapi.addEventListener("audioVolumeChange", function(volume) {
227
+ var isAudioEnabled2 = !!volume;
228
+ if (isAudioEnabled2) {
229
+ changeVolume(sdk.volume);
230
+ } else {
231
+ changeVolume(0);
232
+ }
233
+ });
234
+ dapi.addEventListener("adResized", function(event) {
235
+ var maxSize = dapi.getScreenSize();
236
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
237
+ });
238
+ if (dapi.isViewable()) {
239
+ dapiIsViewable({ isViewable: true });
240
+ }
241
+ dapi.addEventListener("viewableChange", dapiIsViewable);
242
+ isProtocolInitialized = true;
243
+ }
244
+ }
245
+ function startDefaultProtocol() {
246
+ if ("none" !== AD_PROTOCOL) return;
247
+ if (!isProtocolInitialized) {
248
+ if ("mintegral" === AD_NETWORK) {
249
+ window.mintGameStart = function() {
250
+ handleResume();
251
+ handleResize(sdk.maxWidth, sdk.maxHeight);
252
+ };
253
+ window.mintGameClose = function() {
254
+ handlePause();
255
+ };
256
+ }
257
+ document.addEventListener("visibilitychange", () => {
258
+ if (document.visibilityState === "visible") {
259
+ if (sdk.isReady) {
260
+ handleResume();
261
+ } else {
262
+ bootAd();
263
+ }
264
+ } else {
265
+ handlePause();
266
+ }
267
+ });
268
+ if (document.readyState === "complete" || document.visibilityState === "visible") {
269
+ bootAd();
270
+ } else {
271
+ window.addEventListener("load", () => {
272
+ bootAd();
273
+ });
274
+ }
275
+ window.addEventListener("resize", function() {
276
+ handleResize();
277
+ });
278
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
279
+ var tapjoyApi = {
280
+ skipAd: function() {
281
+ try {
282
+ sdk.finish();
283
+ } catch (e) {
284
+ console.warn("Could not skip ad! | " + e);
285
+ }
286
+ }
287
+ };
288
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
289
+ }
290
+ isProtocolInitialized = true;
291
+ }
292
+ }
293
+ function finishTapjoy() {
294
+ window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
295
+ window.TJ_API.playableFinished && window.TJ_API.playableFinished();
296
+ window.TJ_API.gameplayFinished && window.TJ_API.gameplayFinished();
297
+ }
298
+ function handleResize(width, height) {
299
+ sdk.maxWidth = Math.floor(width || window.innerWidth);
300
+ sdk.maxHeight = Math.floor(height || window.innerHeight);
301
+ sdk.isLandscape = sdk.maxWidth > sdk.maxHeight;
302
+ emitEvent("resize", sdk.maxWidth, sdk.maxHeight);
303
+ }
304
+ var isListeningToTouchEvents = false;
305
+ var isTouchEventRegistered = false;
306
+ function onUserInteraction(event) {
307
+ if (typeof TouchEvent !== "undefined" && event instanceof TouchEvent) {
308
+ isListeningToTouchEvents = true;
309
+ }
310
+ if (isListeningToTouchEvents && event instanceof MouseEvent) return;
311
+ sdk.interactions += 1;
312
+ emitEvent("interaction", sdk.interactions);
313
+ }
314
+ function registerTouchHandlers() {
315
+ if (!isTouchEventRegistered) {
316
+ document.addEventListener("mousedown", onUserInteraction);
317
+ document.addEventListener("touchstart", onUserInteraction);
318
+ isTouchEventRegistered = true;
319
+ }
320
+ }
321
+ var _sdk = class _sdk {
322
+ /**
323
+ * Initializes the SDK and sets up protocol-specific handlers.
324
+ * This must be called as earlier as possible.
325
+ *
326
+ * @param callback Optional function called when ad container is ready
327
+ * @example
328
+ * // Basic initialization
329
+ * sdk.init();
330
+ *
331
+ * // Initialization with callback
332
+ * sdk.init((width, height) => {
333
+ * new App(width, height)
334
+ * });
335
+ *
336
+ * @fires init When initialization starts
337
+ * @fires boot When the ad begins booting
338
+ * @fires ready When Ad Network is ready and playable ad can be initialized
339
+ */
340
+ static init(callback) {
341
+ if (isSDKInitialized) return;
342
+ ensureProtocol();
343
+ if (callback) initCallback = callback;
344
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
345
+ if (mraid.getState() !== "ready" && "adcolony" !== AD_NETWORK) {
346
+ mraid.addEventListener("ready", startMraidProtocol);
347
+ } else {
348
+ startMraidProtocol();
349
+ }
350
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
351
+ window.addEventListener("load", function() {
352
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
353
+ });
354
+ } else {
355
+ startDefaultProtocol();
356
+ }
357
+ emitEvent("init");
358
+ isSDKInitialized = true;
359
+ }
360
+ /**
361
+ * Starts the playable ad experience.
362
+ * Should be called after all resources are loaded and first frame is rendered.
363
+ *
364
+ * @example
365
+ * // Call just after all resources are preloaded and first frame is rendered
366
+ * sdk.start();
367
+ *
368
+ * @fires start When the playable ad starts
369
+ * @fires resize When the ad container is initially sized
370
+ */
371
+ static start() {
372
+ if (_sdk.isStarted) return;
373
+ _sdk.isStarted = true;
374
+ emitEvent("start");
375
+ registerTouchHandlers();
376
+ if ("mintegral" === AD_NETWORK && isMintegral()) {
377
+ _sdk.resize();
378
+ handlePause();
379
+ window.gameReady && window.gameReady();
380
+ } else {
381
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
382
+ window.TJ_API.setPlayableBuild({
383
+ orientation: this.isLandscape ? "landscape" : "portrait",
384
+ buildID: __BUILD_ID__
385
+ });
386
+ }
387
+ _sdk.resize();
388
+ }
389
+ }
390
+ /**
391
+ * Marks the playable ad as finished.
392
+ * This triggers network-specific completion handlers.
393
+ *
394
+ * @example
395
+ * // Call when game/experience is complete
396
+ * sdk.finish();
397
+ *
398
+ * @fires finish When the playable ad is marked as finished
399
+ */
400
+ static finish() {
401
+ _sdk.isFinished = true;
402
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
403
+ finishTapjoy();
404
+ } else if ("mintegral" === AD_NETWORK && isMintegral()) {
405
+ window.gameEnd && window.gameEnd();
406
+ } else if ("vungle" === AD_NETWORK) {
407
+ parent.postMessage("complete", "*");
408
+ }
409
+ emitEvent("finish");
410
+ }
411
+ /**
412
+ * Triggers a retry/restart of the playable ad.
413
+ * Behavior varies by ad network.
414
+ *
415
+ * @example
416
+ * // Allow user to try again
417
+ * retryButton.onclick = () => sdk.retry();
418
+ *
419
+ * @fires retry When a retry is triggered
420
+ */
421
+ static retry() {
422
+ if ("mintegral" === AD_NETWORK && isMintegral()) {
423
+ window.gameRetry && window.gameRetry();
424
+ } else if ("nucleo" === AD_PROTOCOL && isNucleo()) {
425
+ NUC.trigger.tryAgain();
426
+ }
427
+ emitEvent("retry");
428
+ }
429
+ /**
430
+ * Triggers the install/download action for the advertised app.
431
+ * Handles different store opening methods across ad networks.
432
+ *
433
+ * @example
434
+ * // Call when user wants to install
435
+ * installButton.onclick = () => sdk.install();
436
+ *
437
+ * @fires finish If the ad hasn't been marked as finished
438
+ * @fires install When the install action is triggered
439
+ */
440
+ static install() {
441
+ if (!_sdk.isFinished) {
442
+ _sdk.isFinished = true;
443
+ let timeout = 0;
444
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
445
+ finishTapjoy();
446
+ timeout = 300;
447
+ }
448
+ emitEvent("finish");
449
+ setTimeout(function() {
450
+ _sdk.install();
451
+ }, timeout);
452
+ return;
453
+ }
454
+ if (isInstallClicked) return;
455
+ isInstallClicked = true;
456
+ setTimeout(function() {
457
+ isInstallClicked = false;
458
+ }, 500);
459
+ emitEvent("install");
460
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
461
+ mraid.openStoreUrl ? mraid.openStoreUrl() : mraid.open(destinationUrl);
462
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
463
+ dapi.openStoreUrl();
464
+ } else if ("nucleo" === AD_PROTOCOL && isNucleo()) {
465
+ NUC.trigger.convert(destinationUrl);
466
+ } else if ("facebook" == AD_NETWORK && isFacebook()) {
467
+ FbPlayableAd.onCTAClick();
468
+ } else if ("vungle" == AD_NETWORK) {
469
+ parent.postMessage("download", "*");
470
+ } else if ("google" == AD_NETWORK && isGoogle()) {
471
+ ExitApi.exit();
472
+ } else if ("mintegral" === AD_NETWORK && isMintegral()) {
473
+ window.install && window.install();
474
+ } else if ("tapjoy" === AD_NETWORK && isTapjoy()) {
475
+ window.TJ_API.click();
476
+ } else if ("tiktok" === AD_NETWORK && isTikTok()) {
477
+ window.openAppStore();
478
+ } else {
479
+ window.open(destinationUrl);
480
+ }
481
+ }
482
+ /**
483
+ * Trigger force resize event
484
+ * Useful when container size changes need to be manually propagated.
485
+ *
486
+ * @example
487
+ * sdk.resize();
488
+ *
489
+ * @fires resize With current maxWidth and maxHeight
490
+ */
491
+ static resize() {
492
+ handleResize(_sdk.maxWidth, _sdk.maxHeight);
493
+ }
494
+ /**
495
+ * Forces the playable ad into a paused state.
496
+ *
497
+ * @example
498
+ * // Pause the experience
499
+ * pauseButton.onclick = () => sdk.pause();
500
+ *
501
+ * @fires pause When the ad enters paused state
502
+ */
503
+ static pause() {
504
+ if (!isForcePaused) {
505
+ isForcePaused = true;
506
+ handlePause();
507
+ }
508
+ }
509
+ /**
510
+ * Resumes the playable ad from a forced pause state.
511
+ * Only works if the ad was paused via sdk.pause().
512
+ *
513
+ * @example
514
+ * // Resume from pause
515
+ * resumeButton.onclick = () => sdk.resume();
516
+ *
517
+ * @fires resume When the ad resumes from pause
518
+ */
519
+ static resume() {
520
+ if (isForcePaused) {
521
+ isForcePaused = false;
522
+ handleResume();
523
+ }
524
+ }
525
+ /**
526
+ * Registers an event listener.
527
+ *
528
+ * @param event Name of the event to listen for
529
+ * @param fn Callback function to execute when event occurs
530
+ * @param context Optional 'this' context for the callback
531
+ *
532
+ * @example
533
+ * // Listen for user interactions
534
+ * sdk.on('interaction', (count) => {
535
+ * console.log(`User interaction #${count}`);
536
+ * });
537
+ *
538
+ * // Listen for resize with context
539
+ * sdk.on('resize', function(width, height) {
540
+ * this.updateLayout(width, height);
541
+ * }, gameInstance);
542
+ */
543
+ static on(event, fn, context) {
544
+ onEvent(event, fn, context);
545
+ }
546
+ /**
547
+ * Registers a one-time event listener that removes itself after execution.
548
+ *
549
+ * @param event Name of the event to listen for
550
+ * @param fn Callback function to execute when event occurs
551
+ * @param context Optional 'this' context for the callback
552
+ *
553
+ * @example
554
+ * // Listen for first interaction only
555
+ * sdk.once('interaction', () => {
556
+ * console.log('First user interaction occurred!');
557
+ * });
558
+ */
559
+ static once(event, fn, context) {
560
+ onEvent(event, fn, context, true);
561
+ }
562
+ /**
563
+ * Removes an event listener.
564
+ *
565
+ * @param event Name of the event to stop listening for
566
+ * @param fn Optional callback function to remove (if not provided, removes all listeners for the event)
567
+ * @param context Optional 'this' context to match when removing
568
+ *
569
+ * @example
570
+ * // Remove specific listener
571
+ * const handler = () => console.log('Interaction');
572
+ * sdk.off('interaction', handler);
573
+ *
574
+ * // Remove all listeners for an event
575
+ * sdk.off('interaction');
576
+ */
577
+ static off(event, fn, context) {
578
+ offEvent(event, fn, context);
579
+ }
580
+ };
581
+ /** Current version of the SDK */
582
+ _sdk.version = "1.0.0";
583
+ /** Current maximum width of the playable ad container in pixels */
584
+ _sdk.maxWidth = Math.floor(window.innerWidth);
585
+ /** Current maximum height of the playable ad container in pixels */
586
+ _sdk.maxHeight = Math.floor(window.innerHeight);
587
+ /** Indicates if the current orientation is landscape (width > height) */
588
+ _sdk.isLandscape = window.innerWidth > window.innerHeight;
589
+ /** Indicates if the Ad Network is ready and playable ad can be initialized */
590
+ _sdk.isReady = false;
591
+ /** Indicates if all playable ad resources are loaded and gameplay has started */
592
+ _sdk.isStarted = false;
593
+ /** Indicates if the playable ad is currently paused */
594
+ _sdk.isPaused = false;
595
+ /** Indicates if the playable ad has finished */
596
+ _sdk.isFinished = false;
597
+ /** Current volume level (0-1) */
598
+ _sdk.volume = 1;
599
+ /** Number of user interactions with the playable ad */
600
+ _sdk.interactions = 0;
601
+ var sdk = _sdk;
602
+ window.PlayableSDK = sdk;
603
+ })();