develop-userscripts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDevelop Userscripts
开发用户脚本
Userscript work usually breaks at the runtime and metadata boundary, not in the page logic. Choose the runtime first, declare the minimum permissions up front, then debug in the environment where the script actually runs.
用户脚本开发中的问题通常出现在运行时和元数据边界,而非页面逻辑部分。请先选择运行时,提前声明最小必要权限,再在脚本实际运行的环境中调试。
When to Use
适用场景
Use this skill for:
- writing or fixing a Tampermonkey or ScriptCat userscript
- debugging injection timing, missing permissions, CSP workarounds, update checks, or behavior
GM_* - deciding between a portable foreground script and ScriptCat-only or
@background@crontab - adding config UI with
==UserConfig== - packaging a ScriptCat bundle or preparing a CloudCat-compatible script
==UserSubscribe==
Do not use this skill for full browser extension development or general browser automation outside userscript managers.
该技能适用于以下场景:
- 编写或修复Tampermonkey或ScriptCat用户脚本
- 调试注入时机、权限缺失、CSP绕过、更新检查或相关行为问题
GM_* - 在可移植前台脚本和仅ScriptCat支持的/
@background脚本之间做选型@crontab - 用添加配置UI
==UserConfig== - 打包ScriptCat 包或适配CloudCat的脚本
==UserSubscribe==
请勿将该技能用于完整浏览器扩展开发,或用户脚本管理器之外的通用浏览器自动化场景。
Runtime Selection
运行时选型
dot
digraph userscript_runtime {
"Need page DOM or page context?" [shape=diamond];
"Need persistent or scheduled work?" [shape=diamond];
"Need to install many scripts as one package?" [shape=diamond];
"Portable foreground script" [shape=box];
"ScriptCat background or crontab script" [shape=box];
"ScriptCat subscription package" [shape=box];
"Need page DOM or page context?" -> "Portable foreground script" [label="yes"];
"Need page DOM or page context?" -> "Need persistent or scheduled work?" [label="no"];
"Need persistent or scheduled work?" -> "ScriptCat background or crontab script" [label="yes"];
"Need persistent or scheduled work?" -> "Need to install many scripts as one package?" [label="no"];
"Need to install many scripts as one package?" -> "ScriptCat subscription package" [label="yes"];
"Need to install many scripts as one package?" -> "Portable foreground script" [label="no"];
}dot
digraph userscript_runtime {
"Need page DOM or page context?" [shape=diamond];
"Need persistent or scheduled work?" [shape=diamond];
"Need to install many scripts as one package?" [shape=diamond];
"Portable foreground script" [shape=box];
"ScriptCat background or crontab script" [shape=box];
"ScriptCat subscription package" [shape=box];
"Need page DOM or page context?" -> "Portable foreground script" [label="yes"];
"Need page DOM or page context?" -> "Need persistent or scheduled work?" [label="no"];
"Need persistent or scheduled work?" -> "ScriptCat background or crontab script" [label="yes"];
"Need persistent or scheduled work?" -> "Need to install many scripts as one package?" [label="no"];
"Need to install many scripts as one package?" -> "ScriptCat subscription package" [label="yes"];
"Need to install many scripts as one package?" -> "Portable foreground script" [label="no"];
}Preflight
前期检查
- Confirm the manager and browser. On Manifest V3 browsers, ScriptCat may require or browser developer mode before scripts run.
Allow User Scripts - Decide page script versus background script before writing code. ScriptCat background scripts cannot touch the DOM.
- Start with metadata, not implementation: ,
@match,@grant,@connect, and any update URLs.@run-at - Prefer portable patterns for ordinary page scripts. Only switch to ScriptCat-only headers when the requested behavior actually needs them.
==UserScript==
- 确认使用的脚本管理器和浏览器。在Manifest V3浏览器中,ScriptCat运行脚本前可能需要开启「允许用户脚本」选项或浏览器开发者模式。
- 编码前先确定是开发页面脚本还是后台脚本,ScriptCat后台脚本无法操作DOM。
- 从元数据而非业务逻辑开始开发:先配置、
@match、@grant、@connect和所有更新链接。@run-at - 普通页面脚本优先使用可移植的规范,只有当需求确实需要时才切换到ScriptCat专属的头字段。
==UserScript==
Workflow
工作流
- Choose the runtime and metadata first.
- Declare the smallest permission surface that fits the task.
- Implement against the runtime you chose.
- Debug where the code really runs.
- Foreground scripts: page console plus manager logs.
- ScriptCat background scripts: run log first, then for real-environment debugging.
background.html
- Publish with the right update model.
- Normal scripts: keep accurate and add
@versionor@updateURLonly when needed.@downloadURL - Subscription bundles: use , HTTPS URLs, and subscription-level
==UserSubscribe==.@connect
- Normal scripts: keep
- 先确定运行时和元数据配置。
- 声明满足需求的最小权限范围。
- 基于所选运行时实现业务逻辑。
- 在代码实际运行的环境中调试。
- 前台脚本:页面控制台 + 脚本管理器日志
- ScriptCat后台脚本:先查看运行日志,再通过进行真实环境调试
background.html
- 按照合适的更新模式发布
- 普通脚本:保证准确,仅在需要时添加
@version或@updateURL@downloadURL - 订阅包:使用、HTTPS链接和订阅级的
==UserSubscribe==配置@connect
- 普通脚本:保证
Quick Reference
快速参考
| Intent | Default choice | Watch for |
|---|---|---|
| Page UI, DOM scraping, page patching | Portable | |
| Cross-origin API access | | Missing hosts, cookie behavior differences, user authorization |
| Long-running worker | ScriptCat | No DOM, must return |
| Scheduled task | ScriptCat | Only first |
| User-editable settings | | Block placement and |
| Silent bundle install and updates | | HTTPS, |
| 用途 | 默认选型 | 注意事项 |
|---|---|---|
| 页面UI、DOM抓取、页面补丁 | 可移植 | |
| 跨域API访问 | 搭配显式 | 缺失主机配置、Cookie行为差异、用户授权问题 |
| 长时间运行的worker | ScriptCat | 无DOM权限,异步任务必须返回 |
| 定时任务 | ScriptCat | 仅第一个 |
| 用户可编辑配置 | | 块放置位置和 |
| 静默安装和更新包 | | 必须使用HTTPS、命名为 |
Common Mistakes
常见错误
- Missing for APIs the script actually uses.
@grant - Missing for hosts used by
@connectorGM_xmlhttpRequest.GM_cookie - Treating as a better default than
@includefor ordinary host targeting.@match - Using DOM APIs inside ScriptCat background or cron scripts.
- Returning from a ScriptCat background script before async GM work is truly finished.
- Mixing and
==UserScript==packaging concepts.==UserSubscribe== - Putting in the wrong place or reading config keys without the
==UserConfig==name.group.key - Assuming Tampermonkey and ScriptCat storage, notification, or request behavior is identical.
- 脚本实际使用的API缺失对应的声明
@grant - 或
GM_xmlhttpRequest使用的主机缺失对应的GM_cookie声明@connect - 普通主机匹配场景下优先使用而非更安全的
@include@match - 在ScriptCat后台或定时脚本中使用DOM API
- ScriptCat后台脚本中异步GM任务未完全完成就提前返回
- 混淆和
==UserScript==打包概念==UserSubscribe== - 放置位置错误,或未使用
==UserConfig==格式读取配置键group.key - 假设Tampermonkey和ScriptCat的存储、通知、请求行为完全一致
References
参考资料
references/metadata-and-api-map.mdreferences/scriptcat-extensions.md
references/metadata-and-api-map.mdreferences/scriptcat-extensions.md