Polyv Help Center

Help Center

**Polyv Video Creation Page Embedding and Communication Protocol**

Updated: 2026-11-13 15:14:39

Document Version: v1.2 Last Updated: November 13, 2026


1. Overview

This document is intended to help developers seamlessly integrate Polyv AI video creation functionality into third-party business platforms in the form of iframe. By following the guidelines in this document, you can achieve:

  • Interface Customization: Control the display and hiding of specific buttons in the embedded page through URL parameters.
  • Function Invocation: Use the Window.postMessage API to establish secure communication between your parent page and the Polyv iframe page, programmatically triggering operations such as video generation and draft saving.

This document is intended for customers who need to provide AI video creation capabilities within their own products and wish to have customized control over the integration interface and workflow.


2. Quick Start

To achieve basic embedding and communication, you need the following two steps:

  1. Embed the iframe: Place the iframe of the Polyv AI video creation page in your page.
  2. Implement the communication script: Write JavaScript code to listen for the iframe's ready state and send commands.

Below is a basic HTML structure example:

<!DOCTYPE html>
<html>
<head>
  <title>集成 Polyv AI 视频创作</title>
</head>
<body>

  <!-- 1. 嵌入 Polyv AI 视频创作编辑页面 -->
  <iframe
    src="https://console.polyv.net/live/index.html#/ai-manager/video-production/create"
    frameborder="0"
    id="polyvAiFrame"
    style="width: 100%; height: 800px;">
  </iframe>

  <!-- 2. 父页面的控制按钮 -->
  <button id="createVideoButton" type="button">从外部触发生成</button>
  <button id="returnButton" type="button">返回</button>

  <!-- 3. 通信脚本 -->
  <script>
    // 详细逻辑见下文
  </script>

</body>
</html>

3. Feature Details

3.1. Interface Customization (URL Parameters)

You can customize the appearance of the embedded page by appending specific parameters to the link in the src attribute of the iframe. This approach is flexible and easy to implement.

Parameter List

Parameter Type Description Example
video-production-submit String N: Hides the [Generate Video] button in the upper right corner of the page. ...create?video-production-submit=N
video-production-return String N: Hides the [Back] button in the upper left corner of the page. ...create?video-production-return=N
video-production-submit-check String Y: Before generating the video, requests the parent page to perform submission validation. ...create?video-production-submit-check=Y

Combined Usage Example

To hide both the "Generate Video" and "Back" buttons and enable pre-generation validation, configure the src as follows:

<iframe
  src="https://console.polyv.net/live/index.html#/ai-manager/video-production/create?video-production-submit=N&video-production-return=N&video-production-submit-check=Y"
  id="polyvAiFrame">
</iframe>

3.2. Interactive Communication (PostMessage API)

To enable precise control of operations within the iframe from the parent page, we have defined a bidirectional communication protocol based on postMessage.

Core Flow:

  1. After the iframe page finishes loading, it sends a ready status notification to the parent page.
  2. After the parent page receives the ready notification, it can confirm that the iframe is ready.
  3. Thereafter, the parent page can send specific commands such as create and save-draft to the iframe.
  4. After pre-generation validation is enabled, the iframe sends submit-check, and the parent page should return the validation result.
  5. After the iframe successfully submits the generation task, it sends a submit-success event notification to the parent page.
  6. When the integrator hides the iframe's built-in [Back] button and uses the parent page's own [Back] button, the parent page can first send save-draft to the iframe, wait for the iframe to return the draft save result, and then execute its own back logic.

JavaScript Communication Logic Implementation

const $iframe = document.getElementById('polyvAiFrame');
const $createVideoButton = document.getElementById('createVideoButton');
const $returnButton = document.getElementById('returnButton');
const targetOrigin = 'https://console.polyv.net';
let isFrameReady = false;

$createVideoButton.disabled = true;
$returnButton.disabled = true;

// 生成前校验示例,请替换为接入方自己的额度、权限或其他业务校验逻辑
async function validateBeforeCreate({ estimatedDuration, userId, draftId }) {
  console.log('生成前校验参数:', { estimatedDuration, userId, draftId });
  return {
    success: true,
    message: ''
  };
}

