Frontend

Make Chrome Extension - manifest.json

rachel_13 2025. 4. 7. 20:06

🧠 크둬 μ΅μŠ€ν…μ…˜ ꡬ쑰 κ°œλ… 정리

크둬 μ΅μŠ€ν…μ…˜μ€ 기본적으둜 μ›Ή 기술둜 λ§Œλ“€ 수 μžˆλŠ” μž‘μ€ 앱이닀.

기본적인 ꡬ성 μš”μ†ŒλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

 

- mainfest.json : μ΅μŠ€ν…μ…˜ μ„€μ • 파일 (ν•„μˆ˜) (이 파일이 μžˆμ–΄μ•Ό 크둬 μ΅μŠ€ν…μ…˜μœΌλ‘œ 인식할 수 μžˆλ‹€.)

- popup : 크둬 νˆ΄λ°”μ—μ„œ μ•„μ΄μ½˜μ„ ν΄λ¦­ν•˜λ©΄ λ‚˜μ˜€λŠ” UI

- background script : λ°±κ·ΈλΌμš΄λ“œμ—μ„œ 계속 λ™μž‘ν•˜λŠ” JS

- content script : μ›Ή νŽ˜μ΄μ§€μ— μ‚½μž…λ˜λŠ” JS

- options page : μ‚¬μš©μž μ„€μ • νŽ˜μ΄μ§€

- permissions : μ–΄λ–€ κΈ°λŠ₯을 μ‚¬μš©ν• μ§€ μ„€μ • (예: νƒ­ μ ‘κ·Ό, μŠ€ν† λ¦¬μ§€)

 

μ˜ˆμ‹œ)

{
  "manifest_version": 3, // 버전 3 μ‚¬μš© (버전 2λŠ” 지원 μ’…λ£Œ)
  "name": "My First Extension", // μ΅μŠ€ν…μ…˜ 이름
  "version": "0.1.0", // ν˜„μž¬ 버전 (μ•± 처럼 관리됨)
  "description": "This is a beginner-friendly Chrome extension.", // μ΅μŠ€ν…μ…˜ μ„€λͺ…
  "icons": { // ν™•μž₯ ν”„λ‘œκ·Έλž¨ μ•„μ΄μ½˜ 파일 경둜
    "16": "assets/icon-16.png",
    "48": "assets/icon-48.png",
    "128": "assets/icon-128.png"
  },
  "action": { // 크둬 νˆ΄λ°”μ—μ„œ ν΄λ¦­ν–ˆμ„ λ•Œ λ‚˜μ˜€λŠ” νŒμ—… μ„€μ •
    "default_popup": "src/popup.html", // μ•„μ΄μ½˜ 클릭 μ‹œ ν‘œμ‹œλ  HTML 파일 경둜
    "default_icon": "assets/icon-48.png" // μ•„μ΄μ½˜ 파일 경둜
  },
  "background": { 
    "service_worker": "src/background.js" // λ°±κ·ΈλΌμš΄λ“œμ—μ„œ 계속 μ‹€ν–‰λ˜λŠ” 슀크립트
  },
  "permissions": ["storage", "tabs"], // μ΅μŠ€ν…μ…˜μ΄ μ‚¬μš©ν•˜λ €λŠ” κΈ°λŠ₯λ“€
  // storage: 데이터 μ €μž₯ , tabs: νƒ­ μ‘°μž‘
  "host_permissions": ["https://example.com/*"], // νŠΉμ • 도메인 μ ‘κ·Ό κΆŒν•œ μ„€μ •
  "content_scripts": [ // μ›Ή νŽ˜μ΄μ§€μ— μ‚½μž…λ˜λŠ” JS μ„€μ •
    {
      "matches": ["https://*/*"], // μ–΄λ–€ URLμ—μ„œ μ½˜ν…μΈ  슀크립트λ₯Ό μ‹€ν–‰ν• μ§€ λͺ…μ‹œ
      "js": ["src/content.js"] // μ‹€ν–‰ν•  μžλ°”μŠ€ν¬λ¦½νŠΈ 파일 경둜
    }
  ]
}

 

 

🧩 μ™œ popup.html을 써야 ν• κΉŒ?

λ‚˜λŠ” React 둜 ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν–ˆκΈ° λ•Œλ¬Έμ—, λ‚΄ 경우 src/Popup.tsx μ•ˆμ— νŒμ—… μ»΄ν¬λ„ŒνŠΈλ₯Ό μž‘μ„±ν•΄λ‘μ—ˆλ‹€.

 

