@@ -257,51 +257,52 @@ export class IframeSandbox implements SignalSandbox {
257257 constructor ( edgeFnUrl : string , processSignalFn ?: string ) {
258258 logger . debug ( 'Initializing iframe sandbox' )
259259 this . edgeFnUrl = edgeFnUrl
260- this . iframe = document . createElement ( 'iframe' )
261- this . iframe . id = 'segment-signals-sandbox'
262- this . iframe . style . display = 'none'
263- this . iframe . src = 'about:blank'
264- document . body . appendChild ( this . iframe )
265-
266- const doc = this . iframe . contentDocument !
260+ const innerHTML = this . buildIframeInnerHTMLWithScriptTag (
261+ Boolean ( processSignalFn )
262+ )
263+ const jsScriptContent = this . getIframeScriptJSScriptContent ( processSignalFn )
264+ const iframe = document . createElement ( 'iframe' )
265+ iframe . id = 'segment-signals-sandbox'
266+ iframe . style . display = 'none'
267+ iframe . src = 'about:blank'
268+ document . body . appendChild ( iframe )
269+ const doc = iframe . contentDocument !
267270 doc . open ( )
268- doc . write ( this . getIframeHTML ( processSignalFn ) )
271+ doc . write ( innerHTML )
269272 doc . close ( )
270- this . iframeReady = new Promise ( ( resolve ) => {
273+ const ready = new Promise < undefined > ( ( resolve ) => {
271274 window . addEventListener ( 'message' , ( e ) => {
272- if (
273- e . source === this . iframe . contentWindow &&
274- e . data === 'iframe_ready'
275- ) {
276- this . iframe . contentWindow ! . postMessage ( {
275+ if ( e . source === iframe . contentWindow && e . data === 'iframe_ready' ) {
276+ iframe . contentWindow ! . postMessage ( {
277277 type : 'init' ,
278278 } )
279279 resolve ( undefined )
280280 }
281281 } )
282282 } )
283- const runtimeJs = this . getIframeJS ( processSignalFn )
284- const blob = new Blob ( [ runtimeJs ] , { type : 'application/javascript' } )
283+ const blob = new Blob ( [ jsScriptContent ] , { type : 'application/javascript' } )
285284 const runtimeScript = doc . createElement ( 'script' )
286285 runtimeScript . src = URL . createObjectURL ( blob )
287286 doc . head . appendChild ( runtimeScript )
287+ this . iframeReady = ready
288+ this . iframe = iframe
288289 }
289290
290- private getIframeHTML ( processSignalFn ?: string ) : string {
291+ private buildIframeInnerHTMLWithScriptTag ( includedEdgeFn = true ) : string {
291292 return [
292293 `<!DOCTYPE html>` ,
293294 `<html>` ,
294295 `<head>` ,
295- processSignalFn
296- ? ''
297- : `<script id="edge-fn" src= ${ this . edgeFnUrl } ></script>` ,
296+ includedEdgeFn
297+ ? `<script id="edge-fn" src= ${ this . edgeFnUrl } ></script>`
298+ : '' ,
298299 `</head>` ,
299300 `<body></body>
300301 </html>` ,
301302 ] . join ( ',' )
302303 }
303304
304- private getIframeJS ( processSignalFn ?: string ) : string {
305+ private getIframeScriptJSScriptContent ( processSignalFn ?: string ) : string {
305306 // External signal processor script
306307 // Inject runtime via Blob (CSP-safe)
307308 return `
0 commit comments