调用示例

JavaScript (Axios)

import axios from 'axios';

const API_BASE = 'https://shater.online/shater';

// 推送拦截数据
async function pushEntry(sessionId, entry) {
  const response = await axios.post(`${API_BASE}/raw-data?sessionId=${sessionId}`, entry);
  return response.data;
}

// 查询拦截记录
async function getEntries(sessionId) {
  const response = await axios.get(`${API_BASE}/entries?sessionId=${sessionId}`);
  return response.data;
}

// 清空拦截记录
async function clearEntries(sessionId) {
  const response = await axios.delete(`${API_BASE}/entries?sessionId=${sessionId}`);
  return response.data;
}

// 删除单条记录
async function deleteEntry(sessionId, id) {
  const response = await axios.delete(`${API_BASE}/entries/${id}?sessionId=${sessionId}`);
  return response.data;
}

// 查询在线状态
async function checkOnline(sessionId) {
  const response = await axios.get(`${API_BASE}/online?sessionId=${sessionId}`);
  return response.data.online;
}

WebSocket (原生)

const sessionId = 'abc123def456ghi';
const ws = new WebSocket(`wss://shater.online/ws?sessionId=${sessionId}`);

ws.onopen = () => {
  console.log('WebSocket 连接成功');

  // 启动心跳
  setInterval(() => {
    ws.send(JSON.stringify({ type: 'ping' }));
  }, 30000);
};

ws.onmessage = (event) => {
  const message = JSON.parse(event.data);

  switch (message.type) {
    case 'connected':
      console.log(`已连接,当前记录数:${message.data}`);
      break;
    case 'entry-received':
      console.log('新记录:', message.data);
      break;
    case 'entry-updated':
      console.log('记录更新:', message.data);
      break;
    case 'deleted':
      console.log('记录删除:', message.data);
      break;
    case 'cleared':
      console.log('记录已清空');
      break;
    case 'pong':
      // 心跳响应
      break;
  }
};

ws.onerror = (error) => {
  console.error('WebSocket 错误:', error);
};

ws.onclose = () => {
  console.log('WebSocket 断开,3 秒后重连...');
  setTimeout(() => {
    // 重新连接
    location.reload();
    // 或重新初始化 WebSocket
  }, 3000);
};

双阶段推送完整示例

const REPORT_URL = 'https://shater.online/shater/raw-data';
const SESSION_ID = '7a58592c72f04fe';

// 全局 fetch 拦截(双阶段推送)
const originalFetch = window.fetch;
window.fetch = async function(input, init) {
  const requestId = `req-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
  const startTime = Date.now();

  // 1. Pending 阶段:立即推送
  fetch(`${REPORT_URL}?sessionId=${SESSION_ID}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      api: new URL(input, location.href).pathname,
      method: init?.method || 'GET',
      requestId,
      timeConsuming: 0,
      response: '',
    }),
  }).catch(() => {});

  // 2. 执行原请求
  const response = await originalFetch(input, init);
  const timeConsuming = Date.now() - startTime;

  // 3. Complete 阶段:完成后更新
  fetch(`${REPORT_URL}?sessionId=${SESSION_ID}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      api: new URL(input, location.href).pathname,
      method: init?.method || 'GET',
      requestId,
      timeConsuming,
      requestHeader: JSON.stringify(init?.headers || {}),
      request: init?.body ? String(init.body) : '',
      responseHeader: JSON.stringify(Object.fromEntries(response.headers.entries())),
      response: await response.clone().text(),
    }),
  }).catch(() => {});

  return response;
};

cURL 示例

推送拦截数据

curl -X POST 'https://shater.online/shater/raw-data?sessionId=7a58592c72f04fe' \
  -H 'Content-Type: application/json' \
  -d '{
    "api": "/api/user/info",
    "method": "POST",
    "request": "{\"userId\": 123}",
    "response": "{\"code\": 0}",
    "timeConsuming": 120
  }'

查询拦截记录

curl 'https://shater.online/shater/entries?sessionId=7a58592c72f04fe'

清空拦截记录

curl -X DELETE 'https://shater.online/shater/entries?sessionId=7a58592c72f04fe'

上传截图

curl -X POST 'https://shater.online/upload?sessionId=7a58592c72f04fe' \
  -F 'screenshot=@/path/to/screenshot.jpg'

相关文档