// ==UserScript== // @name WorkBuddy 派猫猫旅行 // @namespace https://www.workbuddy.cn // @version 0.0.2 // @description 定时查询旅行状态,派出猫猫旅行并领取奖励 // @author WorkBuddy // @crontab * * once * * // @grant GM_xmlhttpRequest // @grant GM_cookie // @grant GM_log // @connect www.workbuddy.cn // @icon https://download.codebuddy.cn/web/workbuddy/39aa83f6eb1effda3c999259e7db691102ff873f/assets/logo.svg // ==/UserScript== return new Promise((resolve, reject) => { const BASE = 'https://www.workbuddy.cn'; const HEADERS = { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json', 'X-Client-Platform': 'web' }; const log = (msg) => { try { GM_log(msg); } catch (e) {} }; // 读取目标站 Cookie 用于接口鉴权 const getCookie = () => new Promise((res) => { GM_cookie('list', { url: BASE }, (cookies, err) => { if (err || !Array.isArray(cookies)) return res(''); res(cookies.map(c => c.name + '=' + c.value).join('; ')); }); }); // 通用请求封装 const request = (method, path, body, cookie) => new Promise((res, rej) => { GM_xmlhttpRequest({ url: BASE + path, method: method, headers: Object.assign({}, HEADERS, { Cookie: cookie }), data: body != null ? JSON.stringify(body) : undefined, onload: (r) => res(r), onerror: () => rej(new Error('network error: ' + path)), ontimeout: () => rej(new Error('timeout: ' + path)) }); }); // 解析响应 JSON 的 data 字段 const parseData = (r) => { try { const j = JSON.parse(r.responseText || '{}'); return j && j.data ? j.data : {}; } catch (e) { return {}; } }; (async () => { try { const cookie = await getCookie(); if (!cookie) throw new Error('未获取到登录 Cookie'); // 1. 查询旅行状态 const statusResp = await request('GET', '/activity/growth/buddy/travel/status', null, cookie); const st = parseData(statusResp); const state = String(st.state || 'unknown'); const dailyLimit = !!st.daily_limit_reached; log('状态: ' + state + (dailyLimit ? ' (今日已达上限)' : '')); // 2. 空闲且未达上限时派出猫猫旅行(location_id 1~3 随机) if (state === 'idle' && !dailyLimit) { const locationId = Math.floor(Math.random() * 3) + 1; const depart = await request('POST', '/activity/growth/buddy/travel/depart', { location_id: locationId }, cookie); log('派发结果(' + depart.status + ', location_id=' + locationId + '): ' + (depart.responseText || '').slice(0, 300)); } else { log('跳过派发'); } // 3. 旅行完成时领取奖励 if (state === 'completed' || state === 'claimable' || state === 'arrived') { const claim = await request('POST', '/activity/growth/buddy/travel/claim', {}, cookie); log('领取结果(' + claim.status + '): ' + (claim.responseText || '').slice(0, 300)); } else { log('跳过领取'); } resolve('done'); } catch (e) { log('执行失败: ' + e.message); reject(e); } })(); });