Merge pull request #501 from alibaba/feat/iife-cdn-without-auto-init
feat: add autoInit switch for demo CDN script
This commit is contained in:
@@ -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 |
|
| 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 |
|
| 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
|
### NPM Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -52,6 +52,8 @@
|
|||||||
| Global | https://cdn.jsdelivr.net/npm/page-agent@1.8.1/dist/iife/page-agent.demo.js |
|
| 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 |
|
| 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 安装
|
### NPM 安装
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -3,8 +3,12 @@
|
|||||||
*/
|
*/
|
||||||
import { PageAgent, type PageAgentConfig } from './PageAgent'
|
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
|
// Clean up existing instances to prevent multiple injections from bookmarklet
|
||||||
if (window.pageAgent) {
|
if (autoInit && window.pageAgent) {
|
||||||
window.pageAgent.dispose()
|
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_BASE_URL = 'https://page-ag-testing-ohftxirgbn.cn-shanghai.fcapp.run'
|
||||||
const DEMO_API_KEY = 'NA'
|
const DEMO_API_KEY = 'NA'
|
||||||
|
|
||||||
const currentScript = document.currentScript as HTMLScriptElement | null
|
|
||||||
|
|
||||||
// in case document.x is not ready yet
|
// in case document.x is not ready yet
|
||||||
setTimeout(() => {
|
if (autoInit) {
|
||||||
let config: PageAgentConfig
|
setTimeout(() => {
|
||||||
let showPanel = true
|
let config: PageAgentConfig
|
||||||
|
let showPanel = true
|
||||||
|
|
||||||
if (currentScript) {
|
if (currentScriptURL) {
|
||||||
console.log('🚀 page-agent.js detected current script:', currentScript.src)
|
const url = currentScriptURL
|
||||||
const url = new URL(currentScript.src)
|
const model = url.searchParams.get('model') || DEMO_MODEL
|
||||||
const model = url.searchParams.get('model') || DEMO_MODEL
|
const baseURL = url.searchParams.get('baseURL') || DEMO_BASE_URL
|
||||||
const baseURL = url.searchParams.get('baseURL') || DEMO_BASE_URL
|
const apiKey = url.searchParams.get('apiKey') || DEMO_API_KEY
|
||||||
const apiKey = url.searchParams.get('apiKey') || DEMO_API_KEY
|
const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN'
|
||||||
const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN'
|
showPanel = ((url.searchParams.get('showPanel') as 'true' | 'false') || 'true') === 'true'
|
||||||
showPanel = ((url.searchParams.get('showPanel') as 'true' | 'false') || 'true') === 'true'
|
config = { model, baseURL, apiKey, language }
|
||||||
config = { model, baseURL, apiKey, language }
|
} else {
|
||||||
} else {
|
console.log('🚀 page-agent.js no current script detected, using default demo config')
|
||||||
console.log('🚀 page-agent.js no current script detected, using default demo config')
|
config = {
|
||||||
config = {
|
model: import.meta.env.LLM_MODEL_NAME ? import.meta.env.LLM_MODEL_NAME : DEMO_MODEL,
|
||||||
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,
|
||||||
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,
|
||||||
apiKey: import.meta.env.LLM_API_KEY ? import.meta.env.LLM_API_KEY : DEMO_API_KEY,
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Create agent
|
// Create agent
|
||||||
window.pageAgent = new PageAgent(config)
|
window.pageAgent = new PageAgent(config)
|
||||||
if (showPanel) {
|
if (showPanel) {
|
||||||
window.pageAgent.panel.show()
|
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)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,6 +58,11 @@ export default function QuickStart() {
|
|||||||
code={`<script src="DEMO_CDN_URL" crossorigin="true"></script>`}
|
code={`<script src="DEMO_CDN_URL" crossorigin="true"></script>`}
|
||||||
language="html"
|
language="html"
|
||||||
/>
|
/>
|
||||||
|
<p className="text-sm text-gray-600 dark:text-gray-300 mb-3">
|
||||||
|
{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(...).'}
|
||||||
|
</p>
|
||||||
<table className="w-full border-collapse text-sm">
|
<table className="w-full border-collapse text-sm">
|
||||||
<thead>
|
<thead>
|
||||||
<tr className="border-b border-gray-200 dark:border-gray-700">
|
<tr className="border-b border-gray-200 dark:border-gray-700">
|
||||||
|
|||||||
Reference in New Issue
Block a user