From 35ab12a20a0f137015acc36a1e3ab0c5cea45935 Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Mon, 11 May 2026 19:20:52 +0800 Subject: [PATCH 1/2] feat: add `autoInit` switch for IIFE demo --- packages/page-agent/src/demo.ts | 61 +++++++++++++++++---------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/packages/page-agent/src/demo.ts b/packages/page-agent/src/demo.ts index ee19603..e15ba39 100644 --- a/packages/page-agent/src/demo.ts +++ b/packages/page-agent/src/demo.ts @@ -3,8 +3,12 @@ */ import { PageAgent, type PageAgentConfig } from './PageAgent' +const currentScript = document.currentScript as HTMLScriptElement | null +const currentScriptURL = currentScript?.src ? new URL(currentScript.src) : null +const autoInit = currentScriptURL?.searchParams.get('autoInit') !== 'false' + // Clean up existing instances to prevent multiple injections from bookmarklet -if (window.pageAgent) { +if (autoInit && window.pageAgent) { window.pageAgent.dispose() } @@ -17,36 +21,35 @@ const DEMO_MODEL = 'qwen3.5-plus' const DEMO_BASE_URL = 'https://page-ag-testing-ohftxirgbn.cn-shanghai.fcapp.run' const DEMO_API_KEY = 'NA' -const currentScript = document.currentScript as HTMLScriptElement | null - // in case document.x is not ready yet -setTimeout(() => { - let config: PageAgentConfig - let showPanel = true +if (autoInit) { + setTimeout(() => { + let config: PageAgentConfig + let showPanel = true - if (currentScript) { - console.log('🚀 page-agent.js detected current script:', currentScript.src) - const url = new URL(currentScript.src) - const model = url.searchParams.get('model') || DEMO_MODEL - const baseURL = url.searchParams.get('baseURL') || DEMO_BASE_URL - const apiKey = url.searchParams.get('apiKey') || DEMO_API_KEY - const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN' - showPanel = ((url.searchParams.get('showPanel') as 'true' | 'false') || 'true') === 'true' - config = { model, baseURL, apiKey, language } - } else { - console.log('🚀 page-agent.js no current script detected, using default demo config') - config = { - model: import.meta.env.LLM_MODEL_NAME ? import.meta.env.LLM_MODEL_NAME : DEMO_MODEL, - baseURL: import.meta.env.LLM_BASE_URL ? import.meta.env.LLM_BASE_URL : DEMO_BASE_URL, - apiKey: import.meta.env.LLM_API_KEY ? import.meta.env.LLM_API_KEY : DEMO_API_KEY, + if (currentScriptURL) { + const url = currentScriptURL + const model = url.searchParams.get('model') || DEMO_MODEL + const baseURL = url.searchParams.get('baseURL') || DEMO_BASE_URL + const apiKey = url.searchParams.get('apiKey') || DEMO_API_KEY + const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN' + showPanel = ((url.searchParams.get('showPanel') as 'true' | 'false') || 'true') === 'true' + config = { model, baseURL, apiKey, language } + } else { + console.log('🚀 page-agent.js no current script detected, using default demo config') + config = { + model: import.meta.env.LLM_MODEL_NAME ? import.meta.env.LLM_MODEL_NAME : DEMO_MODEL, + baseURL: import.meta.env.LLM_BASE_URL ? import.meta.env.LLM_BASE_URL : DEMO_BASE_URL, + apiKey: import.meta.env.LLM_API_KEY ? import.meta.env.LLM_API_KEY : DEMO_API_KEY, + } } - } - // Create agent - window.pageAgent = new PageAgent(config) - if (showPanel) { - window.pageAgent.panel.show() - } + // Create agent + window.pageAgent = new PageAgent(config) + if (showPanel) { + window.pageAgent.panel.show() + } - console.log('🚀 page-agent.js initialized with config:', window.pageAgent.config) -}) + console.log('🚀 page-agent.js initialized with config:', window.pageAgent.config) + }) +} From f05bf68f61a67c8bdd905c2463d67cb151e2809a Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Mon, 11 May 2026 19:28:49 +0800 Subject: [PATCH 2/2] docs: `?autoInit=false` docs --- README.md | 2 ++ docs/README-zh.md | 2 ++ .../website/src/pages/docs/introduction/quick-start/page.tsx | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/README.md b/README.md index 8821929..44789b7 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ Fastest way to try PageAgent with our free Demo LLM: | Global | https://cdn.jsdelivr.net/npm/page-agent@1.8.1/dist/iife/page-agent.demo.js | | China | https://registry.npmmirror.com/page-agent/1.8.1/files/dist/iife/page-agent.demo.js | +Add `?autoInit=false` to load the script without creating the demo agent automatically. You can then instantiate it with `new window.PageAgent(...)`. + ### NPM Installation ```bash diff --git a/docs/README-zh.md b/docs/README-zh.md index 77231fc..0a23570 100644 --- a/docs/README-zh.md +++ b/docs/README-zh.md @@ -52,6 +52,8 @@ | Global | https://cdn.jsdelivr.net/npm/page-agent@1.8.1/dist/iife/page-agent.demo.js | | China | https://registry.npmmirror.com/page-agent/1.8.1/files/dist/iife/page-agent.demo.js | +在 URL 后添加 `?autoInit=false` 可只加载脚本,不自动创建 Demo Agent;之后可通过 `new window.PageAgent(...)` 手动初始化。 + ### NPM 安装 ```bash diff --git a/packages/website/src/pages/docs/introduction/quick-start/page.tsx b/packages/website/src/pages/docs/introduction/quick-start/page.tsx index 86fafc1..7d0aa9a 100644 --- a/packages/website/src/pages/docs/introduction/quick-start/page.tsx +++ b/packages/website/src/pages/docs/introduction/quick-start/page.tsx @@ -58,6 +58,11 @@ export default function QuickStart() { code={``} language="html" /> +

+ {isZh + ? '在 URL 后添加 ?autoInit=false 可只加载脚本,不自动创建 Demo Agent;之后可通过 new window.PageAgent(...) 手动初始化。' + : 'Add ?autoInit=false to load the script without creating the demo agent automatically. You can then instantiate it with new window.PageAgent(...).'} +