Files
jackwener__opencli/clis/yuanbao/shared.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

50 lines
1.9 KiB
JavaScript

import { AuthRequiredError } from '@jackwener/opencli/errors';
export const YUANBAO_DOMAIN = 'yuanbao.tencent.com';
export const YUANBAO_URL = 'https://yuanbao.tencent.com/';
const SESSION_HINT = 'Likely login/auth/challenge/session issue in the existing yuanbao.tencent.com browser session.';
/**
* Reusable visibility check for injected browser scripts.
* Embed in page.evaluate strings via `${IS_VISIBLE_JS}`.
*/
export const IS_VISIBLE_JS = `const isVisible = (node) => {
if (!(node instanceof HTMLElement)) return false;
const rect = node.getBoundingClientRect();
const style = window.getComputedStyle(node);
return rect.width > 0
&& rect.height > 0
&& style.display !== 'none'
&& style.visibility !== 'hidden';
};`;
export function authRequired(message) {
return new AuthRequiredError(YUANBAO_DOMAIN, `${message} ${SESSION_HINT}`);
}
export async function isOnYuanbao(page) {
const url = await page.evaluate('window.location.href').catch(() => '');
if (typeof url !== 'string' || !url)
return false;
try {
const hostname = new URL(url).hostname;
return hostname === YUANBAO_DOMAIN || hostname.endsWith(`.${YUANBAO_DOMAIN}`);
}
catch {
return false;
}
}
export async function ensureYuanbaoPage(page) {
if (!(await isOnYuanbao(page))) {
await page.goto(YUANBAO_URL, { waitUntil: 'load', settleMs: 2500 });
await page.wait(1);
}
}
export async function hasLoginGate(page) {
const result = await page.evaluate(`(() => {
const bodyText = document.body.innerText || '';
const hasWechatLoginText = bodyText.includes('微信扫码登录');
const hasWechatIframe = Array.from(document.querySelectorAll('iframe'))
.some((frame) => (frame.getAttribute('src') || '').includes('open.weixin.qq.com/connect/qrconnect'));
return hasWechatLoginText || hasWechatIframe;
})()`);
return Boolean(result);
}