그런데, 크둬 μ΅μŠ€ν…μ…˜μ€ default_popup에 HTML 파일 경둜만 받을 수 μžˆλ‹€κ³  ν•œλ‹€.

React μ»΄ν¬λ„ŒνŠΈ(popup.tsx)λŠ” λΈŒλΌμš°μ €κ°€ 직접 이해할 수 μ—†κΈ° λ•Œλ¬Έμ—, λ°˜λ“œμ‹œ HTML둜 κ°μ‹Έμ€˜μ•Ό ν•œλ‹€.

 

즉, popup.tsxλŠ” 직접 넣을 수 μ—†κ³ ,

"action": {
  "default_popup": "popup.html"
}

 

이런 μ‹μœΌλ‘œ 써야 ν•˜κ³ , popup.html μ•ˆμ—μ„œ React μ•±(popup.tsx)을 λ§ˆμš΄νŠΈν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²°ν•  수 μžˆλ‹€.

 


πŸ“ ꡬ쑰 μ˜ˆμ‹œ

src/
β”œβ”€β”€ popup.tsx
β”œβ”€β”€ popup.html

 

popup.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Popup</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/popup.tsx"></script>
  </body>
</html>

 

popup.tsx

import React from 'react'
import ReactDOM from 'react-dom/client'

function Popup() {
  return <div className="text-xl text-blue-500">Hello from Popup!</div>
}

const root = document.getElementById('root')!
ReactDOM.createRoot(root).render(<Popup />)

 


πŸ›  Vite μ„€μ • 팁

 

ViteλŠ” 기본적으둜 index.html을 κΈ°μ€€μœΌλ‘œ μž‘λ™ν•˜μ§€λ§Œ, μ—¬λŸ¬ 개의 entry HTML을 λ§Œλ“€ 수 μžˆλ‹€.

 

// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'

export default defineConfig({
  plugins: [react()],
  build: {
    rollupOptions: {
      input: {
        popup: resolve(__dirname, 'src/popup.html'),
        // background μŠ€ν¬λ¦½νŠΈλ‚˜ content scriptλŠ” entry만 λ“±λ‘ν•΄μ€˜λ„ 됨
      },
      output: {
        // 폴더 ꡬ쑰 κΉ”λ”ν•˜κ²Œ μ •λ¦¬ν•˜κ³  μ‹Άλ‹€λ©΄ μ—¬κΈ°μ„œ μ‘°μ •
      }
    },
    outDir: 'dist',
  },
})

 

πŸš€ 디버깅 ν•˜λŠ” 방법

 

πŸ“Œ κ°€μ΄λ“œ

- λ¦¬μ•‘νŠΈλ‘œ κ°œλ°œν•˜κ³  μžˆλ‹€λ©΄, build ν›„, dist/ 폴더에도 manifest.json을 λ³΅μ‚¬ν•œλ‹€.

- 크둬 μ£Όμ†Œμ°½μ— chrome://extensionsλ₯Ό μž…λ ₯ν•œλ‹€.

 

- "개발자 λͺ¨λ“œ"λ₯Ό ν™œμ„±ν™”ν•œλ‹€.

우츑 상단 ν† κΈ€ on


- "μ••μΆ•ν•΄μ œλœ ν™•μž₯ ν”„λ‘œκ·Έλž¨μ„ λ‘œλ“œν•©λ‹ˆλ‹€" λ²„νŠΌμ„ ν΄λ¦­ν•œλ‹€. (Load unpacked λ²„νŠΌ 클릭)
- ν”„λ‘œμ νŠΈ 폴더λ₯Ό μ„ νƒν•œλ‹€. (둜컬 λ””λ°”μ΄μŠ€μ˜ dist 폴더 경둜λ₯Ό μ°Ύμ•„μ„œ μ—…λ‘œλ“œν•΄ μ€€λ‹€. )
- μ •μƒμ μœΌλ‘œ λ‘œλ“œλ˜μ—ˆλ‹€λ©΄ νˆ΄λ°”μ— μ•„μ΄μ½˜μ΄ λ‚˜νƒ€λ‚  것이닀.

 

 

πŸ“Œ μ°Έκ³  사항

background와 content_scriptsλŠ” 각각 λ³„λ„μ˜ 디버깅 μ½˜μ†”μ„ κ°€μ§„λ‹€.