mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-29 11:11:20 -08:00
update iframe.helper.js
This commit is contained in:
1 parent
d055eb3e60
commit
96781698ba
2 files changed
+66
-12
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
class IframeHelper {
|
class IframeHelper {
|
||||||
static postMessage(type, data, id = 0) {
|
static postMessage({ type, data, id = 0 }) {
|
||||||
window.parent.postMessage(
|
window.parent.postMessage(
|
||||||
{
|
{
|
||||||
type: type,
|
type: type,
|
||||||
@@ -18,12 +18,17 @@ class IframeHelper {
|
|||||||
if (data.match(/resolve\((.*?)\)/)) {
|
if (data.match(/resolve\((.*?)\)/)) {
|
||||||
const string = `return new Promise(function(resolve){\n${data}\n})`;
|
const string = `return new Promise(function(resolve){\n${data}\n})`;
|
||||||
const result = await new Function(string)();
|
const result = await new Function(string)();
|
||||||
IframeHelper.postMessage('response', result, id);
|
IframeHelper.postMessage({ type: 'response', data: result, id });
|
||||||
} else {
|
} else {
|
||||||
IframeHelper.postMessage('response', new Function(data)(), id);
|
IframeHelper.postMessage({ type: 'response', data: new Function(data)(), id });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
IframeHelper.postMessage('error', error.message, id);
|
IframeHelper.postMessage({ type: 'error', data: error.message, id });
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'destroy':
|
||||||
|
if (!IframeHelper.isDestroy) {
|
||||||
|
IframeHelper.destroy();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -32,11 +37,16 @@ class IframeHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inject() {
|
static inject() {
|
||||||
IframeHelper.postMessage('inject');
|
IframeHelper.isInject = true;
|
||||||
|
IframeHelper.isDestroy = false;
|
||||||
|
IframeHelper.postMessage({ type: 'inject' });
|
||||||
window.addEventListener('message', IframeHelper.onMessage);
|
window.addEventListener('message', IframeHelper.onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
static destroy() {
|
static destroy() {
|
||||||
|
IframeHelper.isInject = false;
|
||||||
|
IframeHelper.isDestroy = true;
|
||||||
|
IframeHelper.postMessage({ type: 'destroy' });
|
||||||
window.removeEventListener('message', IframeHelper.onMessage);
|
window.removeEventListener('message', IframeHelper.onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,6 +55,7 @@ class IframeHelper {
|
|||||||
this.isInject = false;
|
this.isInject = false;
|
||||||
this.isDestroy = false;
|
this.isDestroy = false;
|
||||||
this.$iframe = iframe;
|
this.$iframe = iframe;
|
||||||
|
this.messageCallback = null;
|
||||||
this.onMessage = this.onMessage.bind(this);
|
this.onMessage = this.onMessage.bind(this);
|
||||||
window.addEventListener('message', this.onMessage);
|
window.addEventListener('message', this.onMessage);
|
||||||
this.$iframe.src = url;
|
this.$iframe.src = url;
|
||||||
@@ -59,6 +70,9 @@ class IframeHelper {
|
|||||||
break;
|
break;
|
||||||
case 'destroy':
|
case 'destroy':
|
||||||
this.isInject = false;
|
this.isInject = false;
|
||||||
|
if (!this.isDestroy) {
|
||||||
|
this.destroy();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -70,12 +84,15 @@ class IframeHelper {
|
|||||||
} else {
|
} else {
|
||||||
this.promises[id].resove(data);
|
this.promises[id].resove(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete this.promises[id];
|
delete this.promises[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.messageCallback) {
|
||||||
|
this.messageCallback({ type, data });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
postMessage(type, data) {
|
postMessage({ type, data }) {
|
||||||
return new Promise((resove, reject) => {
|
return new Promise((resove, reject) => {
|
||||||
(function loop() {
|
(function loop() {
|
||||||
if (this.isInject) {
|
if (this.isInject) {
|
||||||
@@ -100,10 +117,12 @@ class IframeHelper {
|
|||||||
|
|
||||||
commit(callback) {
|
commit(callback) {
|
||||||
const callbackString = callback.toString();
|
const callbackString = callback.toString();
|
||||||
const bodyString = callbackString
|
const bodyString = callbackString.substring(callbackString.indexOf('{') + 1, callbackString.lastIndexOf('}'));
|
||||||
.substring(callbackString.indexOf('{') + 1, callbackString.lastIndexOf('}'))
|
return this.postMessage({ type: 'commit', data: bodyString });
|
||||||
.trim();
|
}
|
||||||
return this.postMessage('commit', bodyString);
|
|
||||||
|
message(callback) {
|
||||||
|
this.messageCallback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
|||||||
@@ -24,6 +24,17 @@
|
|||||||
padding: 50px;
|
padding: 50px;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.fullscreenWeb {
|
||||||
|
position: fixed;
|
||||||
|
z-index: 9999;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
left: 0;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
@@ -34,17 +45,41 @@
|
|||||||
<script src="./iframe.helper.js"></script>
|
<script src="./iframe.helper.js"></script>
|
||||||
<script>
|
<script>
|
||||||
(async function () {
|
(async function () {
|
||||||
|
const $iframe = document.getElementById('iframe');
|
||||||
|
|
||||||
// Init iframe helper
|
// Init iframe helper
|
||||||
const helper = new IframeHelper({
|
const helper = new IframeHelper({
|
||||||
iframe: document.getElementById('iframe'),
|
iframe: $iframe,
|
||||||
url: './inner.html',
|
url: './inner.html',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Listen messages from the iframe
|
||||||
|
helper.message(({ type, data }) => {
|
||||||
|
switch (type) {
|
||||||
|
case 'fullscreenWeb':
|
||||||
|
if (data) {
|
||||||
|
$iframe.classList.add('fullscreenWeb');
|
||||||
|
} else {
|
||||||
|
$iframe.classList.remove('fullscreenWeb');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Instantiate the player and mount the instance in the window
|
// Instantiate the player and mount the instance in the window
|
||||||
await helper.commit(() => {
|
await helper.commit(() => {
|
||||||
window.art = new Artplayer({
|
window.art = new Artplayer({
|
||||||
container: '.artplayer-app',
|
container: '.artplayer-app',
|
||||||
url: '../assets/sample/video.mp4',
|
url: '../assets/sample/video.mp4',
|
||||||
|
fullscreen: true,
|
||||||
|
fullscreenWeb: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send message to parent window
|
||||||
|
window.art.on('fullscreenWeb', state => {
|
||||||
|
IframeHelper.postMessage({ type: 'fullscreenWeb', data: state })
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user