Skip to content

docs(skill): userscript 反应式改动优先 Observer,定时器仅作有界兜底 - #135

Merged
broven merged 1 commit into
mainfrom
docs/userscript-observer-first
Oct 8, 2026
Merged

broven merged 1 commit into
mainfrom
docs/userscript-observer-first

Conversation

@broven

@broven broven commented Oct 8, 2026

Copy link
Copy Markdown
Owner

概述

  • 把 userscript 的「反应式改动」指引从只讲「等元素出现时优先用 MutationObserver 而不是轮询」,扩成完整的 observers first 章节:按信号选 observer 的对照表(MutationObserver / ResizeObserver / IntersectionObserver / PerformanceObserver / ReportingObserver,以及 matchMedia、visualViewport、navigation 这类事件型等价物)。
  • 补上降级阶梯:observer → 本来就触发的事件 → 一次性 setTimeout → 有界 requestAnimationFrame;并明确禁止 setInterval 轮询与无界自续 rAF。
  • 补上反模式与「根本没有 observer 的场景」(另一个 world 的 JS 变量、无 box 变化的 computed style、网络响应体、canvas 像素、用户输入)。
  • --print-skill 的 Userscripts 段改为指向新章节,并列出五个 observer 与「never setInterval or an unbounded rAF loop」。

背景:旧章节只覆盖「等 SPA 元素」,对「按帧跟踪元素几何/动画」是空白,正是无界 rAF 容易混进来的地方;本次先把规则写进文档。

无关联 issue。

测试证明

mise run test(daemon + skill + evals + pi 全绿):

mise run test
[test:evals] summary
[test:evals] ============================================================
[test:evals]   ✓ 13/13 passed
[test:evals] Finished in 432.0ms
[test:pi] ℹ pass 5
[test:pi] ℹ fail 0
[test:pi] ℹ cancelled 0
[test:pi] ℹ skipped 0
[test:pi] ℹ todo 0
[test:pi] Finished in 613.0ms
[test:skill] 338 passed in 9.13s
[test:skill] Finished in 10.91s
[test:daemon] 982 passed, 102 skipped in 49.14s
[test:daemon] Finished in 50.47s
Finished in 50.49s

界面证据

N/A:无界面改动

检查项

  • 「测试证明」已贴实际命令与输出
  • 「界面证据」已填截图或视频,或已写明 N/A

🤖 Generated with Claude Code

The userscript reactivity section only said to prefer a MutationObserver
over polling "to wait for an element". It was silent on the
geometry/animation-tracking case, which is exactly where a self-scheduling
requestAnimationFrame loop creeps in and burns a core on an idle page (a
shipped bilibili card-blocker did this).

Expand it into "Reacting to change: observers first": a signal -> observer
table (MutationObserver, ResizeObserver, IntersectionObserver,
PerformanceObserver, ReportingObserver, plus the event-based
matchMedia/visualViewport/navigation equivalents), the fallback ladder
(observer -> discrete event -> one-shot setTimeout -> bounded rAF),
the anti-patterns, and the cases that have no observer at all. Point
--print-skill's Userscripts section at the new section.

Co-Authored-By: Claude <noreply@anthropic.com>
@broven
broven merged commit 91abab7 into main Oct 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant