現在表示しているWebページの全テキストを取得し、PCにインストールされているVoiceVoxを使用して音声化し、ダウンロードするChrome拡張を作成してください。

ページ内にnotion-page-contentというクラスのDIVタグが存在する場合は、ページ全体ではなく、DIVの子要素を取得して音声化するようにしてください。

以下は、Chrome拡張機能を作成するための手順とコードです。この拡張機能は、指定された条件に基づいてWebページの内容を取得し、VoiceVoxで音声化するリクエストを送信します。


必要な準備

  1. VoiceVoxのセットアップ: PCにVoiceVoxをインストールし、ローカルサーバーを有効にしてください。
  2. Chrome拡張の基本構造:

1. manifest.json

Chrome拡張の設定ファイルです。

{
  "manifest_version": 3,
  "name": "VoiceVox Text-to-Speech",
  "version": "1.0",
  "description": "Webページまたは特定のDIV要素を取得してVoiceVoxで音声化します。",
  "permissions": ["activeTab", "scripting", "downloads"],
  "host_permissions": ["<all_urls>"],
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "icon.png",
      "48": "icon.png",
      "128": "icon.png"
    }
  }
}


2. popup.html

ユーザーインターフェイスを定義します。

<!DOCTYPE html>
<html>
<head>
  <title>VoiceVox TTS</title>
  <script src="popup.js" defer></script>
</head>
<body>
  <h1>VoiceVox TTS</h1>
  <button id="convert">音声化</button>
</body>
</html>


3. popup.js

ボタンがクリックされたときに、現在のページからテキストを取得します。

document.getElementById("convert").addEventListener("click", async () => {
  const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

  chrome.scripting.executeScript(
    {
      target: { tabId: tab.id },
      files: ["content.js"]
    }
  );
});


4. content.js