From cf7af598a3812da34720ec9972b6e0178b5f8b24 Mon Sep 17 00:00:00 2001 From: gdjdkid <67935269+gdjdkid@users.noreply.github.com> Date: Sat, 21 Mar 2026 15:57:36 +0800 Subject: [PATCH] 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. --- lib/fetcher.js | 26 ++++++++------------------ package.json | 2 +- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/fetcher.js b/lib/fetcher.js index 35e5025..69557e4 100644 --- a/lib/fetcher.js +++ b/lib/fetcher.js @@ -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) { diff --git a/package.json b/package.json index b8cf20f..68cb5b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "javinfo", - "version": "1.1.8", + "version": "1.1.9", "description": "AV番号命令行查询工具", "main": "./index.js", "type": "module",