9b395f5cc3
Build Chrome Extension / build (push) Waiting to run
Trigger Website Rebuild (Docs Updated) / dispatch (push) Waiting to run
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
51 lines
1.9 KiB
JavaScript
51 lines
1.9 KiB
JavaScript
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { registerSiteAuthCommands } from '../_shared/site-auth.js';
|
|
|
|
async function hasDoubanSessionCookie(page) {
|
|
const cookies = await page.getCookies({ url: 'https://www.douban.com' });
|
|
const names = new Set(cookies.map(c => c.name));
|
|
return names.has('dbcl2') || names.has('ck');
|
|
}
|
|
|
|
async function verifyDoubanIdentity(page) {
|
|
if (!await hasDoubanSessionCookie(page)) {
|
|
throw new AuthRequiredError('douban.com', 'Douban dbcl2 / ck cookies missing');
|
|
}
|
|
await page.goto('https://www.douban.com/');
|
|
await page.wait(2);
|
|
const probe = await page.evaluate(`
|
|
(() => {
|
|
const navUser = document.querySelector('.nav-user-account .bn-more, .top-nav-info a.bn-more');
|
|
if (!navUser) {
|
|
return { kind: 'auth', detail: 'Douban nav-user element missing — not signed in' };
|
|
}
|
|
const href = navUser.getAttribute('href') || '';
|
|
const m = href.match(/people\\/(\\d+)\\/?/);
|
|
const user_id = m ? m[1] : '';
|
|
const name = (navUser.textContent || '').trim();
|
|
if (!user_id) {
|
|
return { kind: 'auth', detail: 'Douban user_id parse failed: href=' + href };
|
|
}
|
|
return { ok: true, user_id, name };
|
|
})()
|
|
`);
|
|
if (probe?.kind === 'auth') throw new AuthRequiredError('douban.com', probe.detail);
|
|
if (!probe?.ok) throw new CommandExecutionError(`Unexpected Douban probe: ${JSON.stringify(probe)}`);
|
|
return { user_id: probe.user_id, name: probe.name };
|
|
}
|
|
|
|
registerSiteAuthCommands({
|
|
site: 'douban',
|
|
domain: 'douban.com',
|
|
loginUrl: 'https://accounts.douban.com/passport/login',
|
|
columns: ['user_id', 'name'],
|
|
quickCheck: hasDoubanSessionCookie,
|
|
verify: verifyDoubanIdentity,
|
|
poll: async (page) => {
|
|
if (!await hasDoubanSessionCookie(page)) {
|
|
throw new AuthRequiredError('douban.com', 'Waiting for Douban dbcl2 / ck cookies');
|
|
}
|
|
return verifyDoubanIdentity(page);
|
|
},
|
|
});
|