From 366867f1f379b5ee093215721bec9ddd11c2b246 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Thu, 4 Aug 2022 02:42:23 +0200 Subject: [PATCH] fix: wait for media and cross-origin error --- PipCanvas.mjs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/PipCanvas.mjs b/PipCanvas.mjs index 5a1138a..98c43e2 100644 --- a/PipCanvas.mjs +++ b/PipCanvas.mjs @@ -15,6 +15,17 @@ export class PipCanvas { // Create from image HTMLImageElement() { + const img = new URL(this.source.src); + const isDataUrl = new RegExp("^\s*data:"); + + // Can not open cross-origin images + if (!isDataUrl.test(this.source.src) && (img.origin !== window.location.origin)) { + const error = "PipCanvas: This cross-origin image can not be opened in PIP"; + + alert(error); + throw new Error(error); + } + this.ctx.width = this.ctx.canvas.width = this.source.width; this.ctx.height = this.ctx.canvas.height = this.source.height; @@ -23,7 +34,9 @@ export class PipCanvas { // Open PIP open() { - this.video.play(); - this.video.requestPictureInPicture(); + this.video.addEventListener("canplaythrough",() => { + this.video.play(); + this.video.requestPictureInPicture(); + }); } } \ No newline at end of file