Skip to content

Commit

Permalink
Change chrome tts api to Web Speech API
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitrino committed Aug 14, 2024
1 parent fd2dda6 commit 6ff6c25
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const manifest = {
css: ["assets/css/contentStyle<KEY>.chunk.css"],
},
],
permissions: ["scripting", "storage", "tts", "activeTab"],
permissions: ["scripting", "storage", "activeTab"],
optional_host_permissions: ["*://*/*"],
optional_permissions: [],
host_permissions: ["https://translate.google.com/*", "http://localhost:8765/*", "https://api.lingualeo.com/*"],
Expand Down
9 changes: 0 additions & 9 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,5 @@ chrome.runtime.onMessage.addListener(function (message, _sender, sendResponse) {
});
}

if (message.type === "speak") {
console.log("speak request: ", message);
chrome.tts.speak(message.text, { lang: message.lang }, function () {
if (chrome.runtime.lastError) {
console.log("Error: " + chrome.runtime.lastError.message);
}
});
}

return true;
});
6 changes: 5 additions & 1 deletion src/pages/content/components/Subs/SubItemTranslation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export const SubItemTranslation: FC<{ text: string }> = ({ text }) => {
};

const handlePlaySound = async () => {
await chrome.runtime.sendMessage({ type: "speak", text: currentWordTranslation.source, lang: subsLanguage });
const msg = new SpeechSynthesisUtterance();
msg.text = currentWordTranslation.source;
msg.lang = subsLanguage;
msg.rate = 0.8;
window.speechSynthesis.speak(msg);
};

if (subsLanguage === translateLanguage) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Popup = () => {
const handleRequestPermissions = async () => {
const tab = await getTab();
const isGranted = await chrome.permissions.request({
permissions: ["scripting", "storage", "tts", "activeTab"],
permissions: ["scripting", "storage", "activeTab"],
origins: [tab.url],
});
if (isGranted) {
Expand Down

0 comments on commit 6ff6c25

Please sign in to comment.