// 监听来自 iframe 的消息
window.addEventListener('message', async (event) => {
  // 安全校验:确保消息来自指定的源
  if (event.origin !== targetOrigin) {
    return;
  }

  try {
    const data = JSON.parse(event.data);

    // 只处理 Polyv AI 视频创作协议消息
    if (data.type !== 'ai-video-production') {
      return;
    }

    // ready:iframe 已完成初始化,可以接收父页面指令
    if (data.type === 'ai-video-production' && data.event === 'ready') {
      isFrameReady = true;
      console.log('Polyv AI iframe 已准备就绪。');
      // 可在此处启用相关按钮
      $createVideoButton.disabled = false;
      $returnButton.disabled = false;
      return;
    }

    // submit-check:iframe 请求父页面执行生成前校验
    if (data.type === 'ai-video-production' && data.event === 'submit-check') {
      const { estimatedDuration, userId, id, requestId } = data;

      // TODO: 接入方根据预计时长、用户 ID、草稿 ID 等信息执行自己的校验逻辑
      const checkResult = await validateBeforeCreate({
        estimatedDuration,
        userId,
        draftId: id
      });

      // 必须原样返回本次请求的 requestId
      const message = {
        type: 'ai-video-production',
        event: 'submit-check',
        requestId,
        success: checkResult.success,
        message: checkResult.message
      };

      $iframe.contentWindow.postMessage(
        JSON.stringify(message),
        targetOrigin
      );
      return;
    }

    // save-draft:iframe 返回草稿保存结果
    if (data.type === 'ai-video-production' && data.event === 'save-draft') {
      if (data.success) {
        console.log('草稿保存成功,可以执行父页面返回逻辑。');
        // TODO: 在这里执行接入方系统自己的返回逻辑
        // window.history.back();
      } else {
        console.warn('草稿保存失败:', data.message || 'Fail reason');
        // TODO: 在这里提示用户,父页面不应自动返回
      }
    }

    // submit-success:视频生成任务已提交到后台处理队列
    if (data.type === 'ai-video-production' && data.event === 'submit-success') {
      console.log('视频生成任务已提交。');
    }
  } catch (error) {
    console.warn('无法解析来自 iframe 的消息:', error);
  }
});

// 为父页面的按钮绑定点击事件
$createVideoButton.addEventListener('click', () => {
  if (!isFrameReady) {
    alert('视频创作页面尚未准备好,请稍候...');
    return;
  }

  // create:请求 iframe 启动视频生成流程
  const message = {
    type: 'ai-video-production',
    event: 'create'
  };

  // 向 iframe 发送“生成视频”指令
  $iframe.contentWindow.postMessage(JSON.stringify(message), targetOrigin);
});

$returnButton.addEventListener('click', () => {
  if (!isFrameReady) {
    alert('视频创作页面尚未准备好,请稍候...');
    return;
  }

  // save-draft:请求 iframe 保存当前编辑内容的草稿
  const message = {
    type: 'ai-video-production',
    event: 'save-draft'
  };

  // 向 iframe 发送“保存草稿”指令,等待 iframe 返回保存结果后再执行父页面返回逻辑
  $iframe.contentWindow.postMessage(JSON.stringify(message), targetOrigin);
});

4. Communication Protocol Specification

All communication messages are in JSON string format, and type is fixed to ai-video-production. In the draft saving protocol, both the parent page's request and the iframe's result response use the save-draft event; the parent page's request does not carry success, while the iframe's result response carries success and message.

4.1. Iframe → Parent Page

