import{_ as s,c as a,o as n,a as l}from"./app.fcc4273a.js";const C=JSON.parse('{"title":"Iframe 控制","description":"","frontmatter":{},"headers":[{"level":2,"title":"说明","slug":"说明","link":"#说明","children":[]},{"level":2,"title":"演示","slug":"演示","link":"#演示","children":[]},{"level":2,"title":"安装","slug":"安装","link":"#安装","children":[]},{"level":2,"title":"CDN","slug":"cdn","link":"#cdn","children":[]},{"level":2,"title":"使用","slug":"使用","link":"#使用","children":[]},{"level":2,"title":"实例接口","slug":"实例接口","link":"#实例接口","children":[{"level":3,"title":"commit","slug":"commit","link":"#commit","children":[]},{"level":3,"title":"message","slug":"message","link":"#message","children":[]},{"level":3,"title":"destroy","slug":"destroy","link":"#destroy","children":[]}]},{"level":2,"title":"Iframe 接口","slug":"iframe-接口","link":"#iframe-接口","children":[{"level":3,"title":"inject","slug":"inject","link":"#inject","children":[]},{"level":3,"title":"postMessage","slug":"postmessage","link":"#postmessage","children":[]}]}],"relativePath":"plugin/iframe.md","lastUpdated":1673441101000}'),p={name:"plugin/iframe.md"},e=l(`
通过该插件,你可以轻松在 index.html 里控制跨域 iframe.html 页面里的播放器,如在 index.html 里通过代码控制 iframe.html 播放器的功能,或者获取 iframe.html 播放器的值
👉 查看完整演示
npm install artplayer-plugin-iframe
yarn add artplayer-plugin-iframe
pnpm add artplayer-plugin-iframe
<script src="path/to/artplayer-plugin-iframe.js"></script>
CDN #https://cdn.jsdelivr.net/npm/artplayer-plugin-iframe/dist/artplayer-plugin-iframe.js
https://unpkg.com/artplayer-plugin-iframe/dist/artplayer-plugin-iframe.js
<!DOCTYPE html>
<html>
<head>
<title>ArtPlayer</title>
<meta charset="UTF-8" />
</head>
<body>
<iframe id="iframe"></iframe>
<script src="path/to/artplayer-plugin-iframe.js"></script>
<script>
const iframe = new ArtplayerPluginIframe({
// Iframe element
iframe: document.querySelector('#iframe'),
// Iframe url
url: 'path/to/iframe.html',
});
// Send message to iframe
iframe.commit(() => {
var art = new Artplayer({
container: '.artplayer-app',
url: 'path/to/video.mp4',
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>ArtPlayer</title>
<meta charset="UTF-8" />
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div class="artplayer-app" style="width: 100%; height: 100%;"></div>
<script src="path/to/artplayer.js"></script>
<script src="path/to/artplayer-plugin-iframe.js"></script>
<script>
// Inject scripts to receive messages from instances
ArtplayerPluginIframe.inject();
</script>
</body>
</html>
commit #将消息推送到 iframe,该函数将在 iframe 内部运行
iframe.commit(() => {
var art = new Artplayer({
container: '.artplayer-app',
url: 'path/to/video.mp4',
});
});
iframe.commit(() => {
art.seek = 5;
});
// Get the value from the iframe
(async function () {
// Use the return keyword
var currentTime = await iframe.commit(() => {
return art.currentTime;
});
// or use the resolve method
var currentTime2 = await iframe.commit((resolve) => {
setTimeout(() => {
resolve(art.currentTime);
}, 1000);
});
})();
message #接收来自 iframe 的消息
iframe.message((event) => {
console.info(event);
});
destroy #销毁实例,销毁后无法与 iframe 通信
iframe.destroy();
Iframe 接口 #提示
Iframe 接口 只能运行在 iframe 里
inject #注入脚本,接收来自实例的消息
ArtplayerPluginIframe.inject();
postMessage #将消息推送到实例
iframe.message((event) => {
console.info(event);
});
iframe.commit(() => {
ArtplayerPluginIframe.postMessage({
type: 'currentTime',
data: art.currentTime,
});
});