Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Mar 14, 2024
1 parent b45273d commit abb93e6
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import delay from "delay";
import translator from "./translator/index.js";
import tts from "./tts/index.js";
import * as util from "/src/util";
import { debounce } from "lodash";

var setting;
var recentTranslated = "";
Expand Down Expand Up @@ -270,39 +269,37 @@ function addInstallUrl(url) {

// tts=============================================================================

const playTtsQueue = debounce(
async ({
sourceText,
sourceLang,
targetText,
targetLang,
voiceTarget,
voiceRepeat,
}) => {
var sourceText = util.filterEmoji(sourceText);
var targetText = util.filterEmoji(targetText);
var ttsTarget = voiceTarget || setting["voiceTarget"];
var ttsRepeat = voiceRepeat || setting["voiceRepeat"];
ttsRepeat = Number(ttsRepeat);
stopTts();
await delay(50);
var startTimeStamp = Date.now();
for (var i = 0; i < ttsRepeat; i++) {
if (ttsTarget == "source") {
await playTts(sourceText, sourceLang, startTimeStamp);
} else if (ttsTarget == "target") {
await playTts(targetText, targetLang, startTimeStamp);
} else if (ttsTarget == "sourcetarget") {
await playTts(sourceText, sourceLang, startTimeStamp);
await playTts(targetText, targetLang, startTimeStamp);
} else if (ttsTarget == "targetsource") {
await playTts(targetText, targetLang, startTimeStamp);
await playTts(sourceText, sourceLang, startTimeStamp);
}
async function playTtsQueue({
sourceText,
sourceLang,
targetText,
targetLang,
voiceTarget,
voiceRepeat,
}) {
var sourceText = util.filterEmoji(sourceText);
var targetText = util.filterEmoji(targetText);
var ttsTarget = voiceTarget || setting["voiceTarget"];
var ttsRepeat = voiceRepeat || setting["voiceRepeat"];
ttsRepeat = Number(ttsRepeat);
stopTts();
await delay(10);

var startTimeStamp = Date.now();
for (var i = 0; i < ttsRepeat; i++) {
if (ttsTarget == "source") {
await playTts(sourceText, sourceLang, startTimeStamp);
} else if (ttsTarget == "target") {
await playTts(targetText, targetLang, startTimeStamp);
} else if (ttsTarget == "sourcetarget") {
await playTts(sourceText, sourceLang, startTimeStamp);
await playTts(targetText, targetLang, startTimeStamp);
} else if (ttsTarget == "targetsource") {
await playTts(targetText, targetLang, startTimeStamp);
await playTts(sourceText, sourceLang, startTimeStamp);
}
},
100
);
}
}

function stopTts() {
stopTtsTimestamp = Date.now();
Expand Down

0 comments on commit abb93e6

Please sign in to comment.