Communitygithub.com

aubarcito48/portfolio

從已建置的網頁應用程式或 React 元件中,透過內嵌 CSS 與圖片,提取出獨立的靜態 HTML。當你需要捕捉特定的 UI 狀態、分享頁面的靜態版本,或準備上傳至 Stitch 的素材時,可使用此技能——即使用戶只說「儲存 HTML」或「模擬畫面」也能派上用場。

portfolio 是什麼?

這個 AI 技能旨在從任何前端框架(如 React、Vue、Next.js 等)的本地開發伺服器中,提取出完全自足的靜態 HTML 檔案。它會自動將所有 CSS(包括動態 Vite/Tailwind 樣式、CSS-in-JS)內嵌至 `<style>` 區塊,並將圖片、圖示字體轉換為 base64 資料 URI,同時移除 `<script>` 標籤與開發用覆蓋層,最終產出一個可直接在瀏覽器中展示或上傳至 Stitch 的單一 HTML 檔案。 該技能支援兩種主要策略:推薦的 Puppeteer 快照(Strategy A)可提供最高保真度,無需額外設定,適用於任何框架,並提供豐富的命令列旗標(如自訂等待時間、視窗大小、暗黑模式、Canvas 內嵌、認證腳本等);而瀏覽器子代理(Strategy B)則適合需要先與頁面互動(點擊、填表)的情境。若應用程式完全無法在本機執行,技能也提供靜態回退方案(MockPage.jsx)作為最後手段。目前已驗證支援 React + Vite、Next.js、Angular、Vue/Nuxt、Svelte/SvelteKit 及 Storybook 等常見框架。

相容平台~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

從任何網頁應用程式中提取出完全自足的靜態 HTML 檔案。

選擇哪種策略

你必須在繼續之前請使用者選擇要使用的策略。清楚呈現各個選項,推薦策略 A 作為預設選項,並為每個選項提供簡短的優缺點摘要,以幫助他們做出明智的決定。

策略 A (Puppeteer)策略 B (瀏覽器子代理)
適用時機應用程式在本機執行,無需認證需要先與頁面互動(點擊、填寫表單)
保真度最高——計算後的樣式已解析高——渲染後的 DOM
設定無需——不需要模擬檔案無需——不需要模擬檔案
框架任何任何
輸出寫入檔案——無大小限制在代理上下文中有可能截斷

[!警告] 檢查點——需要使用者確認。 在繼續之前,你必須詢問使用者他們偏好的策略。 呈現上述的比較表格,推薦策略 A 為預設選項,並 等待明確批准。請勿自行決定,直到使用者確認為止。


策略 A:Puppeteer 快照(推薦)

啟動無頭 Chrome,捕捉完整渲染的 DOM,並產出包含所有內嵌 CSS 以及 base64 圖片的完整 HTML 檔案。適用於任何框架——不需要 MockPage.jsx。

先決條件

  • 應用程式在本機執行中(例如 npm run dev)
  • Node.js 環境中已安裝 puppeteer(檢查:node -e "require('puppeteer')")

工作流程

  1. 啟動應用程式並記下埠號。

    [!警告] 檢查點——需要使用者確認。 在本機伺服器啟動後,你必須暫停並請使用者確認,然後再執行快照腳本或啟動瀏覽器子代理。向使用者報告該 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—匯出預設 async (page) => void 函數的 JS/TS 模組路徑,用於認證
--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.jsSSR 水合需要 --wait 3000。URL:http://localhost:3000。來自 /_next/image 的 <img srcset> 會自動內嵌為 base64。
Angular (@angular/cli / v17+)使用 ng serve 開箱即用(預設 URL:http://localhost:4200)。對於 Angular Material / PrimeNG 動畫水合及懶加載路由,建議使用 --wait 2000。
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(步驟 0)捕捉 CSSOM,且同源圖示字型(@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:透過模組注入(Vue/Vite)呼叫應用程式自身的登入 API
  // 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

    [!警告] 大型頁面可能會被截斷。為了解決這個問題:

    • 在提取之前移除 <style> 標籤:document.querySelectorAll('style').forEach(el => el.remove())
    • 靜態地重新添加樣式(Tailwind CDN 連結、原始 CSS)
  5. 儲存到檔案。


附錄:靜態回退方案 (MockPage.jsx)

[!注意] 此方法是最後的手段,適用於應用程式無法在本機執行的情況(依賴損壞、缺少後端、無繞過方式的認證障礙)。它需要手動將 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:"頁面標題"

重要旗標:--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. 保留標誌——使用帶本地路徑的 <img>(後處理會將其內嵌)
  5. 移除浮動元素——Cookie 橫幅、聊天小工具、意見回饋按鈕

後處理

內嵌本地圖片:

npx tsx <SKILL_DIR>/scripts/post_process.ts \
  .stitch/Page.html --base-dir <應用程式目錄>

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.

相關技能