event Value Description Message Body (Payload) Example
ready Notifies the parent page that the iframe has finished loading and can receive commands. This is a critical "handshake" signal. {"type": "ai-video-production", "event": "ready"}
submit-success Notifies the parent page after the video generation task has been successfully submitted to the backend processing queue. Indicates that the user's request has been accepted. {"type": "ai-video-production", "event": "submit-success"}
save-draft Notifies the parent page that the draft save result has been returned, and the parent page can determine whether to proceed with its own back logic based on success. `{"type": "ai-video-production", "event": "save-draft", "success": true
submit-check After pre-generation validation is enabled, requests the parent page to determine whether generation is allowed. estimatedDuration is the estimated duration rounded up, in seconds. {"type": "ai-video-production", "event": "submit-check", "estimatedDuration": 120, "userId": "用户ID", "id": "草稿ID", "requestId": "临时请求ID"}

4.2. Parent Page → Iframe

event Value Description Message Body (Payload) Example
create Requests the iframe to start the video generation process. Its effect is equivalent to clicking the [Generate Video] button within the iframe. {"type": "ai-video-production", "event": "create"}
save-draft Requests the iframe to save the current editing content as a draft. This action is typically triggered by the integrator's own [Back] button. {"type": "ai-video-production", "event": "save-draft"}
submit-check Returns the pre-generation validation result. requestId must match the value in the iframe's request; when rejecting, a prompt message can be returned via message. {"type": "ai-video-production", "event": "submit-check", "requestId": "临时请求ID", "success": true, "message": ""}

4.3. Pre-Generation Validation Flow

When the integrator needs to validate quota, permissions, or other business conditions before generating a video, video-production-submit-check=Y should be added to the iframe link, and the following flow should be followed:

The submit-check message sent by the iframe contains the following fields:

Field Type Description
estimatedDuration Number The estimated duration of the current video, in seconds, rounded up.
userId String The current user ID.
id String The current draft ID, i.e., draftId.
requestId String A temporary unique ID for this validation, which the parent page must return as-is when replying.
  1. The user clicks the [Generate Video] button within the iframe, or the parent page sends create to the iframe.
  2. The iframe sends submit-check to the parent page, carrying the estimated duration, user ID, draft ID, and temporary request ID. After sending, the iframe displays a loading state and waits for the result.
  3. After completing validation, the parent page sends submit-check and returns requestId as-is:
    • success: true: Validation passed; the iframe continues the generation process.
    • success: false: Validation failed; the iframe stops generation. The parent page can return a prompt message via message; if not returned, the iframe displays "Unable to generate at this time, please try again later."
  4. The iframe only processes results that match the current request's requestId. If no matching result is received within 10 seconds, the iframe stops generation and displays "Validation timed out, please try again."

When video-production-submit-check=Y is not configured, this validation is not initiated, and the original generation flow is maintained.

4.4. Draft Saving and External Back Flow

When the integrator hides the [Back] button in the upper left corner of the iframe page via video-production-return=N and uses the parent page's own [Back] button, the following flow should be followed:

  1. The user clicks the integrator system's own [Back] button.
  2. After confirming that the iframe's ready event has been received, the parent page sends save-draft to the iframe.
  3. The iframe saves the current editing content as a draft.
  4. The iframe calls back save-draft to the parent page and expresses the save result via success.
  5. When success is true, the parent page can proceed with its own back logic.
  6. When success is false, the parent page should not automatically navigate back; it is recommended to prompt the user or log the event based on message.

If the current page has no new unsaved changes, the iframe should still call back save-draft and return success: true. The parent page does not need to additionally determine whether there are unsaved changes.


5. Important Notes

  1. Security Policy: In addEventListener, strictly validate event.origin to ensure the message source is https://console.polyv.net. When postMessage, also explicitly specify targetOrigin and avoid using * to prevent sensitive data leakage.
  2. Loading Sequence: The parent page must wait for and confirm receipt of the iframe's ready event before sending business commands (such as create and save-draft). It is recommended to set the parent page's control buttons to a disabled state before receiving the ready signal.
  3. Message Format: All communication data must be serialized into strings via JSON.stringify() for transmission and parsed at the receiving end via JSON.parse().
  4. External Back: Before receiving the save-draft callback, the parent page should not directly close, navigate away from, or destroy the current iframe page. Typical scenarios for save failure include but are not limited to network or request timeouts, expired login state, server-side exceptions, and current page data not meeting save conditions.
  5. Permission Rules: No additional permission checks are added in this release; the existing draft saving permissions and login state rules of the video creation page are retained.
  6. Pre-Generation Validation: After enabling video-production-submit-check=Y, the parent page should return a submit-check result matching the current requestId within 10 seconds; otherwise, the iframe will treat it as a timeout and stop this generation.

If you have any questions or need further technical support, please contact our technical support team.

联系客服,在线咨询
在线咨询