简体   繁体   English

使用服务人员更新dom

[英]Using service worker to update dom

I have implemented web push notifications and now I'm looking into the possibility to use the service worker to update the DOM if the website is opened in the browser. 我已经实现了Web推送通知,现在我正在研究如果在浏览器中打开了网站,则可以使用Service Worker更新DOM的可能性。 Similar to how social media sites update if you receive a message (or SO for that matter). 类似于社交媒体网站在您收到消息后(或与此相关的消息)进行更新的方式。

Is this something that is possible, or am I barking up the wrong tree? 这是可能的,还是我吠错了树?

I have tried simply telling the service worker to look for a certain element and update its contents, but that didn't work. 我尝试仅告诉服务人员寻找某个元素并更新其内容,但这没有用。

Service worker (web worker in general) doesn't have access to window or document element. 服务工作者(通常是网络工作者)无权访问窗口或文档元素。 What that means, web workers can`t directly manipulate DOM. 这意味着,网络工作者无法直接操作DOM。

However, web workers can send messages to main thread (document javascript), by using Client.postMessage() . 但是,网络工作者可以使用Client.postMessage()将消息发送到主线程(文档javascript So what you need to do, is send message from service worker to document javascript with list of id/class/tagname/whatever of DOM elements that you want to manipulate, and let main thread do the job. 因此,您需要做的是将服务工作者的消息发送到文档javascript,其中包含id / class / tagname /您要操纵的所有DOM元素列表,然后让主线程完成此工作。

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

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