简体   繁体   English

从国外创建服务工人

[英]Create a service worker from a foreign origin

I'm building a web push notification system that can be integrated on any website. 我正在构建一个可以在任何网站上集成的Web推送通知系统。 I control these sites, but I'm trying to make the integration as simple as possible so it can be done by someone other than me. 我控制着这些站点,但是我正在尝试使集成尽可能简单,以便可以由我以外的其他人来完成。 Ideally, I could just give someone a single JavaScript or iframe tag to drop in and it would just work. 理想情况下,我可以给某人一个JavaScript或iframe标签,这样就可以了。

<script src="https://foreign.com/main.js" />

-- or -- - 要么 -

<iframe src="https://foreign.com/main.html" />

I first tried to create a service worker from a foreign script - that threw an exception and I've since read up on why. 我首先尝试使用外来脚本创建服务工作者-这引发了异常,此后我继续阅读原因。 I understand why this doesn't work. 我了解为什么这行不通。

  navigator.serviceWorker.register('https://foreign.com/sw.js')

So I tried creating a very simple page that just runs the JavaScript to setup the service worker, and calling that from an iframe. 因此,我尝试创建一个非常简单的页面,该页面仅运行JavaScript来设置服务工作者,然后从iframe调用该页面。 This does appear to work - it asks for permission and creates the subscription. 这看起来确实可行-它请求许可并创建订阅。 But if I try to send a message to that subscription, nothing happens. 但是,如果我尝试向该订阅发送消息,则不会发生任何事情。 It just silently fails. 它只是默默地失败了。 Why is this? 为什么是这样?

That cannot be done, it'll be a security issue, not in the Specs. 那是不可能做到的,这将是一个安全问题,而不是在规范中。 SW has to be downloaded from same origin, same path and https 必须从相同的来源,相同的路径和https下载软件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM