5
Script
wanghongenpin edited this page 2025-12-04 16:38:14 +08:00
This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 Provides a scripting feature where developers can write JS code to flexibly manipulate requests/responses

API UseGuide

function onRequest(context, request)

This function is called before the request reaches the server, where you can modify the request data
For specific parameter formats, see > Parameter Definition

async function onRequest(context, request) {
  console.log(request.url);
  //URL Parameter
  request.queries["name"] = "value";
  //Update or add Header
  request.headers["X-New-Headers"] = "My-Value";
  delete request.headers["Key-Need-Delete"];
  
  //Update Body Use the fetch API request interface. For specific documents, you can search for fetch API.
  //request.body = await fetch('https://www.baidu.com/').then(response => response.text());
  
  //Shared parameters are taken out later during onResponse
  context["name"] = "hello";
  return request;
}

REQUEST INTERRUPT If you need to interrupt a request, the result of the onRequest function returns None!

function onResponse(context, request, response)

This function is called before the response data is sent to the client, where you can modify the response data

async function onResponse(context, request, response) {
  //Update or add Header
  //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;
}

Parameter definition

//context 
{
    "os": "macos",
    "scriptName": "Your Script Name",
    "deviceId": "deviceId",
    "session": {} //Runtime session object, parameters can be passed in different requests
}

//request
{
    "method": "<String> HTTP Method. for example: GET, POST, ...",
    "host": "<String> *read-only* domain name. for example: www.baidu.com, localhost, ...",
    "path": "<String>: URL Path. for example: /v1/api",
    "queries": "<Map<String, String>>  JS Dictionary object URL parameters",
    "headers": "<Map<String, String>> JS All Header key values of the dictionary object",
    "body": "<String> Request body string type json format conversion object needs to be called JSON.parse(request.body)",
    "rawBody": [] //Original byte array Uint8Array
}

//response
{
    "statusCode": 200,
    "headers": "<Map<String, String>> JS All Header key values of the dictionary object",
    "body": "<String> Response body string type json format conversion object needs to be called JSON.parse(request.body)",
}

JS built-in methods

MD5
var hash = md5('value') //output 2063c1608d6e0baf80249c42e2be5804
File API

The API is consistent with dart, see dart File documentation https://api.dart.dev/stable/3.5.4/dart-io/File-class.html

//File(path)Create an object representing a file. The path parameter is the 'path' to the file.
var file = File('file.path');

//Async Read file as string
var text = await file.readAsString();
//Async Write the string content to a file.
await file.writeAsString('text');

  • File Class constructor
    File(path)Create an object representing a file, with the parameter 'path' being the file's path。

All methods have synchronous versions. It is recommended to use asynchronous versions. Synchronous methods have a suffix of Sync. For example, readAsString(), the synchronous method is readAsStringSync().

  • Method for reading file content
    readAsString()Async reading of file contents and returning a string. Usually used in conjunction with asynchronous/await.
    readAsStringSync()Synchronize reading of file content and returning a string (Not recommended)
    readAsBytes()Asynchronous reading of file contents and returning a byte list。
    readAsBytesSync(): Corresponding synchronization method

  • Method for Writing File Content
    writeAsString(content, append)Asynchronous writing of string content to a file. Append true means append writeAsBytes(bytes)Asynchronous writing of byte list contents to a file.

  • Methods related to file attributes
    exists()Asynchronous check whether the file exists and return a Boolean value. length()Asynchronous retrieval of file size, in bytes, returns an integer.

  • File operation method: rename(newPath)Rename the file to the specified path.

  • Directory operation related methods
    create(recursive: bool)Create a directory. If recursive is true, multi-level directories can be created

Fetch API Use reference documentation

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