feat: add origin state to KefuWidgetDemo for dynamic script source
This commit is contained in:
@@ -72,10 +72,12 @@ function injectWidget(config: KefuWidgetHostConfig) {
|
|||||||
export function KefuWidgetDemo() {
|
export function KefuWidgetDemo() {
|
||||||
const [config, setConfig] = useState<KefuWidgetHostConfig>(INITIAL_CONFIG)
|
const [config, setConfig] = useState<KefuWidgetHostConfig>(INITIAL_CONFIG)
|
||||||
const [status, setStatus] = useState("请填写 channelId")
|
const [status, setStatus] = useState("请填写 channelId")
|
||||||
|
const [origin, setOrigin] = useState("")
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = window.setTimeout(() => {
|
const timer = window.setTimeout(() => {
|
||||||
const initialConfig = getDefaultConfig()
|
const initialConfig = getDefaultConfig()
|
||||||
|
setOrigin(window.location.origin)
|
||||||
setConfig(initialConfig)
|
setConfig(initialConfig)
|
||||||
setStatus(initialConfig.channelId ? "Widget 已挂载" : "请填写 channelId")
|
setStatus(initialConfig.channelId ? "Widget 已挂载" : "请填写 channelId")
|
||||||
|
|
||||||
@@ -91,13 +93,17 @@ export function KefuWidgetDemo() {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const snippet = useMemo(() => {
|
const snippet = useMemo(() => {
|
||||||
|
const scriptSrc = origin
|
||||||
|
? `${origin}/sdk/cs-ai-agent-sdk.min.js`
|
||||||
|
: "/sdk/cs-ai-agent-sdk.min.js"
|
||||||
|
|
||||||
return `<script>
|
return `<script>
|
||||||
window.CSAgentConfig = {
|
window.CSAgentConfig = {
|
||||||
channelId: "${config.channelId || ""}"
|
channelId: "${config.channelId || ""}"
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
<script async src="/sdk/cs-ai-agent-sdk.min.js"></script>`
|
<script async src="${scriptSrc}"></script>`
|
||||||
}, [config])
|
}, [config, origin])
|
||||||
|
|
||||||
function updateField<K extends keyof KefuWidgetHostConfig>(
|
function updateField<K extends keyof KefuWidgetHostConfig>(
|
||||||
key: K,
|
key: K,
|
||||||
|
|||||||
Reference in New Issue
Block a user