Claude Codeでブラウザ拡張機能を開発する方法
Claude Codeを活用してChrome拡張機能を効率的に開発。manifest.json設定からcontent script、popup UIまで実践コードで解説。
ブラウザ拡張機能の開発をClaude Codeで加速する
Chrome拡張機能の開発は、Manifest V3の仕様理解、content script、background service worker、popup UIなど多くの知識が必要です。Claude Codeならこれらを自然言語で指示するだけで生成できます。
プロジェクトの初期設定
> Chrome拡張機能のプロジェクトを作って。
> Manifest V3で、popup画面とcontent scriptを使う構成にして。
> TypeScript + Viteでビルドできるようにして。
Claude Codeが生成するmanifest.jsonの例:
{
"manifest_version": 3,
"name": "My Extension",
"version": "1.0.0",
"description": "Claude Codeで作ったChrome拡張機能",
"permissions": ["storage", "activeTab"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"css": ["content.css"]
}
],
"background": {
"service_worker": "background.js",
"type": "module"
}
}
Popup UIの実装
拡張アイコンをクリックしたときに表示されるポップアップです。
// popup.ts
interface Settings {
enabled: boolean;
highlightColor: string;
fontSize: number;
}
const defaultSettings: Settings = {
enabled: true,
highlightColor: "#ffeb3b",
fontSize: 14,
};
async function loadSettings(): Promise<Settings> {
const result = await chrome.storage.sync.get("settings");
return { ...defaultSettings, ...result.settings };
}
async function saveSettings(settings: Settings) {
await chrome.storage.sync.set({ settings });
// content scriptに設定変更を通知
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab.id) {
chrome.tabs.sendMessage(tab.id, { type: "SETTINGS_UPDATED", settings });
}
}
document.addEventListener("DOMContentLoaded", async () => {
const settings = await loadSettings();
const enabledCheckbox = document.getElementById("enabled") as HTMLInputElement;
const colorInput = document.getElementById("color") as HTMLInputElement;
const fontSizeInput = document.getElementById("fontSize") as HTMLInputElement;
enabledCheckbox.checked = settings.enabled;
colorInput.value = settings.highlightColor;
fontSizeInput.value = String(settings.fontSize);
enabledCheckbox.addEventListener("change", () => {
settings.enabled = enabledCheckbox.checked;
saveSettings(settings);
});
colorInput.addEventListener("input", () => {
settings.highlightColor = colorInput.value;
saveSettings(settings);
});
fontSizeInput.addEventListener("input", () => {
settings.fontSize = parseInt(fontSizeInput.value);
saveSettings(settings);
});
});
Content Scriptの実装
Webページの内容を操作するcontent scriptです。ここではページ内テキストのハイライト機能を実装します。
// content.ts
let settings: Settings = {
enabled: true,
highlightColor: "#ffeb3b",
fontSize: 14,
};
function highlightText(searchTerm: string) {
if (!settings.enabled || !searchTerm) return;
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
null
);
const matches: { node: Text; index: number }[] = [];
while (walker.nextNode()) {
const node = walker.currentNode as Text;
const index = node.textContent?.toLowerCase().indexOf(searchTerm.toLowerCase()) ?? -1;
if (index !== -1) {
matches.push({ node, index });
}
}
matches.reverse().forEach(({ node, index }) => {
const range = document.createRange();
range.setStart(node, index);
range.setEnd(node, index + searchTerm.length);
const highlight = document.createElement("mark");
highlight.style.backgroundColor = settings.highlightColor;
highlight.className = "ext-highlight";
range.surroundContents(highlight);
});
}
function clearHighlights() {
document.querySelectorAll(".ext-highlight").forEach((el) => {
const parent = el.parentNode;
if (parent) {
parent.replaceChild(document.createTextNode(el.textContent || ""), el);
parent.normalize();
}
});
}
// メッセージリスナー
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.type === "HIGHLIGHT") {
clearHighlights();
highlightText(message.term);
sendResponse({ success: true });
}
if (message.type === "SETTINGS_UPDATED") {
settings = message.settings;
}
});
Background Service Workerの実装
// background.ts
chrome.runtime.onInstalled.addListener(() => {
// コンテキストメニューの追加
chrome.contextMenus.create({
id: "highlight-selection",
title: "選択テキストをハイライト",
contexts: ["selection"],
});
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "highlight-selection" && tab?.id) {
await chrome.tabs.sendMessage(tab.id, {
type: "HIGHLIGHT",
term: info.selectionText,
});
}
});
CLIツール開発との違いについてはCLIツール開発を参照してください。効率的なプロンプトの書き方は生産性を3倍にする10のTipsをご覧ください。
まとめ
Claude Codeを使えば、Manifest V3の設定からUI、content script、background workerまで、Chrome拡張機能の開発を大幅にスピードアップできます。仕様の複雑さをClaude Codeに任せて、機能のアイデアに集中しましょう。
詳しくはClaude Code公式ドキュメントを参照してください。
無料PDF: Claude Code はじめてのチートシート
まずは無料PDFで基本コマンドと最初の使い方をまとめて確認してください。登録後はそのままテンプレート集や導入相談にも進めます。
スパムは送りません。登録情報は厳重に管理します。
Claude Codeを仕事で使える形にしませんか?
無料PDFで基礎を固めたあと、すぐ使えるテンプレート集で試し、必要なら業務自動化や導入相談まで進められます。
この記事を書いた人
Masa
現役DX室長|Claude Code でゼロから多言語AI技術メディア運営中。実務直結の自動化、AI開発相談・研修受付中。
関連書籍・参考図書
この記事のテーマに関連する書籍を楽天ブックスで探せます。
※ 当サイトは楽天市場のアフィリエイトプログラムに参加しています。上記リンクから商品をご購入いただくと、運営者に紹介料が支払われる場合があります。
関連記事
Claude Codeで多言語記事を毎日公開するための7つのデプロイ前チェック
日本語だけ公開して終わらせないために、Claude Codeで多言語記事を毎日出す前に確認したい7つのチェックを実例つきで整理しました。
Codex AutomationsでAIに毎日のコンテンツ運用を任せる方法
Codex Automationsを使って、アクセス確認、記事改善、CTA改善、デプロイ、公開確認までを毎日の運用フローとして回す方法を解説します。
Claude Code × GCP Cloud Functions 完全ガイド|サーバーレス関数を爆速開発
GCP Cloud FunctionsをClaude Codeで効率化。HTTP/Pub/Sub/Firestoreトリガーの実装からローカルテスト・デプロイ自動化まで、Masaの実務経験をもとに実例コードで解説。