11/**
22 * @module Android AdsJS integration
33 */
4- import { load , init } from '../src/content-scope-features.js' ;
5- import { processConfig } from './../src/utils' ;
6- import { AndroidAdsjsMessagingConfig } from '../../messaging/index.js' ;
4+ import { load , init , updateFeatureArgs } from '../src/content-scope-features.js' ;
5+ import { processConfig , isBeingFramed } from './../src/utils' ;
6+ import { AndroidAdsjsMessagingConfig , MessagingContext , Messaging } from '../../messaging/index.js' ;
7+
8+ /**
9+ * Send initial ping once per frame to establish communication with the platform.
10+ * This replaces the per-feature ping that was previously sent in AndroidAdsjsMessagingTransport.
11+ * When response is received, updates all loaded feature configurations.
12+ *
13+ * @param {AndroidAdsjsMessagingConfig } messagingConfig
14+ * @param {object } processedConfig - The base configuration
15+ */
16+ async function sendInitialPingAndUpdate ( messagingConfig , processedConfig ) {
17+ // Only send ping in top context, not in frames
18+ if ( isBeingFramed ( ) ) {
19+ return ;
20+ }
21+
22+ try {
23+ // Create messaging context for the initial ping
24+ const messagingContext = new MessagingContext ( {
25+ context : 'contentScopeScripts' ,
26+ env : processedConfig . debug ? 'development' : 'production' ,
27+ featureName : 'messaging' ,
28+ } ) ;
29+
30+ // Create messaging instance - handles all the subscription/error boilerplate
31+ const messaging = new Messaging ( messagingContext , messagingConfig ) ;
32+
33+ if ( processedConfig . debug ) {
34+ console . log ( 'AndroidAdsjs: Sending initial ping...' ) ;
35+ }
36+
37+ // Send the ping request
38+ const response = await messaging . request ( 'initialPing' , { } ) ;
39+
40+ // Update all loaded features with merged configuration
41+ if ( response && typeof response === 'object' ) {
42+ const updatedConfig = { ...processedConfig , ...response } ;
43+
44+ await updateFeatureArgs ( updatedConfig ) ;
45+ }
46+ } catch ( error ) {
47+ if ( processedConfig . debug ) {
48+ console . error ( 'AndroidAdsjs: Initial ping failed:' , error ) ;
49+ }
50+ }
51+ }
752
853function initCode ( ) {
954 // @ts -expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
@@ -24,6 +69,10 @@ function initCode() {
2469 debug : processedConfig . debug ,
2570 } ) ;
2671
72+ // Send initial ping asynchronously to update feature configurations when response arrives
73+ sendInitialPingAndUpdate ( processedConfig . messagingConfig , processedConfig ) ;
74+
75+ // Load and init features immediately with base configuration
2776 load ( {
2877 platform : processedConfig . platform ,
2978 site : processedConfig . site ,
0 commit comments