Descargar Código Web ZIP

Guarda el código fuente de cualquier página web en un archivo ZIP manteniendo su estructura de carpetas.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Descargar Código Web ZIP
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Guarda el código fuente de cualquier página web en un archivo ZIP manteniendo su estructura de carpetas.
// @author       TuNombre
// @match        *://*/*
// @grant        GM_download
// @require      https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js
// @locale       es
// ==/UserScript==

(function() {
    'use strict';

    async function fetchResource(url) {
        try {
            let response = await fetch(url);
            if (!response.ok) throw new Error(`Error al obtener ${url}`);
            return await response.text();
        } catch (error) {
            console.error(error);
            return `// No se pudo obtener: ${url}`;
        }
    }

    function getRelativePath(url) {
        let urlObj = new URL(url, window.location.origin);
        return urlObj.pathname.startsWith('/') ? urlObj.pathname.substring(1) : urlObj.pathname;
    }

    async function downloadPageAsZip() {
        let zip = new JSZip();
        let hostname = window.location.hostname;

        // Obtener HTML principal
        let htmlContent = document.documentElement.outerHTML;
        zip.file("index.html", htmlContent);

        // Obtener archivos CSS y JS externos
        let resources = [...document.querySelectorAll('link[rel="stylesheet"], script[src]')];
        for (let res of resources) {
            let url = res.href || res.src;
            let relativePath = getRelativePath(url);
            let content = await fetchResource(url);
            zip.file(relativePath, content);
        }

        // Generar y descargar el ZIP
        zip.generateAsync({ type: "blob" }).then(function(content) {
            let a = document.createElement("a");
            a.href = URL.createObjectURL(content);
            a.download = `${hostname}_code.zip`;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        });
    }

    // Crear botón en la interfaz
    let button = document.createElement("button");
    button.innerText = "Descargar Código ZIP";
    button.style.position = "fixed";
    button.style.top = "10px";
    button.style.right = "10px";
    button.style.zIndex = "9999";
    button.style.padding = "10px";
    button.style.backgroundColor = "#007bff";
    button.style.color = "white";
    button.style.border = "none";
    button.style.cursor = "pointer";
    document.body.appendChild(button);
    
    button.addEventListener("click", downloadPageAsZip);
})();