AutoSkill Userscript Event Monitoring and Error Handling
A reusable pattern for monitoring page events (AJAX, DOM, visibility) with per-event-type throttling, and safely extracting/parsing error messages using XPath with conditional logic.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/userscript-event-monitoring-and-error-handling" ~/.claude/skills/ecnu-icalk-autoskill-userscript-event-monitoring-and-error-handling && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/userscript-event-monitoring-and-error-handling/SKILL.mdsource content
Userscript Event Monitoring and Error Handling
A reusable pattern for monitoring page events (AJAX, DOM, visibility) with per-event-type throttling, and safely extracting/parsing error messages using XPath with conditional logic.
Prompt
Role & Objective
Act as a Userscript Developer. Create a script to monitor page events and handle error messages safely.
Operational Rules & Constraints
- Event Monitoring: Monitor XMLHttpRequest (loadstart, progress, load, error, abort), DOM mutations (MutationObserver), page visibility (visibilitychange), and page unload (beforeunload).
- Throttling: Implement per-event-type throttling. Use a dictionary/object to track the last log time for each event type separately. Do not use a global timestamp that blocks all events.
- Error Extraction: Create a function to extract text content using XPath. Wrap
in a try-catch block. Returndocument.evaluate
if the node does not exist or an error occurs.null - Error Matching: Use
to check if the error message contains specific text fragments. Implement conditional logic (if/else) to perform different actions based on the matched text.String.prototype.includes() - Variable Safety: When incrementing numeric variables (e.g., counters, retries), explicitly cast them to
before addition to prevent string concatenation.Number()
Anti-Patterns
- Do not use a global throttle variable that blocks all event types.
- Do not assume XPath nodes always exist; always handle missing nodes gracefully.
- Do not perform arithmetic on variables without ensuring they are numbers.
Triggers
- monitor page events userscript
- throttle event logging per type
- extract error message xpath safely
- handle error messages with conditional logic
- userscript event listener