尝试使用端口让 Service Worker 在 Chrome 扩展中保持持久状态不起作用(Manifest v3)
我正在开发一个应该有持久服务工作者的扩展(同时使用 Manifest V3)。
我已经尝试过此答案中提出的解决方案(https://stackoverflow.com/a/66618269/10364842),它在自行测试时有效(我使用这里的文件来验证:https://bugs.chromium.org/p/chromium/issues/详细信息?id=1152255#c25)。
但是,当我将代码放入目标扩展中时,它不会阻止工作人员卸载。
我在 worker_wrapper.js
中使用 worker_wrapper.js
和 injectScripts
。我在 worker_wrapper.js
的顶部添加了使服务工作线程保持活动状态的代码(也尝试了其他位置)。
我已经验证此代码已注入到其中一个选项卡中:
chrome.runtime.connect({ name: 'keepAlive' });
console.log('keepAlive');
我可以看到该选项卡的控制台中打印有“keepAlive”。
然而,Service Worker 仍然被卸载。
上次我测试时,它在最后一个“keepAlive”打印到控制台后约 1 分钟卸载。
所以看起来它有时会起作用,但通常在调用keepAlive函数约1分钟后,服务工作线程仍然卸载。
不幸的是,我无法附上最小的可重现示例,因为我不确定导致问题的原因。代码来自 https://bugs.chromium.org/ p/chromium/issues/detail?id=1152255#c25 在单独运行时有效。
我还在 DevTools 打开的情况下进行了测试,但它仍然会卸载(显示消息“DevTools 与页面断开连接。页面重新加载后,DevTools 将自动重新连接。”)
测试的 Chrome 版本:99.0.4844.82、101.0.4947.0
测试的操作系统:Ubuntu 20.04
是否还有其他错误可能导致此问题?我应该尝试在每个选项卡中注入 chrome.runtime.connect 吗?或者尝试每 55 秒运行一次 keepAlive
函数,而不是每 4 分 55 秒运行一次?
I'm working on an extension that should have a persistent service worker (while using Manifest V3).
I've tried the solution proposed in this answer (https://stackoverflow.com/a/66618269/10364842), and it works when tested by itself (I used the files from here to verify: https://bugs.chromium.org/p/chromium/issues/detail?id=1152255#c25).
However, when I put the code in the target extension, it doesn't prevent the worker from unloading.
I'm using worker_wrapper.js
and injectScripts
inside worker_wrapper.js
. I added the code that keeps the service worker alive at the top of worker_wrapper.js
(tried other locations as well).
I've verified that this code gets injected into one of the tabs:
chrome.runtime.connect({ name: 'keepAlive' });
console.log('keepAlive');
I can see 'keepAlive' printed in the console for that tab.
However, the service worker still gets unloaded.
Last time I tested, it unloaded ~1 minute after the last 'keepAlive' was printed to the console.
So it seems it works sometimes, but often ~1 minute after keepAlive
function is called the service worker still unloads.
Unfortunately I can't attach a minimal reproducible example, as I'm not sure what causes the problem. And the code from https://bugs.chromium.org/p/chromium/issues/detail?id=1152255#c25 works when run by itself.
I've also tested with DevTools open, and it still unloads (with the message 'DevTools was disconnected from the page. Once page is reloaded, DevTools will automatically reconnect.')
Tested Chrome versions: 99.0.4844.82, 101.0.4947.0
Tested OS: Ubuntu 20.04
Could there be any other bugs that could cause this? Should I try injecting the chrome.runtime.connect
in each tab? Or try to run the keepAlive
function every 55 seconds instead of every 4 minutes 55 seconds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论