From 750480889ca2e920b339e0aa06a7547e92c27670 Mon Sep 17 00:00:00 2001 From: xiaoxuan010 <2592053474@qq.com> Date: Thu, 26 Dec 2024 16:25:06 +0800 Subject: [PATCH] Optimize danmaku matching for segment's skipping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化逻辑,匹配更多时间格式的弹幕 --- public/_locales/en/messages.json | 12 ------- public/_locales/zh_CN/messages.json | 12 ------- public/_locales/zh_TW/messages.json | 12 ------- public/options/options.html | 11 ------- src/config.ts | 18 ++++++++--- src/utils/danmakusUtils.ts | 50 ++++++++++++++++++----------- 6 files changed, 45 insertions(+), 70 deletions(-) diff --git a/public/_locales/en/messages.json b/public/_locales/en/messages.json index 031f2432..15df0709 100644 --- a/public/_locales/en/messages.json +++ b/public/_locales/en/messages.json @@ -786,18 +786,6 @@ "checkTimeDanmakuSkip": { "message": "检查片段附近是否已有上传的跳过信息,已有则不触发此功能" }, - "danmakuTimeMatchingRegexPattern": { - "message": "空降时间匹配正则: " - }, - "danmakuRegexPatternPlaceholder": { - "message": "能匹配2~3组数字的正则表达式" - }, - "danmakuRegexTitle": { - "message": "请输入一个匹配时间格式的正则表达式,要求能捕获2~3组数字。如果不清楚如何写正则表达式,请勿修改此项" - }, - "danmakuTimeMatchingRegexPatternDescription": { - "message": "空降时间匹配类似“01:23”“四分五十六秒”“1分2秒”等格式的弹幕。" - }, "danmakuOffsetMatchingRegexPattern": { "message": "时间偏移匹配正则: " }, diff --git a/public/_locales/zh_CN/messages.json b/public/_locales/zh_CN/messages.json index 6e0acc95..e3edbfef 100644 --- a/public/_locales/zh_CN/messages.json +++ b/public/_locales/zh_CN/messages.json @@ -786,18 +786,6 @@ "checkTimeDanmakuSkip": { "message": "检查片段附近是否已有上传的跳过信息,已有则不触发此功能" }, - "danmakuTimeMatchingRegexPattern": { - "message": "空降时间匹配正则: " - }, - "danmakuRegexPatternPlaceholder": { - "message": "能匹配2~3组数字的正则表达式" - }, - "danmakuRegexTitle": { - "message": "请输入一个匹配时间格式的正则表达式,要求能捕获2~3组数字。如果不清楚如何写正则表达式,请勿修改此项" - }, - "danmakuTimeMatchingRegexPatternDescription": { - "message": "空降时间匹配类似“01:23”“四分五十六秒”“1分2秒”等格式的弹幕。" - }, "danmakuOffsetMatchingRegexPattern": { "message": "时间偏移匹配正则: " }, diff --git a/public/_locales/zh_TW/messages.json b/public/_locales/zh_TW/messages.json index 1beb0da3..f5b1cd55 100644 --- a/public/_locales/zh_TW/messages.json +++ b/public/_locales/zh_TW/messages.json @@ -786,18 +786,6 @@ "checkTimeDanmakuSkip": { "message": "檢查片段附近是否已有上傳的跳過資訊,已有則不觸發此功能" }, - "danmakuTimeMatchingRegexPattern": { - "message": "空降時間匹配正則: " - }, - "danmakuRegexPatternPlaceholder": { - "message": "能匹配2~3組數字的正則表達式" - }, - "danmakuRegexTitle": { - "message": "請輸入一個匹配時間格式的正則表達式,要求能捕獲2~3組數字。如果不清楚如何寫正則表達式,請勿修改此項" - }, - "danmakuTimeMatchingRegexPatternDescription": { - "message": "空降時間匹配類似“01:23”“四分五十六秒”“1分2秒”等格式的彈幕。" - }, "danmakuOffsetMatchingRegexPattern": { "message": "時間偏移匹配正則: " }, diff --git a/public/options/options.html b/public/options/options.html index f96aca4d..f98c6cb5 100644 --- a/public/options/options.html +++ b/public/options/options.html @@ -776,17 +776,6 @@

