Fix: By using env to pass cookies as environment variables to curl, the shell's handling of special characters is bypassed, and it works correctly on both Windows and Linux.

This commit is contained in:
gdjdkid committed 2026-03-21 15:57:36 +08:00
1 parent fd90cc2fed
commit cf7af598a3
2 files changed
+9 -19

No files matched your search

+8 -18
View File
@@ -1,9 +1,7 @@
import { execSync } from 'child_process';
import * as cheerio from 'cheerio';
import { getCache, setCache, getConfig } from './cache.js';
import { writeFileSync, unlinkSync } from 'fs';
import chalk from 'chalk';
import { tmpdir } from 'os';
// ─── 通用请求函数 ────────────────────────────────────────
function fetchHtml(url, cookie = '') {
@@ -157,17 +155,11 @@ async function searchJavdb(id) {
if (!config.session) return null;
const session = decodeURIComponent(config.session);
const cookie = `locale=zh; over18=1; _jdb_session=${session}`;
// 写成 Netscape 格式,curl 能正确解析
// 通过 env 环境变量传递 cookie,彻底避免 shell 特殊字符问题
// 兼容 Windows 和 Linux
const tmpFile = `${tmpdir()}/javinfo_cookie.txt`;
const netscapeCookie = [
'# Netscape HTTP Cookie File',
`javdb.com\tFALSE\t/\tTRUE\t0\t_jdb_session\t${session}`,
`javdb.com\tFALSE\t/\tFALSE\t0\tlocale\tzh`,
`javdb.com\tFALSE\t/\tFALSE\t0\tover18\t1`,
].join('\n');
writeFileSync(tmpFile, netscapeCookie, 'utf-8');
const curlEnv = { ...process.env, JAVDB_COOKIE: cookie };
const searchUrl = `https://javdb.com/search?q=${encodeURIComponent(id)}&f=all`;
const searchHtml = execSync(
@@ -175,8 +167,8 @@ async function searchJavdb(id) {
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
-H "Accept-Language: zh-CN,zh;q=0.9" \
-b "${tmpFile}"`,
{ timeout: 15000, maxBuffer: 1024 * 1024 * 10 }
-H "Cookie: $JAVDB_COOKIE"`,
{ timeout: 15000, maxBuffer: 1024 * 1024 * 10, env: curlEnv }
).toString();
const $ = cheerio.load(searchHtml);
@@ -203,15 +195,13 @@ async function searchJavdb(id) {
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" \
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
-H "Accept-Language: zh-CN,zh;q=0.9" \
-b "${tmpFile}"`,
{ timeout: 15000, maxBuffer: 1024 * 1024 * 10 }
-H "Cookie: $JAVDB_COOKIE"`,
{ timeout: 15000, maxBuffer: 1024 * 1024 * 10, env: curlEnv }
).toString();
if (detailHtml.length > 1000) break; // 正常页面远大于 1000 字节
if (detailHtml.length > 1000) break;
}
try { unlinkSync(tmpFile); } catch {}
if (detailHtml.length < 1000) return null;
return parseJavdb(detailHtml, id);
} catch (e) {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "javinfo",
"version": "1.1.8",
"version": "1.1.9",
"description": "AV番号命令行查询工具",
"main": "./index.js",
"type": "module",