脚本引擎 API

Cookie 管理 API

runtime.cookies.* 手动管理 HTTP Cookie 的持久化。可设置、读取、清除 Cookie。

当前:Cookie 管理 API
脚本引擎 API

Cookie 管理 API

runtime.cookies.* 手动管理 HTTP Cookie 的持久化。可设置、读取、清除 Cookie。

概述

手动管理跨请求的 HTTP Cookie。所有网络请求(getText/getBytes/request 等)已自动携带持久化的 Cookie 并保存响应 Set-Cookie,大多数场景无需手动调用。仅在需要手动注入特定 Cookie 或清除会话时使用。

方法列表

  • runtime.cookies.setCookie(url: string, cookieString: string): void — 为指定 URL 手动设置 Cookie(格式如 session_id=abc123)。
  • runtime.cookies.getCookie(url: string): string — 获取指定 URL 关联的所有 Cookie(格式 key1=val1; key2=val2)。
  • runtime.cookies.clearCookies(): void — 清除所有已持久化的 Cookie。

调用示例

// 手动注入 Cookie 后请求
runtime.cookies.setCookie("https://example.com", "auth_token=eyJhbGciOi...");
var data = runtime.network.getText("https://example.com/api/data");
runtime.process.log("数据: " + data);
// 切换账号时清除 Cookie
runtime.cookies.clearCookies();
// 重新登录
runtime.network.postText("https://example.com/login", "user=new_user&pass=456");
// 新会话的 Cookie 自动生效
var profile = runtime.network.getText("https://example.com/profile");
runtime.process.log("新用户资料: " + data);