Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

47 lines
1.8 KiB
JavaScript

import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { registerSiteAuthCommands } from '../_shared/site-auth.js';
async function hasJdSessionCookie(page) {
const cookies = await page.getCookies({ url: 'https://www.jd.com' });
const names = new Set(cookies.map(c => c.name));
return names.has('pin') || names.has('thor');
}
async function verifyJdIdentity(page) {
if (!await hasJdSessionCookie(page)) {
throw new AuthRequiredError('jd.com', 'JD pin / thor cookie missing');
}
await page.goto('https://home.jd.com/');
await page.wait(3);
const probe = await page.evaluate(`
(() => {
const pinCookie = (document.cookie.split('; ').find(c => c.startsWith('pin=')) || '').split('=')[1] || '';
const decoded = pinCookie ? decodeURIComponent(pinCookie) : '';
if (!decoded) {
return { kind: 'auth', detail: 'JD pin cookie empty after decode' };
}
const nickEl = document.querySelector('.user-info, #aliveUserName, .name, .user-name');
const nickname = (nickEl && nickEl.textContent && nickEl.textContent.trim()) || '';
return { ok: true, pin: decoded, nickname };
})()
`);
if (probe?.kind === 'auth') throw new AuthRequiredError('jd.com', probe.detail);
if (!probe?.ok) throw new CommandExecutionError(`Unexpected JD probe: ${JSON.stringify(probe)}`);
return { pin: probe.pin, nickname: probe.nickname };
}
registerSiteAuthCommands({
site: 'jd',
domain: 'jd.com',
loginUrl: 'https://passport.jd.com/new/login.aspx',
columns: ['pin', 'nickname'],
quickCheck: hasJdSessionCookie,
verify: verifyJdIdentity,
poll: async (page) => {
if (!await hasJdSessionCookie(page)) {
throw new AuthRequiredError('jd.com', 'Waiting for JD pin / thor cookie');
}
return verifyJdIdentity(page);
},
});