wix-cli-embedded-script
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWix Embedded Script Builder
Wix 嵌入式脚本构建工具
Creates embedded script extensions for Wix CLI applications. Embedded scripts are HTML code fragments that get injected into the DOM of Wix sites, enabling integration with third-party services, analytics tracking, advertising, and custom JavaScript functionality.
为Wix CLI应用创建嵌入式脚本扩展。嵌入式脚本是会被注入到Wix网站DOM中的HTML代码片段,可实现与第三方服务、分析跟踪、广告以及自定义JavaScript功能的集成。
Quick Start Checklist
快速开始清单
Follow these steps in order when creating an embedded script:
- Create script folder:
src/site/embedded-scripts/<script-name>/ - Create with config element, styles, and script logic
embedded.html - Create with
extensions.tsand unique UUIDextensions.embeddedScript() - Create dashboard config page:
src/dashboard/pages/<script-name>-settings/ - Implement config page with API from
embeddedScripts@wix/app-management - Update to import and use both extensions
src/extensions.ts - Run to verify TypeScript compiles
npx tsc --noEmit - Run and
npx wix buildto testnpx wix preview - Add the permission in the Wix Dev Center (see Enable Embedded Script Permission)
APPS.MANAGE_EMBEDDED_SCRIPT
创建嵌入式脚本时,请按以下步骤依次操作:
- 创建脚本文件夹:
src/site/embedded-scripts/<script-name>/ - 创建文件,包含配置元素、样式和脚本逻辑
embedded.html - 创建文件,使用
extensions.ts并配置唯一UUIDextensions.embeddedScript() - 创建仪表板配置页面:
src/dashboard/pages/<script-name>-settings/ - 使用中的
@wix/app-managementAPI实现配置页面embeddedScripts - 更新以导入并使用上述两个扩展
src/extensions.ts - 运行验证TypeScript编译通过
npx tsc --noEmit - 运行和
npx wix build进行测试npx wix preview - 在Wix开发者中心添加权限(参见启用嵌入式脚本权限)
APPS.MANAGE_EMBEDDED_SCRIPT
Script Types
脚本类型
Embedded scripts must declare a type for consent management:
| Type | Description | Use Cases |
|---|---|---|
| Core functionality crucial to site operation | Authentication, security features |
| Remembers user choices to improve experience | Language preferences, UI customization |
| Provides statistics on how visitors use the site | Google Analytics, Hotjar, Mixpanel |
| Provides visitor data for marketing purposes | Facebook Pixel, Google Ads, retargeting |
Selection rule: If a script falls into multiple types, choose the option closest to the bottom of the list (most restrictive). For example, a script with both Analytics and Advertising aspects should be typed as .
ADVERTISING嵌入式脚本必须声明类型以用于同意管理:
| 类型 | 描述 | 使用场景 |
|---|---|---|
| 对网站运行至关重要的核心功能 | 身份验证、安全功能 |
| 记住用户选择以提升体验的功能 | 语言偏好、UI自定义 |
| 提供访客网站使用情况统计的功能 | Google Analytics、Hotjar、Mixpanel |
| 为营销目的提供访客数据的功能 | Facebook Pixel、Google Ads、重定向广告 |
选择规则: 如果一个脚本属于多种类型,请选择列表最下方的选项(限制最严格的)。例如,同时具备分析和广告属性的脚本应归类为。
ADVERTISINGPlacement Options
嵌入位置选项
| Placement | Description | Best For |
|---|---|---|
| Between | Analytics, early initialization |
| Immediately after opening | Critical functionality, noscript |
| Immediately before closing | Non-blocking scripts, performance |
Selection guidelines:
- Analytics/tracking → (initialize early)
HEAD - Advertising pixels → (non-blocking)
BODY_END - Critical functionality → or
HEADBODY_START - Non-critical features → (better performance)
BODY_END
| 位置 | 描述 | 最佳适用场景 |
|---|---|---|
| 位于 | 分析跟踪、早期初始化 |
| 紧跟在 | 关键功能、noscript代码 |
| 紧跟在 | 非阻塞脚本、性能优化场景 |
选择指南:
- 分析/跟踪脚本 → (尽早初始化)
HEAD - 广告像素 → (非阻塞)
BODY_END - 关键功能 → 或
HEADBODY_START - 非关键功能 → (性能更优)
BODY_END
Dynamic Parameters and Dashboard Configuration
动态参数与仪表板配置
Every embedded script requires a companion dashboard page to configure its parameters. Site owners use the dashboard page UI to set values, which are then passed to the embedded script as template variables.
每个嵌入式脚本都需要一个配套的仪表板页面来配置其参数。网站所有者通过仪表板页面UI设置值,这些值随后会作为模板变量传递给嵌入式脚本。
Architecture Flow
架构流程
Dashboard Page (React UI)
│
│ embeddedScripts.embedScript({ parameters: {...} })
▼
Wix App Management API
│
│ Stores parameters, injects as template variables
▼
Embedded Script (HTML)
│
│ {{parameterKey}} → actual value
▼
Site DOMRelated skill: Use to create the configuration UI for your embedded script.
wix-cli-dashboard-page仪表板页面(React UI)
│
│ embeddedScripts.embedScript({ parameters: {...} })
▼
Wix 应用管理API
│
│ 存储参数,作为模板变量注入
▼
嵌入式脚本(HTML)
│
│ {{parameterKey}} → 实际值
▼
网站DOM相关技能: 使用为你的嵌入式脚本创建配置UI。
wix-cli-dashboard-pageParameter Types
参数类型
| Type | Description | Dashboard Component |
|---|---|---|
| Single-line text | Input |
| Numeric value | Input type="number" |
| True/false toggle | ToggleSwitch, Checkbox |
| Image from media manager | ImagePicker |
| Date only | DatePicker |
| Date with time | DatePicker + TimeInput |
| URL with validation | Input |
| Dropdown options | Dropdown |
| Color value | ColorPicker |
| 类型 | 描述 | 仪表板组件 |
|---|---|---|
| 单行文本 | 输入框 |
| 数值 | 数字输入框(type="number") |
| 真假切换 | 切换开关、复选框 |
| 媒体管理器中的图片 | 图片选择器 |
| 仅日期 | 日期选择器 |
| 日期加时间 | 日期选择器 + 时间输入框 |
| 带验证的URL | 输入框 |
| 下拉选项 | 下拉菜单 |
| 颜色值 | 颜色选择器 |
Template Variable Syntax
模板变量语法
Embedded scripts support parameterization using template variable syntax . These parameters are configured through the dashboard and passed as template variables that should be used in your HTML/JavaScript code.
{{variableName}}Usage Instructions:
-
Template Variable Syntax:
- Use syntax to insert parameter values into your HTML
{{parameterKey}} - Template variables work in HTML attributes
- They will be replaced with actual values when the script is injected
- Use
-
HTML Attributes (REQUIRED):
- Store ALL parameter values in data attributes on a configuration element
- Template variables can ONLY be used here, not directly in JavaScript
- Example:
<div id="config" data-headline="{{headline}}" data-text="{{text}}"></div>
-
JavaScript Access:
- JavaScript must read parameter values from the data attributes
- Use or the
getAttribute()propertydataset - Examples:
javascript
const config = document.getElementById("config"); const headline = config?.getAttribute("data-headline"); // OR using dataset: const { headline, text } = config.dataset;
-
Type Safety:
- Be aware of parameter types when using them in JavaScript
- NUMBER types: convert with or
Number()parseInt() - BOOLEAN types: compare with or
'true'strings'false' - DATE/DATETIME: parse with
new Date()
-
Required vs Optional:
- Required parameters will always have values
- Optional parameters may be empty - handle gracefully
- Provide fallback values for optional parameters
-
Relevant Parameter Usage:
- Only use dynamic parameters that are relevant to your current use case
- Ignore parameters that don't apply to the functionality you're implementing
- Each parameter you use should serve a clear purpose in the script's functionality
- It's perfectly fine to not use all parameters if they're not applicable
Example Patterns:
Pattern 1 - Configuration in Data Attributes:
html
<div
id="script-config"
data-api-key="{{apiKey}}"
data-enabled="{{enabled}}"
data-color="{{primaryColor}}"
></div>
<script>
const config = document.getElementById("script-config");
const apiKey = config.getAttribute("data-api-key");
const enabled = config.getAttribute("data-enabled") === "true";
const color = config.getAttribute("data-color");
if (enabled && apiKey) {
// Initialize with configuration
}
</script>Pattern 2 - Using dataset Property:
html
<div
id="script-config"
data-headline="{{headline}}"
data-message="{{message}}"
data-image-url="{{imageUrl}}"
></div>
<script>
const config = document.getElementById("script-config");
const { headline, message, imageUrl } = config.dataset;
// Use the variables in your script logic
if (headline) {
document.querySelector("#headline").textContent = headline;
}
</script>Pattern 3 - Conditional Logic:
html
<div
id="config"
data-mode="{{activationMode}}"
data-start="{{startDate}}"
data-end="{{endDate}}"
></div>
<script>
const config = document.getElementById("config");
const mode = config.getAttribute("data-mode");
if (mode === "timed") {
const startDate = new Date(config.getAttribute("data-start"));
const endDate = new Date(config.getAttribute("data-end"));
const now = new Date();
if (now >= startDate && now <= endDate) {
// Show content
}
} else if (mode === "active") {
// Show content immediately
}
</script>Validation Requirements:
- Only use dynamic parameters that are relevant to your specific use case
- Ignore parameters that don't apply to the functionality being implemented
- Template variables must match the exact key names from the parameter definitions
{{parameterKey}} - Handle both required and optional parameters appropriately
- Provide sensible default behavior when optional parameters are not set
- Ensure type-appropriate usage (don't use NUMBER parameters as strings without conversion)
嵌入式脚本支持使用的模板变量语法进行参数化。这些参数通过仪表板配置,并作为模板变量传入,应在你的HTML/JavaScript代码中使用。
{{variableName}}使用说明:
-
模板变量语法:
- 使用语法将参数值插入到HTML中
{{parameterKey}} - 模板变量可用于HTML属性
- 脚本注入时,它们会被替换为实际值
- 使用
-
HTML属性(必填):
- 将所有参数值存储在配置元素的data属性中
- 模板变量只能在此处使用,不能直接在JavaScript中使用
- 示例:
<div id="config" data-headline="{{headline}}" data-text="{{text}}"></div>
-
JavaScript访问:
- JavaScript必须从data属性中读取参数值
- 使用或
getAttribute()属性dataset - 示例:
javascript
const config = document.getElementById("config"); const headline = config?.getAttribute("data-headline"); // 或使用dataset: const { headline, text } = config.dataset;
-
类型安全:
- 在JavaScript中使用时要注意参数类型
- 数值类型:使用或
Number()转换parseInt() - 布尔类型:与或
'true'字符串比较'false' - 日期/日期时间:使用解析
new Date()
-
必填与可选参数:
- 必填参数始终有值
- 可选参数可能为空 - 需优雅处理
- 为可选参数提供回退值
-
相关参数使用:
- 仅使用与当前用例相关的动态参数
- 忽略与实现功能无关的参数
- 每个使用的参数都应在脚本功能中发挥明确作用
- 如果某些参数不适用,完全可以不使用
示例模式:
模式1 - 配置存储在Data属性中:
html
<div
id="script-config"
data-api-key="{{apiKey}}"
data-enabled="{{enabled}}"
data-color="{{primaryColor}}"
></div>
<script>
const config = document.getElementById("script-config");
const apiKey = config.getAttribute("data-api-key");
const enabled = config.getAttribute("data-enabled") === "true";
const color = config.getAttribute("data-color");
if (enabled && apiKey) {
// 使用配置初始化
}
</script>模式2 - 使用dataset属性:
html
<div
id="script-config"
data-headline="{{headline}}"
data-message="{{message}}"
data-image-url="{{imageUrl}}"
></div>
<script>
const config = document.getElementById("script-config");
const { headline, message, imageUrl } = config.dataset;
// 在脚本逻辑中使用变量
if (headline) {
document.querySelector("#headline").textContent = headline;
}
</script>模式3 - 条件逻辑:
html
<div
id="config"
data-mode="{{activationMode}}"
data-start="{{startDate}}"
data-end="{{endDate}}"
></div>
<script>
const config = document.getElementById("config");
const mode = config.getAttribute("data-mode");
if (mode === "timed") {
const startDate = new Date(config.getAttribute("data-start"));
const endDate = new Date(config.getAttribute("data-end"));
const now = new Date();
if (now >= startDate && now <= endDate) {
// 显示内容
}
} else if (mode === "active") {
// 立即显示内容
}
</script>验证要求:
- 仅使用与特定用例相关的动态参数
- 忽略与实现功能无关的参数
- 模板变量必须与参数定义中的键名完全匹配
{{parameterKey}} - 正确处理必填和可选参数
- 当可选参数未设置时,提供合理的默认行为
- 确保类型匹配的使用(不要不转换就将数值参数作为字符串使用)
Common Parameters
常用参数
Every embedded script should have at minimum an enable/disable toggle parameter:
| Parameter | Type | Purpose |
|---|---|---|
| | Allow site owner to activate/disable |
| | Third-party service credentials |
| | Analytics/pixel identifiers |
| | Customizable display text |
| | UI customization |
每个嵌入式脚本至少应包含一个启用/禁用切换参数:
| 参数 | 类型 | 用途 |
|---|---|---|
| | 允许网站所有者激活/禁用脚本 |
| | 第三方服务凭证 |
| | 分析/像素标识符 |
| | 可自定义的显示文本 |
| | UI自定义 |
Output Structure
输出结构
A complete embedded script implementation requires two parts:
完整的嵌入式脚本实现需要两部分:
1. Embedded Script Extension
1. 嵌入式脚本扩展
src/site/embedded-scripts/
└── {script-name}/
├── embedded.html # HTML/JavaScript code to inject
└── extensions.ts # Metadata (scriptType, placement)src/site/embedded-scripts/
└── {script-name}/
├── embedded.html # 要注入的HTML/JavaScript代码
└── extensions.ts # 元数据(scriptType、placement)2. Dashboard Configuration Page (Required)
2. 仪表板配置页面(必填)
src/dashboard/
├── withProviders.tsx # WDS provider wrapper (required)
└── pages/
└── {script-name}-settings/
├── extensions.ts # Extension registration (REQUIRED)
└── page.tsx # Configuration UI using embeddedScripts APINote: The dashboard page requires its own file. Without this file, the dashboard page will not appear in the Wix dashboard.
extensions.tsWARNING: The dashboard page uses DIFFERENT field names than embedded scripts:
- Dashboard pages use ,
title,routePathcomponent - Embedded scripts use ,
name,source,placementscriptType
Do NOT apply embedded script field names to dashboard page registrations.
See skill for dashboard page implementation details and the extension registration pattern.
wix-cli-dashboard-pagesrc/dashboard/
├── withProviders.tsx # WDS提供者包装器(必填)
└── pages/
└── {script-name}-settings/
├── extensions.ts # 扩展注册(必填)
└── page.tsx # 使用embeddedScripts API的配置UI注意: 仪表板页面需要自己的文件。没有此文件,仪表板页面将不会出现在Wix仪表板中。
extensions.ts警告: 仪表板页面使用的字段名称与嵌入式脚本不同:
- 仪表板页面使用、
title、routePathcomponent - 嵌入式脚本使用、
name、source、placementscriptType
不要将嵌入式脚本的字段名称应用于仪表板页面注册。
请参阅技能了解仪表板页面实现细节和扩展注册模式。
wix-cli-dashboard-pageImplementation Pattern
实现模式
html
<!-- Configuration element with template variables -->
<div id="my-config" data-api-key="{{apiKey}}" data-enabled="{{enabled}}"></div>
<!-- Container for dynamic content -->
<div id="my-container"></div>
<style>
/* Scoped styles for the embedded content */
#my-container {
/* styles */
}
</style>
<script type="module">
// Get configuration from data attributes
const config = document.getElementById("my-config");
if (!config) throw new Error("Config element not found");
const { apiKey, enabled } = config.dataset;
// Exit early if disabled (use throw at module scope, not return)
if (enabled !== "true") {
throw new Error("Script disabled");
}
// Implement functionality in a named function (return is allowed here)
async function initialize() {
try {
// Your implementation
} catch (error) {
console.error("Script error:", error);
}
}
// Initialize when DOM is ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initialize);
} else {
initialize();
}
</script>html
<!-- 包含模板变量的配置元素 -->
<div id="my-config" data-api-key="{{apiKey}}" data-enabled="{{enabled}}"></div>
<!-- 动态内容容器 -->
<div id="my-container"></div>
<style>
/* 嵌入式内容的作用域样式 */
#my-container {
/* 样式 */
}
</style>
<script type="module">
// 从data属性获取配置
const config = document.getElementById("my-config");
if (!config) throw new Error("未找到配置元素");
const { apiKey, enabled } = config.dataset;
// 如果禁用则提前退出(在模块作用域使用throw,不要用return)
if (enabled !== "true") {
throw new Error("脚本已禁用");
}
// 在命名函数中实现功能(此处允许使用return)
async function initialize() {
try {
// 你的实现代码
} catch (error) {
console.error("脚本错误:", error);
}
}
// DOM准备就绪时初始化
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initialize);
} else {
initialize();
}
</script>Examples
示例
Analytics Tracking
分析跟踪
Request: "Add Google Analytics tracking to my site"
Output:
- Script type:
ANALYTICS - Placement:
HEAD - Template variables:
{{trackingId}} - Implements: gtag.js initialization, page view tracking
需求: "为我的网站添加Google Analytics跟踪"
输出:
- 脚本类型:
ANALYTICS - 嵌入位置:
HEAD - 模板变量:
{{trackingId}} - 实现内容: gtag.js初始化、页面浏览跟踪
Popup/Modal
弹窗/模态框
Request: "Create a coupon popup that shows when cart value exceeds $50"
Output:
- Script type:
FUNCTIONAL - Placement:
BODY_END - Template variables: ,
{{couponCode}},{{minimumCartValue}}{{enablePopup}} - Implements: Cart value detection, popup display logic, localStorage for "don't show again"
需求: "创建一个当购物车金额超过50美元时显示的优惠券弹窗"
输出:
- 脚本类型:
FUNCTIONAL - 嵌入位置:
BODY_END - 模板变量: ,
{{couponCode}},{{minimumCartValue}}{{enablePopup}} - 实现内容: 购物车金额检测、弹窗显示逻辑、localStorage用于"不再显示"
Third-Party Chat Widget
第三方聊天组件
Request: "Integrate Intercom chat widget"
Output:
- Script type:
FUNCTIONAL - Placement:
BODY_END - Template variables: ,
{{appId}},{{userEmail}}{{userName}} - Implements: Intercom SDK initialization, user identification
需求: "集成Intercom聊天组件"
输出:
- 脚本类型:
FUNCTIONAL - 嵌入位置:
BODY_END - 模板变量: ,
{{appId}},{{userEmail}}{{userName}} - 实现内容: Intercom SDK初始化、用户识别
Best Practices
最佳实践
- Always create a dashboard page: Every embedded script needs a configuration UI
- Include enable/disable toggle: Let site owners control activation without removing the script
- Performance: Minimize impact - scripts should be lightweight and non-blocking
- Security: Avoid inline event handlers, validate data, escape user input
- Error handling: Fail silently when appropriate - don't break the site
- Module scope early exits: Use for early exits at module scope, not
throw new Error(). Rollup (used by Astro) doesn't allowreturnstatements at module scope. Wrap main logic in a named async function wherereturnis valid.return - Type conversions: Parameters are always strings - convert in JavaScript as needed
- API calls: Only create fetch() calls to /api/* endpoints that exist in the API spec
- Scoping: Prefix CSS classes and IDs to avoid conflicts with site styles
- Cleanup: Remove event listeners and intervals when appropriate
- 始终创建仪表板页面: 每个嵌入式脚本都需要配置UI
- 包含启用/禁用切换: 让网站所有者无需移除脚本即可控制激活状态
- 性能优化: 最小化影响 - 脚本应轻量且非阻塞
- 安全: 避免内联事件处理器、验证数据、转义用户输入
- 错误处理: 适当时静默失败 - 不要破坏网站
- 模块作用域提前退出: 在模块作用域使用提前退出,不要用
throw new Error()。Rollup(Astro使用)不允许在模块作用域使用return。将主逻辑包装在命名异步函数中,此处允许使用return。return - 类型转换: 参数始终是字符串 - 在JavaScript中按需转换
- API调用: 仅调用API规范中存在的/api/*端点
- 作用域: 为CSS类和ID添加前缀以避免与网站样式冲突
- 清理: 适当时移除事件监听器和定时器
Complete Example: Coupon Popup
完整示例:优惠券弹窗
1. Define Parameters
1. 定义参数
Parameters for "cart-coupon-popup":
- couponCode (TEXT, required) - The coupon code to display
- popupHeadline (TEXT, required) - Headline text
- popupDescription (TEXT, required) - Description text
- minimumCartValue (NUMBER) - Minimum cart value to show popup
- enablePopup (BOOLEAN, required) - Enable/disable toggle"cart-coupon-popup"的参数:
- couponCode(TEXT,必填)- 要显示的优惠券代码
- popupHeadline(TEXT,必填)- 弹窗标题文本
- popupDescription(TEXT,必填)- 弹窗描述文本
- minimumCartValue(NUMBER)- 显示弹窗的最低购物车金额
- enablePopup(BOOLEAN,必填)- 启用/禁用切换2. Embedded Script (embedded.html
)
embedded.html2. 嵌入式脚本(embedded.html
)
embedded.htmlhtml
<div
id="popup-config"
data-coupon-code="{{couponCode}}"
data-popup-headline="{{popupHeadline}}"
data-minimum-cart-value="{{minimumCartValue}}"
data-enable-popup="{{enablePopup}}"
></div>
<div id="popup-container"></div>
<script type="module">
// Get configuration from data attributes
const config = document.getElementById("popup-config");
if (!config) throw new Error("Config element not found");
const { couponCode, popupHeadline, minimumCartValue, enablePopup } =
config.dataset;
// Exit early if disabled (use throw at module scope, not return)
if (enablePopup !== "true") {
throw new Error("Popup disabled");
}
// Main logic in a function (return is allowed here)
async function initializePopup() {
const minValue = Number(minimumCartValue) || 0;
// ... popup implementation
}
// Initialize when DOM is ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initializePopup);
} else {
initializePopup();
}
</script>html
<div
id="popup-config"
data-coupon-code="{{couponCode}}"
data-popup-headline="{{popupHeadline}}"
data-minimum-cart-value="{{minimumCartValue}}"
data-enable-popup="{{enablePopup}}"
></div>
<div id="popup-container"></div>
<script type="module">
// 从data属性获取配置
const config = document.getElementById("popup-config");
if (!config) throw new Error("未找到配置元素");
const { couponCode, popupHeadline, minimumCartValue, enablePopup } =
config.dataset;
// 如果禁用则提前退出(在模块作用域使用throw,不要用return)
if (enablePopup !== "true") {
throw new Error("弹窗已禁用");
}
// 主逻辑在函数中(此处允许使用return)
async function initializePopup() {
const minValue = Number(minimumCartValue) || 0;
// ... 弹窗实现代码
}
// DOM准备就绪时初始化
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initializePopup);
} else {
initializePopup();
}
</script>3. Dashboard Page (See wix-cli-dashboard-page
skill)
wix-cli-dashboard-page3. 仪表板页面(参见wix-cli-dashboard-page
技能)
wix-cli-dashboard-pageUses API from :
embeddedScripts@wix/app-managementtypescript
import { embeddedScripts } from "@wix/app-management";
// Load parameters
const script = await embeddedScripts.getEmbeddedScript();
const params = script.parameters; // { couponCode: "...", ... }
// Save parameters (all values must be strings)
await embeddedScripts.embedScript({
parameters: {
couponCode: "SAVE20",
minimumCartValue: "50", // Number as string
enablePopup: "true", // Boolean as string
},
});使用中的 API:
@wix/app-managementembeddedScriptstypescript
import { embeddedScripts } from "@wix/app-management";
// 加载参数
const script = await embeddedScripts.getEmbeddedScript();
const params = script.parameters; // { couponCode: "...", ... }
// 保存参数(所有值必须是字符串)
await embeddedScripts.embedScript({
parameters: {
couponCode: "SAVE20",
minimumCartValue: "50", // 数值以字符串形式存储
enablePopup: "true", // 布尔值以字符串形式存储
},
});Extension Registration
扩展注册
Extension registration is MANDATORY and has TWO required steps.
扩展注册是强制性的,需要两个步骤。
Step 1: Create Script-Specific Extension File
步骤1:创建脚本专属扩展文件
Each embedded script requires an file in its folder:
extensions.tstypescript
import { extensions } from "@wix/astro/builders";
export const embeddedscriptMyScript = extensions.embeddedScript({
id: "{{GENERATE_UUID}}",
name: "My Script",
source: "./site/embedded-scripts/my-script/embedded.html",
placement: "BODY_END",
scriptType: "FUNCTIONAL",
});CRITICAL: UUID Generation
The must be a unique, static UUID v4 string. Generate a fresh UUID for each extension - do NOT use or copy UUIDs from examples. Replace with a freshly generated UUID like .
idrandomUUID(){{GENERATE_UUID}}"a1b2c3d4-e5f6-7890-abcd-ef1234567890"| Property | Type | Description |
|---|---|---|
| string | Unique static UUID v4 (generate fresh) |
| string | Display name for the script |
| string | Relative path to the HTML file |
| enum | |
| enum | |
每个嵌入式脚本在其文件夹中需要一个文件:
extensions.tstypescript
import { extensions } from "@wix/astro/builders";
export const embeddedscriptMyScript = extensions.embeddedScript({
id: "{{GENERATE_UUID}}",
name: "My Script",
source: "./site/embedded-scripts/my-script/embedded.html",
placement: "BODY_END",
scriptType: "FUNCTIONAL",
});关键:UUID生成
idrandomUUID(){{GENERATE_UUID}}"a1b2c3d4-e5f6-7890-abcd-ef1234567890"| 属性 | 类型 | 描述 |
|---|---|---|
| string | 唯一的静态UUID v4(需新生成) |
| string | 脚本的显示名称 |
| string | HTML文件的相对路径 |
| 枚举 | |
| 枚举 | |
Step 2: Register in Main Extensions File
步骤2:在主扩展文件中注册
CRITICAL: After creating the script-specific extension file, you MUST read wix-cli-extension-registration and follow the "App Registration" section to update .
src/extensions.tsWithout completing Step 2, the embedded script will not be deployed to the site.
关键: 创建脚本专属扩展文件后,必须阅读wix-cli-extension-registration并按照“应用注册”部分的说明更新。
src/extensions.ts如果不完成步骤2,嵌入式脚本将不会部署到网站。
Enable Embedded Script Permission
启用嵌入式脚本权限
After implementation, the app developer must manually enable the embedded script permission:
- Go to https://manage.wix.com/apps/{app-id}/dev-center-permissions (replace with your actual app ID)
{app-id} - Add the permission
APPS.MANAGE_EMBEDDED_SCRIPT - Save the changes
Note: This is a manual step in the Wix Dev Center. Without this permission, embedded scripts will not function on the site.
实现完成后,应用开发者必须手动启用嵌入式脚本权限:
- 访问https://manage.wix.com/apps/{app-id}/dev-center-permissions(将替换为你的实际应用ID)
{app-id} - 添加权限
APPS.MANAGE_EMBEDDED_SCRIPT - 保存更改
注意: 这是Wix开发者中心中的手动步骤。没有此权限,嵌入式脚本将无法在网站上运行。
Verification
验证
After implementation, use wix-cli-app-validation to validate TypeScript compilation, build, preview, and runtime behavior.
实现完成后,使用wix-cli-app-validation验证TypeScript编译、构建、预览和运行时行为。