Skip to content

Commit d8b79c7

Browse files
committed
perf: 全量识别 schedule 路径加 30 秒节流
修复 dynamic-snapshot 死循环后仍观察到 page-detector 每 12-23 秒被重新注入、每次同步阻塞主线程 1.5+ 秒。scheduleActivePageDetection 在距离上次成功跑过不到 30 秒时跳过排程,让 tabs.onUpdated 反复触发 complete 等情况下不再堆积全量识别。tab loading/removed/url 不支持时清掉 throttle,确保 F5 刷新和切换页面仍能正常重检;用户主动重检走 message-router 直调 runActivePageDetection 不受节流。将版本号提升到 1.2.85。
1 parent d18a97b commit d8b79c7

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "stackprism",
33
"private": true,
4-
"version": "1.2.84",
4+
"version": "1.2.85",
55
"type": "module",
66
"description": "StackPrism 用于检测网页前端、后端、CDN、SaaS、广告营销、统计、登录、支付、网站程序和主题模板线索。",
77
"scripts": {

src/background/detection.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { injectContentObserver } from './content-injector'
88
import { isDetectablePageUrl } from '@/utils/page-support'
99

1010
const activeDetectionTimers = new Map<number, ReturnType<typeof setTimeout>>()
11+
const lastDetectionRunAt = new Map<number, number>()
12+
const DETECTION_THROTTLE_MS = 30000
1113

1214
const normalizePageUrl = (value: unknown): string => {
1315
try {
@@ -130,6 +132,7 @@ export const runActivePageDetection = async (tabId: number) => {
130132
data.updatedAt = Date.now()
131133
await saveTabDataAndBadge(tabId, data, settings)
132134
scheduleBundleLicenseDetection(tabId)
135+
lastDetectionRunAt.set(tabId, Date.now())
133136
} catch {
134137
return
135138
}
@@ -143,8 +146,16 @@ export const clearActiveDetectionTimer = (tabId: number) => {
143146
}
144147
}
145148

149+
export const clearDetectionThrottle = (tabId: number) => {
150+
lastDetectionRunAt.delete(tabId)
151+
}
152+
146153
export const scheduleActivePageDetection = (tabId: number, delay = 600) => {
147154
if (typeof tabId !== 'number' || tabId < 0) return
155+
const last = lastDetectionRunAt.get(tabId) || 0
156+
if (last && Date.now() - last < DETECTION_THROTTLE_MS) {
157+
return
158+
}
148159
clearActiveDetectionTimer(tabId)
149160
const timer = setTimeout(() => {
150161
activeDetectionTimers.delete(tabId)

src/background/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { injectContentObserverIntoOpenTabs } from './content-injector'
22
import { clearBadge, clearTabSession } from './tab-store'
33
import { clearDynamicSnapshotTimer, clearPendingDynamicSnapshot } from './dynamic-snapshot'
44
import { buildHeaderRecord, dedupeApiRecords, mergeHeaderRecords, shouldMergeHeaderRecords } from './headers'
5-
import { clearActiveDetectionTimer, refreshAllBadges, saveTabDataAndBadge, scheduleActivePageDetection } from './detection'
5+
import {
6+
clearActiveDetectionTimer,
7+
clearDetectionThrottle,
8+
refreshAllBadges,
9+
saveTabDataAndBadge,
10+
scheduleActivePageDetection
11+
} from './detection'
612
import { getTabData, getTabSnapshot } from './tab-store'
713
import { SETTINGS_STORAGE_KEY, applyDetectorSettingsUpdate, loadDetectorSettings, loadTechRules } from './detector-settings'
814
import { registerMessageRouter } from './message-router'
@@ -22,6 +28,7 @@ chrome.runtime.onStartup.addListener(() => {
2228

2329
chrome.tabs.onRemoved.addListener(tabId => {
2430
clearActiveDetectionTimer(tabId)
31+
clearDetectionThrottle(tabId)
2532
clearBundleLicenseTimer(tabId)
2633
clearDynamicSnapshotTimer(tabId)
2734
clearPendingDynamicSnapshot(tabId)
@@ -30,6 +37,7 @@ chrome.tabs.onRemoved.addListener(tabId => {
3037

3138
const clearTabDetectionState = (tabId: number) => {
3239
clearActiveDetectionTimer(tabId)
40+
clearDetectionThrottle(tabId)
3341
clearBundleLicenseTimer(tabId)
3442
clearDynamicSnapshotTimer(tabId)
3543
clearPendingDynamicSnapshot(tabId)
@@ -62,6 +70,7 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
6270

6371
if (changeInfo.status === 'loading') {
6472
clearActiveDetectionTimer(tabId)
73+
clearDetectionThrottle(tabId)
6574
clearDynamicSnapshotTimer(tabId)
6675
clearPendingDynamicSnapshot(tabId)
6776
clearBadge(tabId)

0 commit comments

Comments
 (0)