ページ内にnotion-page-contentというクラスのDIVタグが存在する場合は、ページ全体ではなく、DIVの子要素を取得して音声化するようにしてください。
以下は、Chrome拡張機能を作成するための手順とコードです。この拡張機能は、指定された条件に基づいてWebページの内容を取得し、VoiceVoxで音声化するリクエストを送信します。
http://127.0.0.1:50021
で動作します。manifest.json
popup.html
popup.js
content.js
background.js
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"
}
}
}
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>
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"]
}
);
});
content.js