Skip to content

Commit 5d10671

Browse files
author
Taois
committed
fix: 修复header工具对于某些内容识别错误
1 parent bb04cec commit 5d10671

File tree

10 files changed

+281
-56
lines changed

10 files changed

+281
-56
lines changed

drpy-node-bundle/libs/localDsCore.bundled.js

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117106,7 +117106,7 @@ const Xun$1 = new XunDriver();
117106117106
/**
117107117107
* 网盘服务模块集合
117108117108
* 统一导入和导出各种网盘服务提供商的实现
117109-
*
117109+
*
117110117110
* 支持的网盘服务:
117111117111
* - Ali: 阿里云盘服务
117112117112
* - Baidu: 百度网盘服务
@@ -117116,7 +117116,7 @@ const Xun$1 = new XunDriver();
117116117116
* - Quark: 夸克网盘服务
117117117117
* - UC: UC网盘服务
117118117118
* - Yun: 115网盘服务
117119-
*
117119+
*
117120117120
* @example
117121117121
* import pans from './pans.js';
117122117122
* const aliPan = new pans.Ali(config);
@@ -383621,6 +383621,52 @@ var FileHeaderManager = class {
383621383621
}
383622383622
};
383623383623
/**
383624+
* Find the @header(...) block in the comment text
383625+
* @param {string} text Comment text
383626+
* @param {string} ext File extension (.js or .py)
383627+
* @returns {Object|null} { start, end, content }
383628+
*/
383629+
static findHeaderBlock(text, ext) {
383630+
const startIndex = text.indexOf("@header(");
383631+
if (startIndex === -1) return null;
383632+
let index = startIndex + 8;
383633+
let balance = 1;
383634+
let inString = false;
383635+
let stringChar = "";
383636+
let escape = false;
383637+
let inLineComment = false;
383638+
for (; index < text.length; index++) {
383639+
const char = text[index];
383640+
if (inLineComment) {
383641+
if (char === "\n") inLineComment = false;
383642+
continue;
383643+
}
383644+
if (inString) {
383645+
if (escape) escape = false;
383646+
else if (char === "\\") escape = true;
383647+
else if (char === stringChar) inString = false;
383648+
continue;
383649+
}
383650+
if (char === "/" && text[index + 1] === "/") {
383651+
inLineComment = true;
383652+
index++;
383653+
} else if (ext === ".py" && char === "#") inLineComment = true;
383654+
else if (char === "\"" || char === "'") {
383655+
inString = true;
383656+
stringChar = char;
383657+
} else if (char === "(") balance++;
383658+
else if (char === ")") {
383659+
balance--;
383660+
if (balance === 0) return {
383661+
start: startIndex,
383662+
end: index + 1,
383663+
content: text.substring(startIndex + 8, index)
383664+
};
383665+
}
383666+
}
383667+
return null;
383668+
}
383669+
/**
383624383670
* 解析JavaScript对象字面量(支持无引号属性名)
383625383671
* @param {string} str 对象字符串
383626383672
* @returns {Object} 解析后的对象
@@ -383649,10 +383695,10 @@ var FileHeaderManager = class {
383649383695
if (!config) throw new Error(`Unsupported file type: ${ext}`);
383650383696
const match = content.match(config.regex);
383651383697
if (!match) return null;
383652-
const headerMatch = match[0].match(config.headerRegex);
383653-
if (!headerMatch) return null;
383698+
const headerBlock = this.findHeaderBlock(match[0], ext);
383699+
if (!headerBlock) return null;
383654383700
try {
383655-
return this.parseObjectLiteral(headerMatch[1].trim());
383701+
return this.parseObjectLiteral(headerBlock.content.trim());
383656383702
} catch {
383657383703
return null;
383658383704
}
@@ -383715,12 +383761,15 @@ var FileHeaderManager = class {
383715383761
const commentStartIndex = content.indexOf(fullComment);
383716383762
const commentEndIndex = commentStartIndex + fullComment.length;
383717383763
if (content.substring(0, commentStartIndex).trim() !== "") newContent = config.createComment(headerStr) + "\n\n" + content;
383718-
else if (config.headerRegex.test(fullComment)) {
383719-
const updatedComment = fullComment.replace(config.headerRegex, headerStr);
383720-
newContent = content.substring(0, commentStartIndex) + updatedComment + content.substring(commentEndIndex);
383721-
} else {
383722-
const updatedComment = fullComment.replace(config.end, "").trim() + `\n${headerStr}\n${config.end}`;
383723-
newContent = content.substring(0, commentStartIndex) + updatedComment + content.substring(commentEndIndex);
383764+
else {
383765+
const headerBlock = this.findHeaderBlock(fullComment, ext);
383766+
if (headerBlock) {
383767+
const updatedComment = fullComment.substring(0, headerBlock.start) + headerStr + fullComment.substring(headerBlock.end);
383768+
newContent = content.substring(0, commentStartIndex) + updatedComment + content.substring(commentEndIndex);
383769+
} else {
383770+
const updatedComment = fullComment.replace(config.end, "").trim() + `\n${headerStr}\n${config.end}`;
383771+
newContent = content.substring(0, commentStartIndex) + updatedComment + content.substring(commentEndIndex);
383772+
}
383724383773
}
383725383774
} else newContent = config.createComment(headerStr) + "\n\n" + content;
383726383775
if (!newContent.replace(config.regex, "").trim()) throw new Error("写入失败:内容不能只包含文件头而无原始内容");
@@ -383788,13 +383837,13 @@ var FileHeaderManager = class {
383788383837
}
383789383838
const match = content.match(config.regex);
383790383839
if (!match) return content.trim();
383791-
let [fullComment, innerContent] = match;
383792-
if (config.headerRegex.test(innerContent)) {
383793-
innerContent = innerContent.replace(config.headerRegex, "");
383794-
const cleanedInner = innerContent.split("\n").filter((line) => line.trim().length > 0).join("\n");
383840+
let [fullComment] = match;
383841+
const headerBlock = this.findHeaderBlock(fullComment, ext);
383842+
if (headerBlock) {
383843+
let cleanedInner = (fullComment.substring(0, headerBlock.start) + fullComment.substring(headerBlock.end)).replace(config.start, "").replace(config.end, "").split("\n").filter((line) => line.trim().length > 0).join("\n");
383795383844
if (!cleanedInner.trim()) content = content.replace(fullComment, "");
383796383845
else {
383797-
const newComment = `${config.start}${cleanedInner}${config.end}`;
383846+
const newComment = `${config.start}\n${cleanedInner}\n${config.end}`;
383798383847
content = content.replace(fullComment, newComment);
383799383848
}
383800383849
}

drpy-node-bundle/spider/js/30wMV[听].js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var rule = {
9090
lazy: async function (flag, id, flags) {
9191
let {requestHost} = this;
9292
const parseObj = executeParse('_30wmv', requestHost, id);
93-
console.log('parseObj:', parseObj);
93+
// console.log('parseObj:', parseObj);
9494
if (parseObj[0]) {
9595
const _data = await req(parseObj[1]);
9696
const data = JSON.parse(_data.content);

drpy-node-bundle/spider/js/央视大全[官].js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@
44
filterable: 1,
55
quickSearch: 0,
66
title: '央视大全',
7-
lang: 'ds'
7+
lang: 'ds',
8+
isProxyPath: true,
9+
more: {
10+
parseApi: [
11+
{
12+
host: 'cctv://(.+)',
13+
flag: 'CCTV'
14+
},
15+
{
16+
host: 'cctv4k://(.+)',
17+
flag: 'CCTV4K'
18+
},
19+
{
20+
host: 'cctvlive://(.+)',
21+
flag: 'CCTV直播'
22+
},
23+
]
24+
},
25+
826
})
927
*/
1028

