Commit a03ab7f
committed
perf: 把 content-observer 的优化覆盖扩展到所有重型视频站、缩略图墙、聊天直播站
之前只针对 B 站做了三轴优化(Set 去重 / SKIP_TAGS 跳媒体元素 / mutation+resource 上限自停),但场景类似的站点很多——抖音 / 快手 / TikTok 短视频流、YouTube / Twitch / niconico / AcFun 弹幕聊天、Pornhub / XHamster / XVideos / MissAV / Jable / JavDB 等大量缩略图墙加无限滚动。这一轮把这套保护机制做厚做全。
PerformanceObserver 加双轴过滤:
- SKIP_INITIATOR_TYPES 拒绝 img / video / audio / beacon / track / object / embed / css 这八类 entry。这些资源对识别技术栈零价值(图片是产品 / 缩略图,video / audio 是流媒体本体,css 是样式不是 stylesheet 文件本身),却占掉绝大多数 PerformanceObserver entry。Pornhub / 抖音的资源数能从几千降到几百
- SKIP_RESOURCE_EXT 兜底正则:.ts / .m4s / .mp4 / .webm / .mov / .m3u8 / .mpd / .jpg / .jpeg / .png / .gif / .webp / .avif / .ico / .woff / .woff2 / .ttf / .otf / .eot 这些扩展名直接 skip。HLS 视频流 .ts 分片每分钟产生几百条,不过滤一定打满。entry 携带 query string 也匹配。两轴并联是因为 initiatorType 不一定可靠(某些 SDK 用 fetch 加载 m3u8,initiatorType=fetch 不会被 SKIP_INITIATOR_TYPES 拦下,靠扩展名兜底)
mutation 加 burst 冷却:
- 1 秒滑动窗口,新增节点数累计超过 MUTATION_BURST_THRESHOLD = 300 直接进 5 秒冷却(mutationCooldownUntil = now + 5000)
- 冷却期内整个 MutationObserver 回调直接 return,不入队、不 flush、清掉 pendingMutationNodes 与未调度的 RAF
- 抖音 / 快手 / TikTok feed 滑动一次插入 200-1000 个 DOM 节点,在原本「累计 5000 条自停」之前就已经卡 1-2 秒。Burst 冷却让前 300 条收完就立即停采集 5 秒,给主线程让位
- 5 秒后窗口重新开始计数,如果用户已经从短视频流滑走或停手,新窗口里 mutation 数量会回归正常水平继续采集;如果还在持续滑动,下一个窗口同样很快触发冷却,循环保护
弹幕 / 聊天 / 直播 / feed 容器整个子树拉黑:
- SKIP_CONTAINER_NAMES 是一组正则,匹配元素的 id 或 class token:danmaku / barrage / bullet-comment / bullet-screen / chat / chat-(panel|area|list|box|room|stream|window) / live-chat / comment-(stream|live|list) / feed / webcast
- 命中:B 站 / AcFun / niconico 弹幕容器、Twitch / YouTube live chat、抖音 / TikTok 直播间 webcast 容器、各 SPA 推荐 feed
- 检查时机:collectFromElement 入口和 mutation push 入队前各拦截一次。命中即整个子树跳过,不进 querySelectorAll、不入队
- 实现细节:matchesSkipContainer 把 element.id + className.split(/\s+/) 拼成 token 列表,对 9 条正则做 token 级匹配(不跑 element.matches,避免选择器编译开销)。class token 切分一次,正则数组遍历一次
state.resourceCount 计入跳过前的总数(用作自停阈值),但 addUrl 只对真正入列的资源算"添加成功",scheduleSend 只在真正添加时触发。这样过滤后的 entry 不会引发不必要的 sendMessage 噪声,但累计阈值仍按"实际收到了多少 entry"工作。
插件版本升级到 1.1.3。1 parent 0f147b5 commit a03ab7f
2 files changed
Lines changed: 59 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
11 | 27 | | |
12 | 28 | | |
13 | 29 | | |
| |||
39 | 55 | | |
40 | 56 | | |
41 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
42 | 61 | | |
43 | 62 | | |
44 | 63 | | |
| |||
197 | 216 | | |
198 | 217 | | |
199 | 218 | | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
200 | 234 | | |
201 | 235 | | |
202 | 236 | | |
| 237 | + | |
203 | 238 | | |
204 | 239 | | |
205 | 240 | | |
| |||
369 | 404 | | |
370 | 405 | | |
371 | 406 | | |
| 407 | + | |
372 | 408 | | |
373 | | - | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
374 | 412 | | |
375 | 413 | | |
376 | 414 | | |
377 | 415 | | |
378 | 416 | | |
379 | 417 | | |
380 | | - | |
| 418 | + | |
381 | 419 | | |
382 | 420 | | |
383 | 421 | | |
| |||
426 | 464 | | |
427 | 465 | | |
428 | 466 | | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
429 | 473 | | |
430 | 474 | | |
431 | 475 | | |
432 | 476 | | |
| 477 | + | |
433 | 478 | | |
| 479 | + | |
434 | 480 | | |
435 | 481 | | |
436 | 482 | | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
437 | 493 | | |
438 | 494 | | |
439 | 495 | | |
| |||
0 commit comments