__MSG_exportOtherData__

-
-
- - - - -
-
__MSG_danmakuTimeMatchingRegexPatternDescription__
-
-
diff --git a/src/config.ts b/src/config.ts index 615ca8b9..7c03ef51 100644 --- a/src/config.ts +++ b/src/config.ts @@ -33,7 +33,6 @@ interface SBConfig { enableDanmakuSkip: boolean; enableAutoSkipDanmakuSkip: boolean; enableMenuDanmakuSkip: boolean; - danmakuTimeMatchingRegexPattern: string; danmakuOffsetMatchingRegexPattern: string; checkTimeDanmakuSkip: boolean; muteSegments: boolean; @@ -192,9 +191,20 @@ function migrateOldSyncFormats(config: SBConfig) { config["serverAddress"] = CompileConfig.serverAddress; } - // "danmakuRegexPattern" 在 0.5.9 版本中被移除, + // "danmakuRegexPattern" 在 0.6.0 版本中被移除, // 取而代之的是 "danmakuTimeMatchingRegexPattern" 和 "danmakuOffsetMatchingRegexPattern" delete config["danmakuRegexPattern"]; + + //"danmakuTimeMatchingRegexPattern" 在 0.6.1 版本中被移除(预计) + delete config["danmakuTimeMatchingRegexPattern"]; + + // 更新默认的弹幕偏移匹配正则表达式 + const oldDanmakuOffsetMatchingRegexPatterns = [ + "(?:^|(右|右滑|按|右下|右向|右方向|→|⇒|⇢|⇨|⮕|🡆|🠺|🠾|🢒|👉))(\\d+)(下|次)?$", // 0.6.0 + ]; + if (oldDanmakuOffsetMatchingRegexPatterns.includes(config["danmakuOffsetMatchingRegexPattern"])) { + config["danmakuOffsetMatchingRegexPattern"] = syncDefaults.danmakuOffsetMatchingRegexPattern; + } } const syncDefaults = { @@ -216,9 +226,7 @@ const syncDefaults = { enableDanmakuSkip: false, enableAutoSkipDanmakuSkip: false, enableMenuDanmakuSkip: false, - danmakuTimeMatchingRegexPattern: - "(?:(\\d{1,2})\\s*(?:小时|h|H|:|:|;|;|\\.|-|—)\\s*)?(?:(\\d{1,2})\\s*(?:分钟|分|:|:|;|;|\\.|-|—|m|M)\\s*)?(?:(\\d{1,2})\\s*(秒|s|S)?)", - danmakuOffsetMatchingRegexPattern: "(?:^|(右|右滑|按|右下|右向|右方向|→|⇒|⇢|⇨|⮕|🡆|🠺|🠾|🢒|👉))(\\d+)(下|次)?$", + danmakuOffsetMatchingRegexPattern: "(?:^|(右|右滑|按|右下|右向|右方向|→|⇒|⇢|⇨|⮕|🡆|🠺|🠾|🢒|👉))(\\d+)(下|次)?", checkTimeDanmakuSkip: true, muteSegments: true, diff --git a/src/utils/danmakusUtils.ts b/src/utils/danmakusUtils.ts index 62adea0e..86b91a06 100644 --- a/src/utils/danmakusUtils.ts +++ b/src/utils/danmakusUtils.ts @@ -14,22 +14,33 @@ export function parseTargetTimeFromDanmaku(text: string, currentTime: number) { * @param text - 包含需要解析的时间的输入字符串。 * @returns 由时间字符串表示的总秒数,如果时间字符串无效则返回null。 * - * 该函数使用在 `Config.config.danmakuTimeMatchingRegexPattern` 中定义的正则表达式模式 - * 来匹配和提取输入字符串中的小时、分钟和秒。如果匹配成功且有效, - * 它通过将小时转换为秒、分钟转换为秒并将它们加到解析的秒数中来计算总秒数。 + * 该函数使用正则表达式来匹配和提取输入字符串中的小时、分钟和秒。 + * 如果匹配成功且有效,则返回总秒数;否则返回null。 */ function parseTime(text: string) { - const regex = new RegExp(Config.config.danmakuTimeMatchingRegexPattern, "g"); + const danmakuTimeMatchingRegex = + /(?:(\d{1,2})\s*(?:(小时|h|H)|(:|:|;|;|\.|-|—))\s*)?(?:(\d{1,2})\s*(分钟|分|:|:|;|;|\.|-|—|m|M)\s*)?(?:(\d{1,2}|整)\s*(秒|s|S)?)/g; let match: RegExpExecArray | null; - while ((match = regex.exec(text)) !== null) { - const [, , minutes, seconds, secondsSuffix] = match; + while ((match = danmakuTimeMatchingRegex.exec(text)) !== null) { + const [, hours, hourSpecificSuffix, , minutes, minuteSuffix, seconds, secondSuffix] = match; + + if (seconds !== undefined && (secondSuffix !== undefined || minutes !== undefined || hours !== undefined)) { + let hoursNum = hours ? parseInt(hours) : 0; + let minutesNum = minutes ? parseInt(minutes) : 0; + const secondsNum = seconds === "整" ? 0 : parseInt(seconds); - if (seconds && (secondsSuffix || minutes)) { - const hours = parseInt(match[1] || "0"); - const minutes = parseInt(match[2] || "0"); - const seconds = parseInt(match[3] || "0"); - return hours * 3600 + minutes * 60 + seconds; + if ( + hours !== undefined && + hourSpecificSuffix === undefined && + minutes === undefined && + minuteSuffix === undefined + ) { + minutesNum = hoursNum; + hoursNum = 0; + } + + return hoursNum * 3600 + minutesNum * 60 + secondsNum; } } @@ -65,10 +76,13 @@ export function parseTargetTimeFromDanmaku(text: string, currentTime: number) { text = text.replace(/[零一二三四五六七八九两壹贰叁肆伍陆柒捌玖十百千万]+/g, (cnNum) => parseChineseNumber(cnNum)); const directParsedTime = parseTime(text); - if (directParsedTime) return directParsedTime; - else { + if (directParsedTime) { + return directParsedTime; + } else { const offsetParsedTime = parseOffsetTime(text); - if (offsetParsedTime) return offsetParsedTime + currentTime; + if (offsetParsedTime) { + return offsetParsedTime + currentTime; + } } return null; } @@ -77,7 +91,7 @@ export function parseTargetTimeFromDanmaku(text: string, currentTime: number) { * 将中文数字字符串转换为阿拉伯数字字符串。 * * @param inputText - 包含中文数字的字符串。 - * @returns 转换后的阿拉伯数字字符串。如果输入包含无效字符,则返回 null。 + * @returns 转换后的阿拉伯数字字符串。如果输入包含无效字符,则返回空字符串。 * * @example * ```typescript @@ -85,10 +99,10 @@ export function parseTargetTimeFromDanmaku(text: string, currentTime: number) { * parseChineseNumber("十二"); // 返回 "12" * parseChineseNumber("二十"); // 返回 "20" * parseChineseNumber("二十一"); // 返回 "21" - * parseChineseNumber("一百"); // 返回 null + * parseChineseNumber("一百"); // 返回 "" * ``` */ -export function parseChineseNumber(inputText: string) { +export function parseChineseNumber(inputText: string): string { const cnChrMap: { [key: string]: number } = { 零: 0, 一: 1, @@ -130,7 +144,7 @@ export function parseChineseNumber(inputText: string) { num = num * unit; unit = 1; } else { - return null; + return ""; } }