Created
May 20, 2024 19:53
-
-
Save betillogalvanfbc/1f428dc6a7b3f76457f570c4b280ff4a to your computer and use it in GitHub Desktop.
downloadjs.js
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
// Función para descargar un archivo y guardarlo en la carpeta Downloads | |
async function downloadJSFile(url, filename) { | |
try { | |
const response = await fetch(url); | |
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`); | |
const blob = await response.blob(); | |
const a = document.createElement('a'); | |
a.href = URL.createObjectURL(blob); | |
a.download = filename; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
console.log(`Downloaded: ${filename}`); | |
} catch (err) { | |
console.error(`Error downloading ${filename}:`, err); | |
} | |
} | |
// Obtener todas las solicitudes de archivos .js desde el panel "Network" | |
async function getJSFilesAndDownload() { | |
// Esperar unos segundos para que se capturen todas las solicitudes | |
await new Promise(resolve => setTimeout(resolve, 3000)); | |
// Obtener las entradas de la pestaña "Network" | |
const networkEntries = performance.getEntriesByType('resource') | |
.filter(entry => entry.initiatorType === 'script' && entry.name.endsWith('.js')); | |
if (networkEntries.length === 0) { | |
console.log('No se encontraron archivos .js para descargar.'); | |
return; | |
} | |
// Descargar cada archivo .js | |
for (const [index, entry] of networkEntries.entries()) { | |
const url = entry.name; | |
const filename = url.split('/').pop().split('?')[0] || `script${index + 1}.js`; | |
await downloadJSFile(url, filename); | |
} | |
console.log('Descarga de archivos .js completada.'); | |
} | |
// Llamar a la función para descargar los archivos .js | |
getJSFilesAndDownload(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment