fix: subtitle
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { logger } from "../libs/log.js";
|
import { logger } from "../libs/log.js";
|
||||||
import { truncateWords } from "../libs/utils.js";
|
import { truncateWords } from "../libs/utils.js";
|
||||||
|
import { apiTranslate } from "../apis/index.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class BilingualSubtitleManager
|
* @class BilingualSubtitleManager
|
||||||
@@ -8,7 +9,6 @@ import { truncateWords } from "../libs/utils.js";
|
|||||||
export class BilingualSubtitleManager {
|
export class BilingualSubtitleManager {
|
||||||
#videoEl;
|
#videoEl;
|
||||||
#formattedSubtitles = [];
|
#formattedSubtitles = [];
|
||||||
#translationService;
|
|
||||||
#captionWindowEl = null;
|
#captionWindowEl = null;
|
||||||
#paperEl = null;
|
#paperEl = null;
|
||||||
#currentSubtitleIndex = -1;
|
#currentSubtitleIndex = -1;
|
||||||
@@ -20,14 +20,12 @@ export class BilingualSubtitleManager {
|
|||||||
* @param {object} options
|
* @param {object} options
|
||||||
* @param {HTMLVideoElement} options.videoEl - 页面上的 video 元素。
|
* @param {HTMLVideoElement} options.videoEl - 页面上的 video 元素。
|
||||||
* @param {Array<object>} options.formattedSubtitles - 已格式化好的字幕数组。
|
* @param {Array<object>} options.formattedSubtitles - 已格式化好的字幕数组。
|
||||||
* @param {(text: string, toLang: string) => Promise<string>} options.translationService - 外部翻译函数。
|
|
||||||
* @param {object} options.setting - 配置对象,如目标翻译语言。
|
* @param {object} options.setting - 配置对象,如目标翻译语言。
|
||||||
*/
|
*/
|
||||||
constructor({ videoEl, formattedSubtitles, translationService, setting }) {
|
constructor({ videoEl, formattedSubtitles, setting }) {
|
||||||
this.#setting = setting;
|
this.#setting = setting;
|
||||||
this.#videoEl = videoEl;
|
this.#videoEl = videoEl;
|
||||||
this.#formattedSubtitles = formattedSubtitles;
|
this.#formattedSubtitles = formattedSubtitles;
|
||||||
this.#translationService = translationService;
|
|
||||||
|
|
||||||
this.onTimeUpdate = this.onTimeUpdate.bind(this);
|
this.onTimeUpdate = this.onTimeUpdate.bind(this);
|
||||||
this.onSeek = this.onSeek.bind(this);
|
this.onSeek = this.onSeek.bind(this);
|
||||||
@@ -309,7 +307,7 @@ export class BilingualSubtitleManager {
|
|||||||
subtitle.isTranslating = true;
|
subtitle.isTranslating = true;
|
||||||
try {
|
try {
|
||||||
const { fromLang, toLang, apiSetting } = this.#setting;
|
const { fromLang, toLang, apiSetting } = this.#setting;
|
||||||
const { trText } = await this.#translationService({
|
const { trText } = await apiTranslate({
|
||||||
text: subtitle.text,
|
text: subtitle.text,
|
||||||
fromLang,
|
fromLang,
|
||||||
toLang,
|
toLang,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { logger } from "../libs/log.js";
|
import { logger } from "../libs/log.js";
|
||||||
import { apiSubtitle, apiTranslate } from "../apis/index.js";
|
import { apiSubtitle } from "../apis/index.js";
|
||||||
import { BilingualSubtitleManager } from "./BilingualSubtitleManager.js";
|
import { BilingualSubtitleManager } from "./BilingualSubtitleManager.js";
|
||||||
import {
|
import {
|
||||||
MSG_XHR_DATA_YOUTUBE,
|
MSG_XHR_DATA_YOUTUBE,
|
||||||
@@ -171,8 +171,7 @@ class YouTubeCaptionProvider {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const toggleButton = document.createElement("button");
|
const toggleButton = document.createElement("button");
|
||||||
toggleButton.className =
|
toggleButton.className = "ytp-button kiss-subtitle-button";
|
||||||
"ytp-button kiss-subtitle-button";
|
|
||||||
toggleButton.title = APP_NAME;
|
toggleButton.title = APP_NAME;
|
||||||
Object.assign(toggleButton.style, {
|
Object.assign(toggleButton.style, {
|
||||||
color: "white",
|
color: "white",
|
||||||
@@ -509,7 +508,6 @@ class YouTubeCaptionProvider {
|
|||||||
this.#managerInstance = new BilingualSubtitleManager({
|
this.#managerInstance = new BilingualSubtitleManager({
|
||||||
videoEl,
|
videoEl,
|
||||||
formattedSubtitles: this.#subtitles,
|
formattedSubtitles: this.#subtitles,
|
||||||
translationService: apiTranslate,
|
|
||||||
setting: { ...this.#setting, fromLang: this.#fromLang },
|
setting: { ...this.#setting, fromLang: this.#fromLang },
|
||||||
});
|
});
|
||||||
this.#managerInstance.start();
|
this.#managerInstance.start();
|
||||||
|
|||||||
Reference in New Issue
Block a user