Variant Export

Export the content generated by the variant as an HTML file

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Variant Export
// @description Export the content generated by the variant as an HTML file
// @namespace   Violentmonkey Scripts
// @match       https://variant.com/*
// @match       https://variant.ai/*
// @grant       none
// @version     1.0.1
// @author      skye-z
// @license     MIT
// @description 2026/3/31 17:14:19
// ==/UserScript==

(function() {
    'use strict';

    const buttonStyle = `
        position: absolute;
        bottom: 5px;
        left: 5px;
        z-index: 9999;
        padding: 4px 8px;
        background: #409eff;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 12px;
        opacity: 0.2;
        transition: opacity 0.2s;
    `;
    const hoverStyle = `
        opacity: 1;
        background: #337ecc;
    `;

    function createExportButton(iframe) {
        if (iframe.dataset.exportBtnAdded) return;

        if (window.getComputedStyle(iframe).position === 'static') {
            iframe.style.position = 'relative';
        }

        const exportBtn = document.createElement('button');
        exportBtn.innerText = 'Export';
        exportBtn.style.cssText = buttonStyle;
        exportBtn.dataset.iframeId = iframe.id || `iframe-${Date.now()}`;

        exportBtn.addEventListener('mouseover', () => {
            exportBtn.style.cssText = buttonStyle + hoverStyle;
        });
        exportBtn.addEventListener('mouseout', () => {
            exportBtn.style.cssText = buttonStyle;
        });

        exportBtn.addEventListener('click', () => {
            try {
                let iframeDoc;
                try {
                    iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
                } catch (e) {
                    alert('Export error: CORS');
                    return;
                }

                const htmlContent = `<!DOCTYPE html>${iframeDoc.documentElement.outerHTML}`;
                const blob = new Blob([htmlContent], { type: 'text/html' });
                const downloadLink = document.createElement('a');
                downloadLink.href = URL.createObjectURL(blob);
                const fileName = `iframe-export-${new Date().getTime()}.html`;
                downloadLink.download = fileName;
                downloadLink.click();
                URL.revokeObjectURL(downloadLink.href);
            } catch (error) {
                alert(`Export error: ${error.message}`);
            }
        });

        iframe.parentNode.appendChild(exportBtn);
        iframe.dataset.exportBtnAdded = 'true';

        const observer = new MutationObserver(() => {
            if (!document.body.contains(iframe)) {
                exportBtn.remove();
                observer.disconnect();
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    function initExportButtons() {
        const iframes = document.querySelectorAll('iframe');
        iframes.forEach(iframe => {
            createExportButton(iframe);
        });
    }

    const iframeObserver = new MutationObserver((mutations) => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.tagName === 'IFRAME') {
                    createExportButton(node);
                }
                if (node.querySelectorAll) {
                    const newIframes = node.querySelectorAll('iframe');
                    newIframes.forEach(iframe => {
                        createExportButton(iframe);
                    });
                }
            });
        });
    });

    iframeObserver.observe(document.body, {
        childList: true,
        subtree: true
    });

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initExportButtons);
    } else {
        initExportButtons();
    }
})();