Skip to content

Commit 1acf881

Browse files
author
Taois
committed
fix: 适配新功能
增加源和函数
1 parent 4c69ea6 commit 1acf881

File tree

5 files changed

+823
-11
lines changed

5 files changed

+823
-11
lines changed

docs/updateRecord.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# drpyS更新记录
22

3+
### 20250928
4+
5+
更新至V1.3.7
6+
7+
1. 增加一个cat源 `爱玩音乐.js`,补齐缺失的 `lrcToSrt` `strExtract` 并注入到drpys沙箱环境和cat全局变量
8+
39
### 20250927
410

511
更新至V1.3.6

libs/drpyS.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
parseQueryString, buildQueryString, encodeIfContainsSpecialChars, objectToQueryString,
3636
getOriginalJs,
3737
pako, gbkTool, JSEncrypt, CryptoJS, NODERSA, JSON5, jinja, atob, btoa, stringify,
38+
lrcToSrt, strExtract,
3839
jsEncoder
3940
} from '../libs_drpy/drpyCustom.js';
4041

@@ -245,7 +246,9 @@ export async function getSandbox(env = {}) {
245246
buildQueryString,
246247
encodeIfContainsSpecialChars,
247248
objectToQueryString,
248-
forge
249+
forge,
250+
lrcToSrt,
251+
strExtract,
249252
};
250253

251254
const libsSanbox = {

libs_drpy/drpyCustom.js

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,102 @@ export async function processImage(vod_pic, moduleObject, injectVars = null) {
954954
return vod_pic;
955955
}
956956

957+
// 格式化时间为SRT格式 HH:MM:SS,mmm
958+
function formatSrtTime(milliseconds) {
959+
const hours = Math.floor(milliseconds / 3600000);
960+
const minutes = Math.floor((milliseconds % 3600000) / 60000);
961+
const seconds = Math.floor((milliseconds % 60000) / 1000);
962+
const ms = milliseconds % 1000;
963+
964+
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')},${ms.toString().padStart(3, '0')}`;
965+
}
966+
967+
// LRC格式歌词转SRT字幕格式
968+
export function lrcToSrt(lrcContent) {
969+
if (!lrcContent || typeof lrcContent !== 'string') {
970+
return '';
971+
}
972+
973+
// 解析LRC歌词行
974+
const lines = lrcContent.split('\n');
975+
const timeLines = [];
976+
977+
for (const line of lines) {
978+
const trimmedLine = line.trim();
979+
if (!trimmedLine) continue;
980+
981+
// 匹配时间标签格式 [mm:ss.xx] 或 [mm:ss]
982+
const timeMatch = trimmedLine.match(/^\[(\d{1,2}):(\d{2})(?:\.(\d{1,3}))?\](.*)$/);
983+
if (timeMatch) {
984+
const minutes = parseInt(timeMatch[1]);
985+
const seconds = parseInt(timeMatch[2]);
986+
const milliseconds = timeMatch[3] ? parseInt(timeMatch[3].padEnd(3, '0')) : 0;
987+
const text = timeMatch[4].trim();
988+
989+
// 计算总毫秒数
990+
const totalMs = minutes * 60 * 1000 + seconds * 1000 + milliseconds;
991+
992+
if (text) { // 只添加有文本内容的行
993+
timeLines.push({
994+
time: totalMs,
995+
text: text
996+
});
997+
}
998+
}
999+
}
1000+
1001+
// 按时间排序
1002+
timeLines.sort((a, b) => a.time - b.time);
1003+
1004+
if (timeLines.length === 0) {
1005+
return '';
1006+
}
1007+
1008+
// 转换为SRT格式
1009+
let srtContent = '';
1010+
for (let i = 0; i < timeLines.length; i++) {
1011+
const currentLine = timeLines[i];
1012+
const nextLine = timeLines[i + 1];
1013+
1014+
// 计算结束时间(下一行的开始时间,或当前时间+3秒)
1015+
const endTime = nextLine ? nextLine.time : currentLine.time + 3000;
1016+
1017+
// 格式化时间为SRT格式 HH:MM:SS,mmm
1018+
const startTimeStr = formatSrtTime(currentLine.time);
1019+
const endTimeStr = formatSrtTime(endTime);
1020+
1021+
// 添加SRT条目
1022+
srtContent += `${i + 1}\n`;
1023+
srtContent += `${startTimeStr} --> ${endTimeStr}\n`;
1024+
srtContent += `${currentLine.text}\n\n`;
1025+
}
1026+
1027+
return srtContent.trim();
1028+
}
1029+
1030+
/**
1031+
* 字符串正则表达式提取函数
1032+
* @param {string} content - 要搜索的源字符串
1033+
* @param {string} pattern - 正则表达式模式
1034+
* @param {number} groupIndex - 捕获组索引,0表示整个匹配,1表示第一个捕获组,以此类推
1035+
* @returns {string} 提取到的字符串,如果没有匹配则返回空字符串
1036+
*/
1037+
export function strExtract(content, pattern, groupIndex = 0) {
1038+
try {
1039+
const regex = new RegExp(pattern);
1040+
const match = content.match(regex);
1041+
1042+
if (match && match[groupIndex] !== undefined) {
1043+
return match[groupIndex];
1044+
}
1045+
1046+
return '';
1047+
} catch (error) {
1048+
console.error('strExtract error:', error);
1049+
return '';
1050+
}
1051+
}
1052+
9571053
export const pako = globalThis.pako;
9581054
export const gbkTool = globalThis.gbkTool;
9591055
export const JSEncrypt = globalThis.JSEncrypt;
@@ -970,4 +1066,6 @@ globalThis.base64Encode = base64Encode;
9701066
globalThis.base64Decode = base64Decode;
9711067
globalThis.gzip = gzip;
9721068
globalThis.ungzip = ungzip;
973-
globalThis.parseQueryString = parseQueryString;
1069+
globalThis.parseQueryString = parseQueryString;
1070+
globalThis.lrcToSrt = lrcToSrt;
1071+
globalThis.strExtract = strExtract;

0 commit comments

Comments
 (0)