@@ -34,7 +52,7 @@ var rule = {
3452
play_parse: true,
3553
lazy: async function () {
3654
let {input, flag, getProxyUrl} = this;
37-
// log(input);
55+
log(input);
3856
// log(flag);
3957
let guid = '';
4058
let url = '';

drpy-node-bundle/spider/js/爱推图[画].js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ var rule = {
2626
searchable: 2,
2727
quickSearch: 0,
2828
filterable: 0,
29-
预处理: async function () {
30-
// console.log(encodeStr('你好'));
31-
// console.log(typeof JSEncrypt);
32-
// console.log(typeof JSON5);
33-
// console.log(typeof gbkTool);
34-
// console.log(typeof NODERSA);
35-
// console.log(typeof atob);
36-
// console.log(typeof btoa);
37-
},
3829
headers: {
3930
'User-Agent': 'MOBILE_UA',
4031
},
@@ -64,10 +55,6 @@ var rule = {
6455
推荐: '*',
6556
searchUrl: '/sou-**-fypage.html',
6657
一级: '#content&&article;h2&&Text;img&&data-src;;a&&href',
67-
// 一级: async function () {
68-
// let {getProxyUrl} = this;
69-
// console.log('本地代理地址:' + getProxyUrl());
70-
// },
7158
二级: '*',
7259
搜索: '*',
7360
}

drpy-node-bundle/spider/js/设置中心.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
filterable: 0,
55
quickSearch: 0,
66
title: '设置中心',
7+
isProxyPath: true,
78
logo: 'https://avatars.githubusercontent.com/u/49803097?v=4',
89
more: {
910
sourceTag: '设置,动作',
@@ -73,6 +74,7 @@ let quick_data = {
7374
直链1: 'https://vdse.bdstatic.com//628ca08719cef5987ea2ae3c6f0d2386.mp4',
7475
嗅探1: 'https://www.6080kk.cc/haokanplay/178120-1-1.html',
7576
嗅探2: 'https://www.hahads.com/play/537106-3-1.html',
77+
央视跨源:'cctv://3c18e2b3bba249b78b831e910608cfec',
7678
多集: 'https://v.qq.com/x/cover/m441e3rjq9kwpsc/m00253deqqo.html@https://pan.quark.cn/s/6c8158e258f3@https://pan.baidu.com/s/1TdbgcwaMG1dK7B5pQ1LbBg?pwd=1234',
7779
海阔二级单线路: gzip(JSON.stringify({
7880
"actor": "剧集",

package-bundle.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,24 @@ const INCLUDE_ITEMS = [
1212
'localDsCoreTest.js'
1313
];
1414

15+
// 需要额外复制的特定文件列表 (文件名,相对于 spider/js)
16+
const EXTRA_FILES = [
17+
'30wMV[听].js',
18+
'爱推图[画].js',
19+
'央视大全[官].js',
20+
'设置中心.js'
21+
];
22+
1523
// 获取脚本所在目录 (e:\gitwork\drpy-node)
1624
const getScriptDir = () => dirname(resolve(url.fileURLToPath(import.meta.url)));
1725

18-
// 复制 _lib 开头的文件到 bundle 目录
19-
const copyLibFiles = (scriptDir) => {
26+
// 复制 _lib 开头的文件及额外指定的文件到 bundle 目录
27+
const copyFiles = (scriptDir) => {
2028
const sourceDir = join(scriptDir, 'spider', 'js');
2129
const targetDir = join(scriptDir, 'drpy-node-bundle', 'spider', 'js');
2230

2331
if (!existsSync(sourceDir)) {
24-
console.warn(`警告: 源目录 ${sourceDir} 不存在,跳过复制 lib 文件。`);
32+
console.warn(`警告: 源目录 ${sourceDir} 不存在,跳过复制文件。`);
2533
return;
2634
}
2735

@@ -30,23 +38,37 @@ const copyLibFiles = (scriptDir) => {
3038
mkdirSync(targetDir, {recursive: true});
3139
}
3240

33-
console.log(`正在从 ${sourceDir} 复制 lib 文件到 ${targetDir}...`);
41+
console.log(`正在从 ${sourceDir} 复制文件到 ${targetDir}...`);
3442

3543
try {
3644
const files = readdirSync(sourceDir);
37-
let count = 0;
45+
let libCount = 0;
46+
let extraCount = 0;
47+
3848
for (const file of files) {
49+
let shouldCopy = false;
50+
51+
// 检查是否是 _lib 文件
3952
if (file.startsWith('_lib') && (file.endsWith('.js') || file.endsWith('.cjs'))) {
53+
shouldCopy = true;
54+
libCount++;
55+
}
56+
// 检查是否在额外文件列表中
57+
else if (EXTRA_FILES.includes(file)) {
58+
shouldCopy = true;
59+
extraCount++;
60+
}
61+
62+
if (shouldCopy) {
4063
const srcPath = join(sourceDir, file);
4164
const destPath = join(targetDir, file);
4265
copyFileSync(srcPath, destPath);
4366
// console.log(`已复制: ${file}`);
44-
count++;
4567
}
4668
}
47-
console.log(`成功复制了 ${count} 个 lib 文件。`);
69+
console.log(`成功复制了 ${libCount} 个 lib 文件和 ${extraCount} 个额外文件。`);
4870
} catch (error) {
49-
console.error(`复制 lib 文件失败: ${error.message}`);
71+
console.error(`复制文件失败: ${error.message}`);
5072
}
5173
};
5274

@@ -114,7 +136,7 @@ const compressBundle = (scriptDir) => {
114136
// 主程序入口
115137
const main = () => {
116138
const scriptDir = getScriptDir();
117-
copyLibFiles(scriptDir);
139+
copyFiles(scriptDir);
118140
compressBundle(scriptDir);
119141
};
120142

scripts/test/test_fileHeaderManager.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,61 @@ function test() {
8787
await FileHeaderManager.writeHeader(jsWithCommentFile, headerObj);
8888
console.log('\n添加文件头后:');
8989
console.log(await fs.readFile(jsWithCommentFile, 'utf8'));
90+
91+
// 测试3.5: 复杂嵌套头信息
92+
console.log('\n\n测试3.5: 复杂嵌套头信息');
93+
const complexFile = path.join(testDir, 'complex.js');
94+
const complexContent = `/*
95+
@header({
96+
searchable: 1,
97+
filterable: 1,
98+
quickSearch: 0,
99+
title: '央视大全',
100+
lang: 'ds',
101+
isProxyPath: true,
102+
more: {
103+
parseApi: [
104+
{
105+
host: 'cctv://(.+)',
106+
flag: 'CCTV'
107+
},
108+
{
109+
host: 'cctv4k://(.+)',
110+
flag: 'CCTV4K'
111+
},
112+
{
113+
host: 'cctvlive://(.+)',
114+
flag: 'CCTV直播'
115+
},
116+
]
117+
},
118+
119+
})
120+
*/
121+
var rule = {};`;
122+
await createTestFile(complexFile, complexContent);
123+
124+
// 读取并验证
125+
const complexHeader = await FileHeaderManager.readHeader(complexFile);
126+
console.log('读取复杂头信息:', complexHeader.title);
127+
if (complexHeader.more && complexHeader.more.parseApi && complexHeader.more.parseApi.length === 3) {
128+
console.log('✓ 复杂结构解析正确');
129+
} else {
130+
console.error('✗ 复杂结构解析失败');
131+
}
132+
133+
// 写入并验证不损坏
134+
await FileHeaderManager.writeHeader(complexFile, complexHeader);
135+
const newComplexContent = await fs.readFile(complexFile, 'utf8');
136+
// Check for cleanliness (no trailing garbage like in the bug)
137+
// The bug caused "})'," or similar garbage at the end of the comment block
138+
// The correct content should end the comment cleanly.
139+
if (newComplexContent.includes("host: 'cctv://(.+)'") && !newComplexContent.includes("})',")) {
140+
console.log('✓ 写入复杂头信息正确 (无乱码残留)');
141+
} else {
142+
console.error('✗ 写入复杂头信息失败 (可能有残留)');
143+
console.log(newComplexContent);
144+
}
90145

91146
// 测试4: 测试内容完整性检查
92147
console.log('\n\n测试4: 内容完整性检查');

spider/js/央视大全[官].js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@
44
filterable: 1,
55
quickSearch: 0,
66
title: '央视大全',
7-
lang: 'ds'
7+
lang: 'ds',
8+
isProxyPath: true,
9+
more: {
10+
parseApi: [
11+
{
12+
host: 'cctv://(.+)',
13+
flag: 'CCTV'
14+
},
15+
{
16+
host: 'cctv4k://(.+)',
17+
flag: 'CCTV4K'
18+
},
19+
{
20+
host: 'cctvlive://(.+)',
21+
flag: 'CCTV直播'
22+
},
23+
]
24+
},
25+
826
})
927
*/
1028

@@ -34,7 +52,7 @@ var rule = {
3452
play_parse: true,
3553
lazy: async function () {
3654
let {input, flag, getProxyUrl} = this;
37-
// log(input);
55+
log(input);
3856
// log(flag);
3957
let guid = '';
4058
let url = '';

0 commit comments

Comments
 (0)