You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jint修改JS的antiCapthaPredefinedApiKey与defaultConfig代理配置问题

问题核心原因

你当前的代码仅修改了Jint运行时内存中的变量值,没有将修改结果回写至本地JS文件,且defaultConfig中的account_key、account_key_checked是JS初始化阶段就完成赋值的静态值,单独修改antiCapthaPredefinedApiKey不会自动同步这两个关联字段,代理配置也需要主动修改对应属性。

实现代码

你可以参考以下完整实现,需要提前安装Newtonsoft.Json用于序列化(也可替换为项目中已有的JSON序列化组件):

using Newtonsoft.Json;
using Jint;
using System.IO;

public static void ModifyJsConfig(string apiKey, string proxyProtocol, string proxyServer, string proxyPort, string proxyLogin = "", string proxyPwd = "")
{
    string filePath = Path.Combine(Environment.CurrentDirectory, @"anticaptcha\js\config_ac_api_key.js");
    string originalJs = File.ReadAllText(filePath);

    // 加载原始JS到Jint引擎
    var engine = new Engine();
    engine.Execute(originalJs);

    // 1. 修改API密钥及关联配置
    engine.SetValue("antiCapthaPredefinedApiKey", apiKey);
    // 同步更新defaultConfig中的关联字段
    engine.Execute(@"
        defaultConfig.account_key = antiCapthaPredefinedApiKey;
        defaultConfig.account_key_checked = !!antiCapthaPredefinedApiKey;
    ");

    // 2. 修改代理配置
    engine.SetValue("user_proxy_protocol", proxyProtocol);
    engine.SetValue("user_proxy_server", proxyServer);
    engine.SetValue("user_proxy_port", proxyPort);
    engine.SetValue("user_proxy_login", proxyLogin);
    engine.SetValue("user_proxy_password", proxyPwd);
    engine.Execute(@"
        defaultConfig.solve_proxy_on_tasks = true; // 开启代理使用
        defaultConfig.user_proxy_protocol = user_proxy_protocol;
        defaultConfig.user_proxy_server = user_proxy_server;
        defaultConfig.user_proxy_port = user_proxy_port;
        defaultConfig.user_proxy_login = user_proxy_login;
        defaultConfig.user_proxy_password = user_proxy_password;
    ");

    // 3. 读取修改后的变量值,序列化后拼接为新的JS内容
    var modifiedApiKey = engine.GetValue("antiCapthaPredefinedApiKey").AsString();
    var modifiedConfig = engine.GetValue("defaultConfig").ToObject();
    string newJsContent = $@"// Public config
var antiCapthaPredefinedApiKey = {JsonConvert.SerializeObject(modifiedApiKey)};
var defaultConfig = {JsonConvert.SerializeObject(modifiedConfig, Formatting.Indented)};";

    // 4. 覆盖写入原文件
    File.WriteAllText(filePath, newJsContent);
}

调用说明

如果你的代理参数是拼接的字符串,可以自行拆分后传入方法,例如:

// 示例调用
ModifyJsConfig("你的API密钥", "HTTP", "127.0.0.1", "8080", "代理账号", "代理密码");

如果不需要代理验证,留空最后两个参数即可。

内容的提问来源于stack exchange,提问作者Tanner Ensign

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 02:54:03