简体   繁体   English

在网络应用中注册在线服务工作者

[英]Register inline service worker in web app

I'm toying with adding web push notifications to a web app hosted with apps script. 我想将Web推送通知添加到由应用脚本托管的Web应用中。 To do so, I need to register a service worker and I ran into an error with a cross-domain request. 为此,我需要注册服务工作者,并且遇到跨域请求错误。 Raw content is hosted from *.googleusercontent.com while the script URL is script.google.com/* . 原始内容由*.googleusercontent.com托管,而脚本URL为script.google.com/*

I was able to successfully create an inline worker using this post on creating inline URLs created from a blob with the script. 我可以使用本文通过使用脚本创建从blob创建的内联URL来成功创建内联工作器。 Now I'm stuck at the point of registering the worker in the browser. 现在,我陷入了在浏览器中注册工作人员的问题。

The following model works: 以下模型适用:

HTML 的HTML

<!-- Display the worker status -->
<div id="log"></log>

Service Worker 服务人员

<script id="worker" type="javascript/worker">
  self.onmessage = function(e) {
    self.postMessage('msg from worker');
  };
  console.log('[Service Worker] Process started');

  })
</script> 

Inline install 内联安装

<script>
  function log(msg) {
    var fragment = document.createDocumentFragment();
    fragment.appendChild(document.createTextNode(msg));
    fragment.appendChild(document.createElement('br'));

    document.querySelector("#log").appendChild(fragment);
  }

  // Create the object with the script
  var blob = new Blob([ document.querySelector("#worker").textContent ]);

  // assign a absolute URL to the obejct for hosting
  var worker = new Worker(window.URL.createObjectURL(blob));
  worker.onmessage = function(e) {
    log("Received: " + e.data);
  }

  worker.postMessage('start');
  // navigator.serviceWorker.register(worker) this line fails
</script>

navigator.serviceWorker.register(worker); returns a 400: Bad HTTP response code error. 返回400: Bad HTTP response code错误。

Can inline workers be installed? 可以安装内联工人吗? Or do all installed workers have to come from external scripts? 还是所有已安装的工作程序都必须来自外部脚本?

It's worth noting that Web Workers and Service Workers, which are used with push notifications, are different (see this SO response to read about the difference ). 值得注意的是,与推送通知一起使用的Web Worker和Service Worker是不同的(请参阅此SO响应以了解不同之处 )。

According to MDN Service Workers require a script URL . 根据MDN,服务人员需要脚本URL In Google Apps Script you could serve a service worker file with something like: 在Google Apps脚本中,您可以使用以下内容提供服务工作者文件:

function doGet(e) {
  var file = e.parameter.file;
  if (file == 'service-worker.js'){
    return ContentService.createTextOutput(HtmlService.createHtmlOutputFromFile('service-worker.js').getContent())
                         .setMimeType(ContentService.MimeType.JAVASCRIPT);
  } else {
    return HtmlService.createHtmlOutputFromFile('index');
  }
}

But as in this example ( source code here ) because of the way web apps are served you'll encounter the error: 但是如在此示例此处源代码 )所示,由于Web应用程序的服务方式,您将遇到错误:

SecurityError: Failed to register a ServiceWorker: The script resource is behind a redirect, which is disallowed. SecurityError:无法注册ServiceWorker:脚本资源位于重定向之后,这是不允许的。

There was a proposal for Foreign Fetch for Service Workers but it's still in draft. 对于服务人员,有一个关于外国获取建议,但它仍在草案中。

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

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