1/** 2 * @file 3 * MoEngage OSM campaign handling and user identification. 4 */ 5 6(function (Drupal, drupalSettings) { 7 'use strict'; 8 Drupal.behaviors.moengage_script = { 9 attach: function () { 10 let moengagePromise = null; 11 12 window.waitForMoEngageOSMReady = function waitForMoEngageOSMReady({ 13 maxWaitForSDKMs = 15000, 14 pollIntervalMs = 250, 15 campaignTimeoutMs = 30000, 16 } = {}) { 17 if (moengagePromise) { 18 return moengagePromise; 19 } 20 21 moengagePromise = new Promise((resolve) => { 22 const t0 = Date.now(); 23 24 (function poll() { 25 const moe = window.Moengage; 26 const apiReady = !!(moe && moe.onsite && typeof moe.onsite.getSelfHandledOSM === 'function'); 27 28 if (!apiReady) { 29 if (Date.now() - t0 > maxWaitForSDKMs) { 30 console.error('MoEngage OSM API not available. Check SDK load and version (>= 2.55.0).'); 31 return resolve(null); 32 } 33 return setTimeout(poll, pollIntervalMs); 34 } 35 36 let settled = false; 37 const campaignTimer = setTimeout(() => { 38 if (!settled) { 39 console.warn('No Self-Handled OSM campaign received within timeout.'); 40 settled = true; 41 resolve(null); 42 } 43 }, campaignTimeoutMs); 44 45 try { 46 moe.onsite.getSelfHandledOSM(function (fullCampaignData) { 47 try { 48 if (!fullCampaignData || !fullCampaignData.length) { 49 if (!settled) { settled = true; clearTimeout(campaignTimer); resolve(null); } 50 return; 51 } 52 53 const first = fullCampaignData[0]; 54 const raw = first && first.jsonPayload; 55 let payload = null; 56 57 if (typeof raw === 'string') { 58 payload = JSON.parse(raw); 59 } else if (raw && typeof raw === 'object') { 60 payload = raw; 61 } 62 63 const moengage_id = payload && payload.moengage_id ? String(payload.moengage_id) : null; 64 drupalSettings.moengage_id = moengage_id; 65 if (!settled) { settled = true; clearTimeout(campaignTimer); resolve(moengage_id); } 66 } catch (e) { 67 if (!settled) { settled = true; clearTimeout(campaignTimer); resolve(null); } 68 } 69 }); 70 } catch (err) { 71 if (!settled) { settled = true; clearTimeout(campaignTimer); resolve(null); } 72 } 73 })(); 74 }); 75 return moengagePromise; 76 }; 77 } 78 } 79})(Drupal, drupalSettings);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.