[英]Share local blob data between two client-only JS applications deployed on different domains
I have two client-only (backend is static file server) applications written in JS.我有两个用 JS 编写的仅限客户端(后端是静态文件服务器)的应用程序。 One is a recorder that records videos (eg the webcam) via the
MediaRecorder
API (this results in a Blob
object).一个是通过
MediaRecorder
API 录制视频(例如网络摄像头)的MediaRecorder
(这会产生一个Blob
对象)。 The other application is a video editor (don't mind how that one is implemented).另一个应用程序是视频编辑器(不要介意它是如何实现的)。
The two applications are deployed on different domains (eg foo-recorder.com
and foo-editor.com
).这两个应用程序部署在不同的域(例如
foo-recorder.com
和foo-editor.com
)。 My goal is to make editing your recordings as easy as possible.我的目标是尽可能轻松地编辑您的录音。 I would like the recorder to have a button "edit recording in editor" that opens the editor with the recording already loaded.
我希望录音机有一个按钮“在编辑器中编辑录音”,可以打开已经加载录音的编辑器。
A naive solution would be to URL.createObjectUrl(blob)
in the recorder, URL-encode that URL and pass it as query parameter to the editor.一个简单的解决方案是在记录器中使用
URL.createObjectUrl(blob)
,对该 URL 进行 URL 编码并将其作为查询参数传递给编辑器。 Eg https://foo-editor.com?video=blob://...
.例如
https://foo-editor.com?video=blob://...
。 However, as far as I can tell, browsers do not allow sites to access blob URLs from another domain.但是,据我所知,浏览器不允许站点访问来自另一个域的 blob URL。 (That's probably for good privacy reasons.)
(这可能是出于很好的隐私原因。)
I also thought about:我也想过:
So I can't come up with a working solution.所以我想不出一个可行的解决方案。 And I wouldn't be surprised if it doesn't work at all because it's probably very tricky privacy and security wise.
如果它根本不起作用,我也不会感到惊讶,因为它在隐私和安全方面可能非常棘手。
Does anyone have an idea how I could pass this blob from one app to the other?有谁知道如何将这个 blob 从一个应用程序传递到另一个应用程序? Obviously, the blob should not be uploaded.
显然,不应上传 blob。
Thanks to Musa's hint, I found a solution: postMessage
allows me to pass the Blob
to the editor, even if it's deployed on another domain.感谢 Musa 的提示,我找到了一个解决方案:
postMessage
允许我将Blob
传递给编辑器,即使它部署在另一个域上。 The recorder has to open a new browsing contect with the editor loaded.记录器必须在加载了编辑器的情况下打开一个新的浏览内容。 This can be:
这可以是:
windows.open
to open a popup window windows.open
打开一个弹出窗口<iframe>
with the editor as src
.src
的<iframe>
。 Acquire the context via iframeDomNode.contentWindow
.iframeDomNode.contentWindow
获取上下文。 It's now possible to use postMessage
to send a message to that context:现在可以使用
postMessage
向该上下文发送消息:
const iframe = document.getElementById('editor-iframe');
iframe.contentWindow.postMessage(videoBlob, "*");
// ^ TODO: change "*" to the specific target origin in production code!!!
On the editor side, I can receive that message via:在编辑器方面,我可以通过以下方式接收该消息:
window.addEventListener("message", event => {
// TODO: in real code, you should check `event.origin` here!
const v = document.getElementById("a-video-element");
v.src = URL.createObjectURL(event.data);
}, false);
Note: if you generate the blob URL on the recorder side, pass that to the editor, the editor still cannot access that.注意:如果您在记录器端生成 blob URL,将其传递给编辑器,编辑器仍然无法访问它。 So you need to pass the blob directly.
所以你需要直接传递blob。
Also note that sending a Blob
via postMessage
is not expensive and does not copy the Blob
data .另请注意,通过
postMessage
发送Blob
并不昂贵,并且不会复制Blob
数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.