Communitygithub.com

aubarcito48/portfolio

从已构建的Web应用或React组件中提取自包含的静态HTML,通过内联CSS和图片实现。当你需要捕获特定的UI状态、分享页面的静态版本或为Stitch上传准备资源时使用此技能。

portfolio 是什么?

该技能允许开发者将本地运行的Web应用或React组件转换为一个完全自包含的静态HTML文件。它支持两种主要策略:推荐策略A使用Puppeteer以最高保真度捕获渲染后的DOM(包括计算样式、动态CSS和所有资源内联)。策略B则提供一个浏览器子代理,在捕获前可进行页面交互(如点击、填写表单等),但大型页面输出可能被截断。两种策略都显著优于手动创建MockPage.jsx的备用方案。该技能可处理React/Vite、Next.js、Angular、Vue、Svelte等框架,并自动内联CSS、字体、HTML图片srcsrcset和canvas元素。它还支持通过自定义脚本进行页面认证、移除固定元素、暗黑模式、以及处理懒加载页面。其设计目的是让开发者快速捕获UI状态,用于调试、文档、演示或Stitch上传,而无需手动编写静态HTML或Mock组件。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/aubarcito48/portfolio/tree/HEAD/.agents/skills/stitch-extract-static-html

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

提取静态HTML

从任何Web应用中提取一个自包含的静态HTML文件。

选择哪种策略

在继续之前,你必须让用户选择使用哪种策略。清晰地呈现选项,推荐策略A作为首选默认方案,并为每个选项提供简要的优缺点总结,帮助他们做出明智的决定。

策略A(Puppeteer)策略B(浏览器子代理)
适用场景应用本地运行,无认证墙需要先与页面交互(点击、填写表单)
保真度最高——解析了计算样式高——渲染的DOM
设置零——无需Mock文件零——无需Mock文件
框架任意任意
输出写入文件——无大小限制在代理上下文中可能截断

[!WARNING] 检查点——需要用户确认。 在继续之前,你必须询问用户更喜欢哪种策略。 展示上面的对比表格,推荐策略A为默认选项,并 等待用户明确批准。不要自行决定,也不要在用户确认之前继续。


策略A:Puppeteer快照(推荐)

启动无头Chrome,捕获完全渲染的DOM,并生成一个自包含的HTML文件,其中所有CSS已内联,图片以base64形式嵌入。适用于任何框架——无需MockPage.jsx。

前提条件

  • 应用在本地运行(例如 npm run dev)
  • Node.js且puppeteer可用(检查:node -e "require('puppeteer')")

工作流程

  1. 启动应用并记下端口。

    [!WARNING] 检查点——需要用户确认。 启动本地服务器后,你必须暂停并请求用户确认,然后再运行快照脚本或启动浏览器子代理。向用户报告URL和端口,以便他们验证应用是否正在运行并正确渲染。在用户确认之前,不要继续执行快照步骤。

  2. 运行快照脚本:

    npx tsx <SKILL_DIR>/scripts/snapshot.ts \
      --url http://localhost:5173 \
      --output .stitch/home.html \
      --wait 2000
    
  3. 多个页面——每个路由运行一次:

    npx tsx <SKILL_DIR>/scripts/snapshot.ts \
      --url http://localhost:5173 --output .stitch/home.html --wait 2000
    npx tsx <SKILL_DIR>/scripts/snapshot.ts \
      --url http://localhost:5173/pricing --output .stitch/pricing.html --wait 2000
    npx tsx <SKILL_DIR>/scripts/snapshot.ts \
      --url http://localhost:5173/dashboard --output .stitch/dashboard.html --wait 2000 --html-class dark
    
  4. 清理开发服务器: 如果专门为快照提取启动了本地开发服务器,确保在提取完成后停止服务器进程或终止后台任务。

脚本标志

标志默认值描述
--url(必需)要捕获的URL
--output(必需)输出文件路径
--wait1000网络空闲后的额外等待时间(毫秒)。对于懒加载应用请增加。
--viewport1280x800视窗大小,格式为 WIDTHxHEIGHT
--html-class—<html>元素的类(例如 dark)
--remove-fixedfalse移除固定/粘性元素(Cookie横幅、聊天小部件)
--full-heightfalse将视窗大小调整为完整的滚动高度
--title—覆盖页面标题(设置为路由路径,例如 /dashboard 或 /settings/profile)
--auth-script—JS/TS模块路径,该模块导出一个默认的 async (page) => void 函数,用于身份验证
--inline-canvasfalse将 <canvas> 元素(ECharts, Chart.js, D3)转换为base64 <img> 标签

自动完成的功能

  • 从 document.styleSheets 捕获所有CSSOM规则(保留动态的Vite/Tailwind开发样式和CSS-in-JS)
  • 将所有 <link rel="stylesheet"> 内联为 <style> 块
  • 将 <img> 的 src 和 srcset 转换为base64数据URI(跳过外部字体)
  • 将同源和相对的图标字体文件(@font-face)内联为base64数据URI,确保连字永远不会渲染为ASCII文本
  • 内联 <source srcset> URL为base64
  • 移除失败/无效的 srcset 条目,使浏览器回退到已内联的 src
  • 移除 <script> 标签、Vite HMR开发样式块(createHotContext、import.meta.hot)和开发覆盖层
  • 在CSS url() 路径内联之前解析相对路径

框架说明

