|
1 | 1 | BASE_URL = 'http://localhost/'; |
2 | 2 |
|
3 | | -function changeTraceElements(codeElements) { |
| 3 | +function changeElements(codeElements, endpoint) { |
4 | 4 | // Add a button to each pre element |
5 | 5 | codeElements.forEach(function(codeElement) { |
6 | 6 |
|
7 | 7 | // replace the code element with an iframe |
8 | 8 | const textContent = codeElement.textContent || codeElement.innerText; |
9 | 9 | 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}`; |
12 | 14 | codeElement.replaceWith(iframe); |
13 | 15 |
|
14 | 16 | 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) { |
17 | 19 | iframe.style.height = event.data.height + 'px'; |
18 | 20 | } |
19 | 21 | }); |
20 | 22 |
|
21 | 23 | }); |
22 | 24 | } |
23 | 25 |
|
24 | | - |
25 | 26 | document.addEventListener('DOMContentLoaded', function() { |
26 | 27 | // check if BASE_URL is defined and reachable |
27 | | - fetch(`${BASE_URL}`) |
| 28 | + fetch(`${BASE_URL}embed/traceview`) |
28 | 29 | .then(response => { |
29 | 30 | if (!response.ok) { |
| 31 | + console.log('Network response was not ok'); |
30 | 32 | throw new Error('Network response was not ok'); |
31 | 33 | } |
32 | 34 | return response.text(); |
33 | 35 | }) |
34 | 36 | .then(data => { |
35 | 37 | // if we can reach it, add buttons to trace and guardrail elements |
36 | 38 | // 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')) |
| 39 | + changeElements(document.querySelectorAll('div.language-trace'), 'traceview?trace') |
| 40 | + changeElements(document.querySelectorAll('div.language-guardrail'), 'playground?policy') |
39 | 41 | }) |
40 | 42 | .catch(error => { |
41 | 43 | console.error('There was a problem with the fetch operation:', error); |
|
0 commit comments