Files
wehub-resource-sync 9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

60 lines
2.3 KiB
JavaScript

import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { registerSiteAuthCommands } from '../_shared/site-auth.js';
import { hasLoginGate, ensureYuanbaoPage, YUANBAO_DOMAIN, YUANBAO_URL } from './shared.js';
async function hasYuanbaoUserCookie(page) {
const cookies = await page.getCookies({ url: 'https://yuanbao.tencent.com' });
return cookies.some(c => c.name === 'hy_user' && c.value);
}
async function verifyYuanbaoIdentity(page) {
await ensureYuanbaoPage(page);
if (await hasLoginGate(page)) {
throw new AuthRequiredError(YUANBAO_DOMAIN, 'Yuanbao showed wx login gate — anonymous session');
}
const cookies = await page.getCookies({ url: 'https://yuanbao.tencent.com' });
const hyUser = cookies.find(c => c.name === 'hy_user')?.value || '';
if (!hyUser) {
throw new AuthRequiredError(YUANBAO_DOMAIN, 'Yuanbao hy_user cookie missing — anonymous session');
}
const probe = await page.evaluate(`
(() => {
const bodyText = document.body?.innerText || '';
const nickMatch = bodyText.match(/用户[a-z0-9]{4,}/i);
const state = window.__INITIAL_STATE__ || {};
const seen = new Set();
const stack = [state];
let stateNick = '';
while (stack.length) {
const node = stack.pop();
if (!node || typeof node !== 'object' || seen.has(node)) continue;
seen.add(node);
if (Array.isArray(node)) { stack.push(...node); continue; }
const u = node.userInfo || node.user;
if (u && typeof u === 'object' && (u.nickname || u.nick)) {
stateNick = String(u.nickname || u.nick);
break;
}
for (const v of Object.values(node)) if (v && typeof v === 'object') stack.push(v);
}
return { nickname: stateNick || (nickMatch ? nickMatch[0] : '') };
})()
`);
return { user_id: String(hyUser), nickname: String(probe.nickname || '') };
}
registerSiteAuthCommands({
site: 'yuanbao',
domain: YUANBAO_DOMAIN,
loginUrl: YUANBAO_URL,
columns: ['user_id', 'nickname'],
quickCheck: hasYuanbaoUserCookie,
verify: verifyYuanbaoIdentity,
poll: async (page) => {
if (!await hasYuanbaoUserCookie(page)) {
throw new AuthRequiredError(YUANBAO_DOMAIN, 'Waiting for Yuanbao hy_user cookie');
}
return verifyYuanbaoIdentity(page);
},
});