脚本
wanghongenpin edited this page 2025-12-04 16:36:15 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

ProxyPin提供了一个脚本功能开发人员可以编写JS代码以灵活的方式操作请求/响应

API说明

function onRequest(context, request)

在请求到达服务器之前,调用此函数,您可以在此处修改请求数据
具体参数格式见 > 参数定义

async function onRequest(context, request) {
  console.log(request.url);
  //URL参数
  request.queries["name"] = "value";
  //更新或添加新标头
  request.headers["X-New-Headers"] = "My-Value";
  delete request.headers["Key-Need-Delete"];
  
  //Update Body 使用fetch API请求接口具体文档可网上搜索fetch API
  //request.body = await fetch('https://www.baidu.com/').then(response => response.text());
  
  //共享参数 后面onResponse时取出
  context["name"] = "hello";
  return request;
}

请求中断
如果需要中断此请求onRequest函数结果返回null即可

function onResponse(context, request, response)

在将响应数据发送到客户端之前,调用此函数,您可以在此处修改响应数据

async function onResponse(context, request, response) {
  // 更新或添加新标头 
  // response.headers["Name"] = context["name"];
    
  // Update status Code
  response.statusCode = 500;

  //var body = JSON.parse(response.body);
  //body['key'] = "value";
  //response.body = JSON.stringify(body);
  return response;
}

参数定义

//context 
{
    "os": "macos",
    "scriptName": "Your Script Name",
    "deviceId": "设备id", 
    "session": {} //运行时会话对象,不同请求可传递参数
}

//request
{
    "method": "<String> HTTP Method. 示例: GET, POST, ...",
    "host": "<String> *只读* 域名. Ex: www.baidu.com, localhost, ...",
    "path": "<String>: URL Path. Ex: /v1/api",
    "queries": "<Map<String, String>>  JS字典对象URL参数",
    "headers": "<Map<String, String>> JS字典对象所有Header键值",
    "body": "<String> 请求体字符串类型 json格式转换对象需要调用JSON.parse(request.body)",
    "rawBody": [] //原始字节数组 Uint8Array
}

//response
{
    "statusCode": 200,
    "headers": "<Map<String, String>> JS字典对象包含所有Header键值",
    "body": "<String> 请求体字符串类型 json格式转换对象需要调用JSON.parse(request.body)",
}

JS内置方法

MD5
var hash = md5('value') //output 2063c1608d6e0baf80249c42e2be5804
File 文件操作

API和dart一致见dart File文档 https://api.dart.ac.cn/stable/3.4.4/dart-io/File-class.html

//File(path):创建一个表示文件的对象,参数 path 是文件的路径。
var file = File('file.path');

//异步读取文件为字符串
var text = await file.readAsString();
//异步将字符串内容写入文件。
await file.writeAsString('text');

  • File类的构造函数
    File(path):创建一个表示文件的对象,参数 path 是文件的路径。

所有方法都有同步版本推荐都使用异步版本同步方法多了个Sync后缀 比如readAsString(), 同步方法就是readAsStringSync()

  • 读取文件内容的方法:
    readAsString():异步读取文件内容并返回一个字符串。通常结合 async/await 使用。
    readAsStringSync():同步读取文件内容并返回一个字符串 (不推荐使用)
    readAsBytes():异步读取文件内容并返回一个字节列表。
    readAsBytesSync(): 对应同步方法

  • 写入文件内容的方法:
    writeAsString(content, append):异步将字符串内容写入文件。 append true表示追加.
    writeAsBytes(bytes):异步将字节列表内容写入文件。

  • 文件属性相关方法:
    exists():异步检查文件是否存在,返回一个布尔值。
    length():异步获取文件的大小,以字节为单位,返回一个整数。

  • 文件操作方法:
    rename(newPath):重命名文件为指定路径。

  • 目录操作相关方法(如果 File 对象表示一个目录):
    create(recursive: bool):创建目录,如果 recursive 为 true则可以创建多级目录.

Fetch API使用参考文档

https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch
https://www.ruanyifeng.com/blog/2020/12/fetch-tutorial.html