Chrome 通知 - 即使网站未打开也能自动推送
Chrome Notifications - Magic Push even when the website is not open
我过去构建了一个 chrome 扩展,我在该扩展中使用了 chrome 通知 API,所以我熟悉它的工作原理。
我最近发现这个网站会在发生某些事情时显示此通知,但最奇怪的是,即使该网站未在任何选项卡中打开,它也会推送这些通知(只要 chrome 处于打开状态)。我想知道他们是怎么做到的。
我检查了我的设置 -> 扩展列表,看看我是否以某种方式安装了他们的扩展,但有 none。那么他们运行这个魔法javascript来自哪里呢?
根据 official documentation 和@Andreas 所述
The reason for this is that when a push message is received, the
browser can start up a service worker, which runs in the background
without a page being open, and dispatch an event so that you can
decide how to handle that push message.
您要的javascript代码在worker里面。
如果您想查看浏览器中实际使用的工作人员,只需转到新选项卡中的 chrome://inspect/#service-workers
。点击worker名称下的inspect link可以看到worker代码。
我过去构建了一个 chrome 扩展,我在该扩展中使用了 chrome 通知 API,所以我熟悉它的工作原理。
我最近发现这个网站会在发生某些事情时显示此通知,但最奇怪的是,即使该网站未在任何选项卡中打开,它也会推送这些通知(只要 chrome 处于打开状态)。我想知道他们是怎么做到的。
我检查了我的设置 -> 扩展列表,看看我是否以某种方式安装了他们的扩展,但有 none。那么他们运行这个魔法javascript来自哪里呢?
根据 official documentation 和@Andreas 所述
The reason for this is that when a push message is received, the browser can start up a service worker, which runs in the background without a page being open, and dispatch an event so that you can decide how to handle that push message.
您要的javascript代码在worker里面。
如果您想查看浏览器中实际使用的工作人员,只需转到新选项卡中的 chrome://inspect/#service-workers
。点击worker名称下的inspect link可以看到worker代码。