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

53 lines
2.1 KiB
JavaScript

import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { registerSiteAuthCommands } from '../_shared/site-auth.js';
async function hasRednoteSessionCookie(page) {
const cookies = await page.getCookies({ url: 'https://www.rednote.com' });
return cookies.some(c => c.name === 'web_session' && c.value);
}
async function verifyRednoteIdentity(page) {
if (!await hasRednoteSessionCookie(page)) {
throw new AuthRequiredError('rednote.com', 'Rednote web_session cookie missing');
}
await page.goto('https://www.rednote.com/explore');
await page.wait(2);
const probe = await page.evaluate(`
(() => {
const state = window.__INITIAL_STATE__;
if (!state?.user) {
return { kind: 'auth', detail: 'Rednote __INITIAL_STATE__.user missing' };
}
const loggedIn = state.user.loggedIn?._value;
const userInfo = state.user.userInfo?._value || {};
if (loggedIn !== true) {
return { kind: 'auth', detail: 'Rednote loggedIn._value=' + String(loggedIn) + ' — anonymous' };
}
const userId = String(userInfo.userId || userInfo.user_id || '');
const nickname = String(userInfo.nickname || userInfo.name || '');
if (!userId) {
return { kind: 'auth', detail: 'Rednote logged-in but userId missing — stale session' };
}
return { ok: true, user_id: userId, nickname };
})()
`);
if (probe?.kind === 'auth') throw new AuthRequiredError('rednote.com', probe.detail);
if (!probe?.ok) throw new CommandExecutionError(`Unexpected Rednote probe: ${JSON.stringify(probe)}`);
return { user_id: probe.user_id, nickname: probe.nickname };
}
registerSiteAuthCommands({
site: 'rednote',
domain: 'rednote.com',
loginUrl: 'https://www.rednote.com/explore',
columns: ['user_id', 'nickname'],
quickCheck: hasRednoteSessionCookie,
verify: verifyRednoteIdentity,
poll: async (page) => {
if (!await hasRednoteSessionCookie(page)) {
throw new AuthRequiredError('rednote.com', 'Waiting for Rednote web_session cookie');
}
return verifyRednoteIdentity(page);
},
});