Last active
April 29, 2025 01:01
-
-
Save angelmartz/b527283bdafb5a35e7c575ab8229880d to your computer and use it in GitHub Desktop.
Consola de Preguntas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const scripts = Array.from(document.querySelectorAll('script')); | |
const targetScript = scripts[26]; | |
const preguntas = targetScript.textContent; | |
const match = preguntas.match(/"([^]*)"/); | |
if (match) { | |
let extracted = match[1]; | |
const indexFirstBracket = extracted.indexOf('['); | |
if (indexFirstBracket !== -1) { | |
extracted = extracted.substring(indexFirstBracket); | |
} | |
extracted = extracted | |
.replace(/\$undefined/g, 'null') | |
.replace(/\$([A-Za-z0-9]+)/g, '"$1"') | |
.replace(/\\"/g, '"') | |
.replace(/""/g, '"') | |
.replace(/"D(\d{4})"-/g, '"D$1-'); | |
const lastBracketIndex = extracted.lastIndexOf(']'); | |
if (lastBracketIndex !== -1) { | |
extracted = extracted.substring(0, lastBracketIndex + 1); | |
} | |
try { | |
const parsed = JSON.parse(extracted); | |
const data = parsed; | |
info = data[3].children[1][3].children[1][3]; | |
const questions = info?.questions; | |
questions.forEach(q => { | |
console.log(`${q.order}. ${q.question}`); | |
console.log(`Criterio: ${q.context}`); | |
console.log('---'); | |
}); | |
} catch (err) { | |
console.error('No se pudo parsear a JSON:', err.message); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Buscar el <script> que contiene los datos | |
const scripts = Array.from(document.querySelectorAll('script')); | |
const targetScript = scripts[26]; | |
const preguntas = targetScript.textContent; | |
// 2. Buscar el contenido | |
const match = preguntas.match(/"([^]*)"/); | |
if (match) { | |
let extracted = match[1]; | |
// 3. Cortar hasta donde empieza el array | |
const indexFirstBracket = extracted.indexOf('['); | |
if (indexFirstBracket !== -1) { | |
extracted = extracted.substring(indexFirstBracket); | |
} | |
// 4. Limpiar variables y corregir errores | |
extracted = extracted | |
.replace(/\$undefined/g, 'null') | |
.replace(/\$([A-Za-z0-9]+)/g, '"$1"') | |
.replace(/\\"/g, '"') | |
.replace(/""/g, '"') | |
.replace(/"D(\d{4})"-/g, '"D$1-'); | |
// 5. Cortar basura final si hay | |
const lastBracketIndex = extracted.lastIndexOf(']'); | |
if (lastBracketIndex !== -1) { | |
extracted = extracted.substring(0, lastBracketIndex + 1); | |
} | |
try { | |
// 6. Parsear a JSON | |
const parsed = JSON.parse(extracted); | |
const data = parsed; | |
// 7. Navegar a las preguntas | |
const info = data[3].children[1][3].children[1][3]; | |
const questions = info?.questions; | |
// 8. Imprimir preguntas en consola | |
questions.forEach(q => { | |
console.log(`${q.order}. ${q.question}`); | |
console.log(`Criterio: ${q.context}`); | |
console.log('---'); | |
}); | |
// 9. Crear un texto para el PDF | |
let output = ''; | |
questions.forEach(q => { | |
output += `${q.order}. ${q.question}\n`; | |
output += `Criterio: ${q.context}\n\n`; | |
}); | |
// 10. Crear un Blob de tipo texto | |
const blob = new Blob([output], { type: 'text/plain' }); | |
// 11. Crear una URL temporal | |
const url = URL.createObjectURL(blob); | |
// 12. Crear un iframe oculto para imprimir | |
const iframe = document.createElement('iframe'); | |
iframe.style.display = 'none'; | |
iframe.src = url; | |
document.body.appendChild(iframe); | |
// 13. Imprimir automáticamente cuando el iframe cargue | |
iframe.onload = () => { | |
iframe.contentWindow.print(); | |
}; | |
} catch (err) { | |
console.error('No se pudo parsear a JSON:', err.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment