简体   繁体   English

在部署在不同域上的两个纯客户端 JS 应用程序之间共享本地 blob 数据

[英]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.comfoo-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:我也想过:

  • Storing the blob as a file on the user's device and pass the path to the other site.将 blob 作为文件存储在用户设备上,并将路径传递到其他站点。 But we can't just willy nilly write files with JS.但是我们不能随便用 JS 写文件。
  • Storing the blob in local storage: local storage usually is limited to a few MB and can't be access cross-domain either.将 blob 存储在本地存储中:本地存储通常限制为几 MB,也不能跨域访问。
  • Passing the whole video base64 encoded as query parameter: that's ridiculous.传递整个视频 base64 编码为查询参数:这太荒谬了。 No.不。

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打开一个弹出窗口
  • An <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.

相关问题 在不同域之间共享数据 - share data between different domains 如何防止在同一容器中部署的两个或多个不同应用程序之间传递HTML5本地存储数据? - How to prevent passing HTML5 local storage data between two or more different applications which were deployed at the same container? 不同域上的两个Web应用程序之间的Javascript通信 - Javascript communication between two web applications on different domains 在不同域之间共享令牌 - Share token between different domains 如何在两个以上的React应用程序之间共享数据? - How to share data between more than two React applications? 仅限客户端的 Nuxt 3 Vue 插件 - Client-only Nuxt 3 Vue plugin 在两个不同的模块和两个不同的js文件之间共享服务 - Share Service Between Two Different Modules and two different js files 在(角度 6)中的不同域之间共享 LocalStorage/SessionStorage - Share LocalStorage/SessionStorage between different domains in (angular 6) 如何在angularjs中的两个应用程序之间共享数组 - How to share an array between two applications in angularjs 客户端和服务器在不同域上的安全连接 - Secure connection between client and server on different domains
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM