-
Notifications
You must be signed in to change notification settings - Fork 310
Description
When navigating between pages using back/forward (history navigation), the badge count in the extension popup does not persist correctly. Currently the badge count is tracked only by tabId, and because chrome.webNavigation.onBeforeNavigate and popupPageLoadUpdate are not fired in some navigation scenarios, the popup loses the correct count.
Steps to reproduce:
- Open a page such as the beta changelog (e.g.,
docs.scriptcat.org/.../1.3.0-beta) - Navigate directly to another site (e.g.,
www.google.com)
→ Because the domain and number of iframes differ, badge counts change. - Navigate back using browser “Back”
→ The badge count disappears.
Another example is closing an installation page and going “back” — the count does not reappear correctly.
Expected behavior:
Badge count should persist across history navigation, similar to how Tampermonkey handles it.
Possible solutions proposed:
- Store badge count in a content script running in
frameId 0.
Whenchrome.webNavigation.onHistoryStateUpdatedfires, the service worker should request the count from the content script and send it to the popup. - Have the
frameId 0content script generate and store a random identifier, and send that ID with badge updates.
OnonHistoryStateUpdated, the service worker requests the correct ID from the content script and relays it to the popup.
In general, storing badge count in content script state seems more reliable than relying solely on tabId in the popup.
当使用 上一页/下一页(历史记录导航) 时,扩展徽章计数在弹出视窗中未正确保存。目前因为徽章计数仅以 tabId 来识别,而在某些导航情况下 chrome.webNavigation.onBeforeNavigate 与 popupPageLoadUpdate 事件没有被触发,所以导致徽章计数丢失。
重现步骤:
- 进入像是 beta 版本更新日志页(例如
docs.scriptcat.org/.../1.3.0-beta) - 直接输入另一个网址(例如
www.google.com)
→ 由于网域及 iframes 不同,徽章计数会改变 - 再按浏览器「上一页」
→ 徽章计数消失
另一例子是安装脚本页关闭后按「上一页」也是同样问题。
预期行为:
徽章计数应该能在历史导航间持续显示,像 Tampermonkey 一样。
提出的可能解法:
- 将徽章计数储存在
frameId 0的 content script 中。
在chrome.webNavigation.onHistoryStateUpdated事件时让 service_worker 与 content script 取得最新计数,再发送给 popup。 - 让
frameId 0content script 生成并储存一个 随机识别 ID,将该 ID 与徽章计数一起传送。
当onHistoryStateUpdated事件触发时,service_worker 会从 content script 读取该 ID 并提供给 popup。
总体来说,把徽章计数状态放在 content script 中,比单纯用 tabId 在 popup 端维持会更稳定。