框架说明
React + Vite开箱即用。--wait 1000。
Next.js需要 --wait 3000 等待SSR水合。URL: http://localhost:3000。来自 /_next/image 的 <img srcset> 自动内联为base64。
Angular (@angular/cli / v17+)使用 ng serve 开箱即用(默认URL:http://localhost:4200)。需要 --wait 2000 等待Angular Material / PrimeNG动画水合和懒加载路由。
Vue / Nuxt开箱即用。
Svelte / SvelteKit开箱即用。
Storybook使用故事URL:--url http://localhost:6006/?path=/story/...
SSR (Webpack)可能需要更长的 --wait。

故障排除

问题解决方案
图片缺失增加 --wait
服务器停止后图片显示为损坏验证 srcset 是否已内联——检查日志中的“Inlined N images”。如果 srcset URL失败,它们会被自动移除,以便使用已内联的 src。
图标显示为文本 / 无衬线未样式字体确保 snapshot.ts 从 document.styleSheets 捕获CSSOM(步骤0),并且同源图标字体(@font-face)作为base64数据URI内联。
Next.js /_next/image 未内联确保运行快照时开发服务器正在运行——脚本会从正在运行的服务器获取优化后的图片。
深色模式未应用--html-class dark
输出中有Cookie横幅--remove-fixed
页面需要登录使用 --auth-script ./auth.ts(参见下面的认证页面)
图表/图形显示为空白框使用 --inline-canvas 将 <canvas> 序列化为base64 <img>
Cannot find module 'puppeteer'npm install -g puppeteer

认证页面

对于有登录守卫的应用(Vue Router beforeEach,React ProtectedRoute 等),创建一个小型认证脚本,在Puppeteer会话中运行:

// auth-myapp.ts
import type { Page } from 'puppeteer';

export default async function authenticate(page: Page) {
  // 示例1:填写并提交登录表单
  await page.type('#username', 'admin');
  await page.type('#password', 'password123');
  await page.click('#login-button');
  await page.waitForNavigation({ waitUntil: 'networkidle2' });

  // 示例2:直接注入cookie/localStorage
  // await page.evaluate(() => {
  //   localStorage.setItem('token', 'mock-jwt-token');
  // });

  // 示例3:通过模块注入调用应用自身的登录API(Vue/Vite)
  // await page.evaluate(() => {
  //   return new Promise((resolve) => {
  //     const script = document.createElement('script');
  //     script.type = 'module';
  //     script.textContent = `
  //       import { useUserStore } from '/src/store/modules/user.ts';
  //       import { fetchLogin } from '/src/api/auth.ts';
  //       const res = await fetchLogin({ userName: 'Admin', password: '123456' });
  //       useUserStore().setToken(res.token, res.refreshToken);
  //       window.dispatchEvent(new CustomEvent('auth-done'));
  //     `;
  //     document.head.appendChild(script);
  //     window.addEventListener('auth-done', () => resolve(true), { once: true });
  //   });
  // });
}

然后使用它:

npx tsx <SKILL_DIR>/scripts/snapshot.ts \
  --url http://localhost:5173/#/dashboard \
  --output .stitch/dashboard.html \
  --auth-script ./auth-myapp.ts \
  --inline-canvas \
  --wait 5000

脚本会首先导航到 --url(可能会重定向到登录页面),然后运行你的认证函数,最后重新导航到原始的 --url,此时会话已通过认证。


策略B:浏览器子代理捕获

当你需要在捕获前与页面交互(点击按钮、填写表单、导航标签)时使用。浏览器子代理能提供完全的控制,但对于大型页面输出可能会被截断。

工作流程

  1. 在本地启动应用。

  2. 使用浏览器子代理导航。

  3. 根据需要交互(点击、滚动、填写表单)。

  4. 提取DOM:document.documentElement.outerHTML

    [!WARNING] 大型页面可能被截断。为处理此问题:

    • 在提取前移除 <style> 标签:document.querySelectorAll('style').forEach(el => el.remove())
    • 静态重新添加样式(Tailwind CDN链接,源CSS)
  5. 保存到文件。


附录:静态备用方案(MockPage.jsx)

[!NOTE] 此方法是在应用无法本地运行(依赖损坏、缺少后端、无法绕过的认证墙等)时的最后手段。它需要手动将React组件扁平化为单个JSX文件。尽可能优先使用策略A。

何时使用

  • 应用完全无法本地运行
  • 页面需要认证且无模拟/绕过方式
  • 你需要通过导航无法到达的特定UI状态(错误屏幕、空状态)

快速参考

npx tsx <SKILL_DIR>/scripts/extract_inline_html.ts \
  --index-css src/css/App.css \
  --extra-css index.html \
  --outdir .stitch \
  --page src/MockPage.jsx:Page.html:"Page Title"

关键标志:--no-tailwind(非Tailwind应用),--html-class dark(深色模式),--css-files(额外CSS文件)。

自动检测:Tailwind配置会被自动检测。@apply 指令自动使用 <style type="text/tailwindcss">。

MockPage.jsx 规则

  1. 包含完整布局——页眉、侧边栏、页脚(先阅读 App.js)
  2. 扁平化所有条件逻辑——选择一个状态,移除所有三元表达式和 && 守卫
  3. 硬编码所有数据——将 {variable} 替换为具体值,展开 .map() 循环
  4. 保留Logo——使用带有本地路径的 <img>(后处理会将它们内联)
  5. 移除浮动元素——Cookie横幅、聊天小部件、反馈按钮

后处理

内联本地图片:

npx tsx <SKILL_DIR>/scripts/post_process.ts \
  .stitch/Page.html --base-dir <app-directory>

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

aubarcito48/portfolio

Transforms vague UI ideas into polished, Stitch-optimized prompts. Enhances specificity, adds UI/UX keywords, injects design system context, and structures output for better generation results.

aubarcito48/portfolio

>- Convert frontend code (Vite, React, Angular, Vue, etc.) to a Stitch Design by chaining static HTML extraction, design system extraction, and file upload. **ALWAYS** use this skill when the user's intent is to move existing web apps or React/Angular/Vue components into Stitch (e.g., requests to "save", "migrate", or "upload"). You must use this skill even for simple "save" operations, as it is the only way to ensure the design system is extracted and assets are properly linked.

aubarcito48/portfolio

>- Extract a comprehensive design system (DESIGN.md) directly from frontend source code — React, Vue, Svelte, Angular, plain HTML/CSS, or any web framework. Analyzes component files, stylesheets, Tailwind configs, theme definitions, and design tokens to produce a rich, Stitch-compatible design system document. Use this skill whenever the user wants to reverse-engineer a design system from an existing codebase, audit the visual language of a project, extract design tokens from source files, or understand the styling patterns in a frontend repo — even if they just say "what does this app look like?" or "pull out the design from this code.

aubarcito48/portfolio

>- Generate new screens from text prompts or images, edit existing screens with prompts and design system tokens, and generate design variants using Stitch MCP. Includes prompt enhancement pipeline, design mappings, professional UI/UX terminology, design tokens and theme system capabilities.

相关技能