11BASE_URL = 'http://localhost/' ;
22
3- function changeTraceElements ( codeElements ) {
3+ function changeElements ( codeElements , endpoint ) {
44 // Add a button to each pre element
55 codeElements . forEach ( function ( codeElement ) {
66
77 // replace the code element with an iframe
88 const textContent = codeElement . textContent || codeElement . innerText ;
99 const encodedContent = btoa ( textContent ) ;
10- const iframe = document . createElement ( 'iframe' , { id : 'traceview-' + encodedContent } ) ;
11- iframe . src = `${ BASE_URL } traceview?trace=${ encodedContent } ` ;
10+ // get a UUID
11+ const id = crypto . randomUUID ( ) . toString ( ) ;
12+ const iframe = document . createElement ( 'iframe' , { id : id } ) ;
13+ iframe . src = `${ BASE_URL } embed/${ endpoint } =${ encodedContent } &id=${ id } ` ;
1214 codeElement . replaceWith ( iframe ) ;
1315
1416 window . addEventListener ( 'message' , function ( event ) {
15- if ( event . data . type === 'resize' ) {
16- console . log ( 'resize' , event . data ) ;
17+ //check which element the message is coming from
18+ if ( event . data . type === 'resize' && event . data . id === id ) {
1719 iframe . style . height = event . data . height + 'px' ;
1820 }
1921 } ) ;
2022
2123 } ) ;
2224}
2325
24-
2526document . addEventListener ( 'DOMContentLoaded' , function ( ) {
2627 // check if BASE_URL is defined and reachable
2728 fetch ( `${ BASE_URL } ` )
@@ -34,8 +35,8 @@ document.addEventListener('DOMContentLoaded', function() {
3435 . then ( data => {
3536 // if we can reach it, add buttons to trace and guardrail elements
3637 // currently disabled as the traceview endpoint is not yet enabled on explorer
37- //changeTraceElements (document.querySelectorAll('div.language-trace'))
38- //changeGuardrailElements (document.querySelectorAll('div.language-guardrail'))
38+ changeElements ( document . querySelectorAll ( 'div.language-trace' ) , 'traceview?trace' )
39+ changeElements ( document . querySelectorAll ( 'div.language-guardrail' ) , 'playground?policy' )
3940 } )
4041 . catch ( error => {
4142 console . error ( 'There was a problem with the fetch operation:' , error ) ;
0 commit comments