Chrome扩展程序中的永久服务工作者 [英] Persistent Service Worker in Chrome Extension

查看:96
本文介绍了Chrome扩展程序中的永久服务工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的Chrome扩展程序中将Service Worker定义为持久性的,因为我正在使用webRequest API来拦截针对特定请求以表单形式传递的一些数据,但是我不知道该怎么做.我已经尽力了,但是我的服务人员不断卸载.

如何保持它的加载状态并等待直到请求被拦截?

解决方案

在您的情况下,它可能是ManifestV3中的错误, https://crbug.com/1024211 :该工作程序不会因webRequest事件而醒来.因此,只需使用ManifestV2,直到此问题解决为止,因为没有诸如持久性服务工作程序之类的东西.

如果您仍要阻止后台服务工作程序卸载,则必须打开消息端口从任何选项卡的内容脚本或扩展程序的另一页面(如弹出窗口)中页.该端口将保留五分钟(这是ManifestV3中的错误/功能),因此您必须使用该端口的onDisconnect事件再次与某个随机选项卡重新连接.这种解决方法的缺点是,只有网页才能运行内容脚本,因此,如果浏览器没有任何包含网页的标签,那么您将很不走运.另一个缺点是需要在所有URL上使用内容脚本,这是广泛的主机许可,可将大多数扩展放入网络商店的慢速审核队列中.Chromium团队继续忽略既定事实,有时有时需要在任意持续时间内拥有持久的后台脚本,但我们希望他们能够提供一种API来控制此行为,而无需诉诸此类卑鄙的黑客手段.

I need to define my Service Worker as persistent in my Chrome extension because I'm using the webRequest API to intercept some data passed in a form for a specific request, but I don't know how I can do that. I've tried everything, but my Service Worker keeps unloading.

How can I keep it loaded and waiting until the request is intercepted?

解决方案

In your case it's probably a bug in ManifestV3, https://crbug.com/1024211: the worker doesn't wake up for webRequest events. So just use ManifestV2 until this is fixed because there is no such thing as a persistent service worker.

If you still want to prevent the background service worker from unloading, you'll have to open a messaging port from any tab's content script or from another page of the extension like the popup page. This port will live for five minutes (it's a bug/feature in ManifestV3) so you'll have to use the port's onDisconnect event to reconnect with some random tab again. The downside of this workaround is that only web pages run content scripts so if the browser doesn't have any tabs with web pages you're out of luck. Another downside is the need for content scripts on all URLs which is a broad host permission that puts most extensions in the slow review queue in the web store. Chromium team continues to ignore the established fact that sometimes it's necessary to have a persistent background script for an arbitrary duration of time, but let's hope they'll provide an API to control this behavior without the need to resort to such dirty hacks.

这篇关于Chrome扩展程序中的永久服务工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