Skip to content

Instantly share code, notes, and snippets.

@angelmartz
Created April 29, 2025 01:06
Show Gist options
  • Save angelmartz/ab2b33727819980595260ede3ad70fb7 to your computer and use it in GitHub Desktop.
Save angelmartz/ab2b33727819980595260ede3ad70fb7 to your computer and use it in GitHub Desktop.
Preguntas
// 1. Buscar el <script> que contiene los datos
const scripts = Array.from(document.querySelectorAll('script'));
const scriptObjetivo = scripts[26];
const textoPreguntas = scriptObjetivo.textContent;
// 2. Buscar el contenido
const coincidencia = textoPreguntas.match(/"([^]*)"/);
if (coincidencia) {
let extraido = coincidencia[1];
// 3. Cortar hasta donde empieza el array
const indicePrimerCorchete = extraido.indexOf('[');
if (indicePrimerCorchete !== -1) {
extraido = extraido.substring(indicePrimerCorchete);
}
// 4. Limpiar variables y corregir errores
extraido = extraido
.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 indiceUltimoCorchete = extraido.lastIndexOf(']');
if (indiceUltimoCorchete !== -1) {
extraido = extraido.substring(0, indiceUltimoCorchete + 1);
}
try {
// 6. Parsear a JSON
const datos = JSON.parse(extraido);
// 7. Navegar a las preguntas
const informacion = datos[3].children[1][3].children[1][3];
const preguntas = informacion?.questions;
// 8. Imprimir preguntas en consola
preguntas.forEach(pregunta => {
console.log(`${pregunta.order}. ${pregunta.question}`);
console.log(`Criterio: ${pregunta.context}`);
console.log('---');
});
// 9. Crear un texto para el PDF
let contenidoParaPDF = '';
preguntas.forEach(pregunta => {
contenidoParaPDF += `${pregunta.order}. ${pregunta.question}\n`;
contenidoParaPDF += `Criterio: ${pregunta.context}\n\n`;
});
// 10. Crear un Blob de tipo texto
const blob = new Blob([contenidoParaPDF], { type: 'text/plain' });
// 11. Crear una URL temporal
const urlTemporal = URL.createObjectURL(blob);
// 12. Crear un iframe oculto para imprimir
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = urlTemporal;
document.body.appendChild(iframe);
// 13. Imprimir automáticamente cuando el iframe cargue
iframe.onload = () => {
iframe.contentWindow.print();
};
} catch (error) {
console.error('No se pudo parsear a JSON:', error.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment