From 22a5019df07cae66dfa5a31ebf50d22b98227b7a Mon Sep 17 00:00:00 2001 From: ray <1416431931@qq.com> Date: Wed, 29 Apr 2026 15:46:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=A2=E5=8D=95=E7=88=AC?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aliyun-sync/aliyun-aps-sync/src/sync.js | 31 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/aliyun-sync/aliyun-aps-sync/src/sync.js b/aliyun-sync/aliyun-aps-sync/src/sync.js index 9ff7734..10d029f 100644 --- a/aliyun-sync/aliyun-aps-sync/src/sync.js +++ b/aliyun-sync/aliyun-aps-sync/src/sync.js @@ -207,6 +207,27 @@ async function getContext() { return _context; } +async function resolveActivePage(context, targetUrl = '') { + const pages = context.pages(); + let page = null; + + if (config.browserMode === 'cdp' && targetUrl) { + page = pages.find((item) => item.url().includes(targetUrl)); + } + + if (!page) { + page = pages[0] || await context.newPage(); + } + + if (config.browserMode === 'cdp') { + const pageIndex = pages.indexOf(page); + console.log(`[CDP] 使用 tab=${pageIndex >= 0 ? pageIndex : 'new'} url=${page.url() || '(blank)'}`); + await page.bringToFront().catch(() => null); + } + + return page; +} + async function restoreStorageState(context) { if (!fs.existsSync(config.storageStateFile)) { return; @@ -427,7 +448,7 @@ export async function login() { process.once('SIGTERM', onSigterm); try { - const page = context.pages()[0] || (await context.newPage()); + const page = await resolveActivePage(context, '/detail/my_customer/~/customer/list'); await page.goto(datasets.customers.url, { waitUntil: 'domcontentloaded' }); console.log('请在打开的浏览器里完成阿里云伙伴中心登录。检测到进入“我的客户”和“账单查询”页面后,脚本会自动保存登录态并关闭浏览器。'); await waitUntilReady(page, datasets.customers.heading, 10 * 60 * 1000, { allowInteractiveAuth: true }); @@ -453,7 +474,7 @@ export async function syncAll(options = {}) { try { const summary = { startedAt: new Date().toISOString(), datasets: {} }; - page = context.pages()[0] || (await context.newPage()); + page = await resolveActivePage(context, '/detail/my_customer/~/customer/list'); if (config.fullSync) { summary.datasets.customers = await syncCustomers(page); @@ -496,7 +517,7 @@ export async function syncBillsOnly(options = {}) { try { const summary = { startedAt: new Date().toISOString(), datasets: {} }; - page = context.pages()[0] || (await context.newPage()); + page = await resolveActivePage(context, '/detail/bill/~/costCenter/bill'); summary.datasets.bills = await syncBills(page, options); summary.finishedAt = new Date().toISOString(); @@ -526,7 +547,7 @@ export async function syncMessagesOnly(options = {}) { try { const summary = { startedAt: new Date().toISOString(), datasets: {} }; - page = context.pages()[0] || (await context.newPage()); + page = await resolveActivePage(context, '/message'); summary.datasets.messages = await syncMessages(page, options); summary.finishedAt = new Date().toISOString(); @@ -574,7 +595,7 @@ export async function syncAllIncremental() { try { const summary = { startedAt: new Date().toISOString(), mode: 'incremental', datasets: {} }; - page = context.pages()[0] || (await context.newPage()); + page = await resolveActivePage(context, '/detail/order/~/costCenter/order'); summary.datasets.orders = await syncOrders(page, { incremental: true }); const latestOrders = loadCurrentState('orders', datasets.orders.uniqueKey); const orderIdsForDetail = collectValidOrderIds(latestOrders.records || []);