{"record":{"id":"2616f4c3bd8ee189","repo":"jackwener/OpenCLI","slug":"1688-action-navigation-lost-the-current-browser","errorCode":null,"errorMessage":"1688 ${action} navigation lost the current browser target","messagePattern":"1688 (.+?) navigation lost the current browser target","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/1688/shared.js","lineNumber":401,"sourceCode":"  `);\n    return {\n        href: cleanText(result.href),\n        title: cleanText(result.title),\n        body_text: cleanMultilineText(result.body_text),\n    };\n}\nexport async function gotoAndReadState(page, url, settleMs = 2500, action = 'page') {\n    try {\n        await page.goto(url, { settleMs });\n        await page.wait(1.5);\n        return readPageState(page);\n    }\n    catch (error) {\n        const message = error instanceof Error ? error.message : String(error);\n        if (message.includes('Inspected target navigated or closed')\n            || message.includes('Cannot find context with specified id')\n            || message.includes('Target closed')) {\n            throw new CommandExecutionError(`1688 ${action} navigation lost the current browser target`, `${buildCaptchaHint(action)} If CDP is attached to a stale or blocked tab, open a fresh 1688 tab and point OPENCLI_CDP_TARGET at that tab.`);\n        }\n        throw error;\n    }\n}\nexport async function ensure1688Session(page) {\n    const state = await gotoAndReadState(page, HOME_URL, 1500, 'homepage');\n    assertAuthenticatedState(state, 'homepage');\n}\nexport function assertAuthenticatedState(state, action) {\n    if (!isCaptchaState(state) && !isLoginState(state))\n        return;\n    throw new AuthRequiredError('1688.com', `请先在共享 Chrome 完成 1688 登录/验证，再重试（${action}）`);\n}\nexport function assertNotCaptcha(state, action) {\n    assertAuthenticatedState(state, action);\n}\nexport function toNumber(value) {\n    if (typeof value === 'number' && Number.isFinite(value)) {","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/1688/shared.js#L383-L419","documentation":"gotoAndReadState navigates the page and reads DOM state; if navigation fails because the CDP-attached browser target navigated away or closed, it throws CommandExecutionError with a captcha-hint message. This converts Puppeteer/CDP connection-level failures ('Inspected target navigated or closed', 'Cannot find context with specified id', 'Target closed') into an actionable CLI error telling the user to attach to a fresh tab.","triggerScenarios":"OPENCLI_CDP_TARGET points at a tab that closes or navigates during gotoAndReadState; the CDP execution context is destroyed mid-navigation; the shared Chrome tab is closed by the user; 1688 redirects/crashes the tab during anti-bot interstitials.","commonSituations":"Stale OPENCLI_CDP_TARGET after Chrome restarted or tabs changed; automation attached to an incognito/temporary tab; 1688 anti-bot flow navigating the tab mid-scrape; CDP session invalidated by a manual user action in shared Chrome.","solutions":["Open a fresh 1688 tab in the shared Chrome instance and set OPENCLI_CDP_TARGET to that new tab's target, then retry.","Verify the target tab is still open and stable (not auto-refreshing/closing) before running the command.","Reconnect the CDP attachment (restart the CLI session) to drop the stale session; if anti-bot keeps hijacking the tab, complete login/captcha in Chrome first."],"exampleFix":"// before\nOPENCLI_CDP_TARGET=old-closed-tab opencli 1688 item 887904326744\n// after — attach to a live 1688 tab\nexport OPENCLI_CDP_TARGET=$(opencli chrome target --url 'detail.1688.com')\nopencli 1688 item 887904326744","handlingStrategy":"try-catch","validationCode":"// before running: verify the CDP target tab is alive\nconst targetAlive = await fetch(`${CDP_HTTP}/json/list`)\n  .then(r => r.json())\n  .then(list => list.some(t => t.id === TARGET_ID && !t.url.startsWith('devtools')));\nif (!targetAlive) throw new Error('OPENCLI_CDP_TARGET is stale — attach to a live 1688 tab');","typeGuard":"function isHealthyCdpTarget(target) {\n  return Boolean(target && target.id && target.url && !target.url.startsWith('devtools') && !target.closed);\n}","tryCatchPattern":"try {\n  await run1688Command();\n} catch (e) {\n  if (e instanceof CommandExecutionError && e.message.includes('lost the current browser target')) {\n    // refresh OPENCLI_CDP_TARGET to a fresh 1688 tab and retry once\n    const tabs = await fetch(`${CDP_HTTP}/json/list`).then(r => r.json());\n    const tab = tabs.find(t => t.url.includes('1688.com'));\n    if (tab) { process.env.OPENCLI_CDP_TARGET = tab.id; return run1688Command(); }\n  }\n  throw e;\n}","preventionTips":["Re-resolve OPENCLI_CDP_TARGET at startup instead of caching a tab id across sessions.","Keep a dedicated, stable 1688 tab open in shared Chrome for automation.","Avoid attaching to incognito/temporary tabs that users may close.","Detect the three underlying CDP messages and auto-recover by reattaching."],"tags":["cdp","puppeteer","browser-target","navigation"],"backgroundTag":"cdp-target-closed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}