|
40 | 40 |
|
41 | 41 | const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; |
42 | 42 |
|
43 | | - // Limpiar el contenido del iframe |
44 | | - iframeDoc.open(); |
45 | | - iframeDoc.close(); |
| 43 | + // Limpiar iframe SIN open/close |
| 44 | + iframeDoc.body.innerHTML = ''; |
46 | 45 |
|
47 | 46 |
|
48 | 47 | // Crear un contenedor para los logs |
49 | 48 | const logContainer = iframeDoc.createElement('div'); |
50 | 49 | logContainer.classList.add('logContainer'); |
51 | 50 | iframeDoc.body.appendChild(logContainer); |
52 | 51 |
|
53 | | - // Sobrescribir console.log y console.error en el contexto del iframe |
54 | | - iframe.contentWindow.console.log = function(...args) { |
55 | | - const logMessage = iframeDoc.createElement('div'); |
56 | | - logMessage.textContent = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : arg).join(' '); |
57 | | - logContainer.appendChild(logMessage); |
58 | | - }; |
59 | | - |
60 | | - iframe.contentWindow.console.error = function(...args) { |
61 | | - const errorMessage = iframeDoc.createElement('div'); |
62 | | - errorMessage.style.color = 'red'; // Estilo para errores |
63 | | - errorMessage.textContent = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : arg).join(' '); |
64 | | - logContainer.appendChild(errorMessage); |
65 | | - }; |
66 | | - |
67 | | - |
68 | | - // Siempre crear y ejecutar un script para ejecutar el código dentro del iframe |
| 52 | + // Script con consola aislada |
69 | 53 | const script = iframeDoc.createElement('script'); |
70 | 54 | script.textContent = ` |
71 | | - (function() { |
72 | | - try { |
73 | | - ${code} |
74 | | - } catch (error) { |
75 | | - console.error('Error:', error.message); |
| 55 | + (function () { |
| 56 | + const console = { |
| 57 | + log: (...args) => { |
| 58 | + const div = document.createElement('div'); |
| 59 | + div.textContent = args.map(a => |
| 60 | + typeof a === 'object' ? JSON.stringify(a) : a |
| 61 | + ).join(' '); |
| 62 | + document.querySelector('.logContainer').appendChild(div); |
| 63 | + }, |
| 64 | + error: (...args) => { |
| 65 | + const div = document.createElement('div'); |
| 66 | + div.style.color = 'red'; |
| 67 | + div.textContent = args.join(' '); |
| 68 | + document.querySelector('.logContainer').appendChild(div); |
76 | 69 | } |
| 70 | + }; |
| 71 | + |
| 72 | + try { |
| 73 | + ${code} |
| 74 | + } catch (error) { |
| 75 | + console.error('Error:', error.message); |
| 76 | + } |
77 | 77 | })(); |
78 | 78 | `; |
79 | 79 | iframeDoc.body.appendChild(script); |
80 | | - } |
| 80 | + } |
81 | 81 |
|
82 | 82 | function restoreCode() { |
83 | 83 | const codeBlock = document.getElementById('codeBlock'); |
|
0 commit comments