StarModel 邀请活动
← 返回 Skill 列表
extension
分类: 开发与工程API Key 暂未确认

tampermonkey

编写Tampermonkey用户脚本来实现浏览器自动化、页面修改和网页增强。在创建浏览器脚本、编写Greasemonkey脚本、自动化用户交互、向网页注入CSS或JavaScript、修改网站行为、构建浏览器扩展、隐藏不需要的页面元素、添加表单自动填充、抓取网站数据、拦截请求、检测单页应用中的URL变化,或存储持久化的用户偏好设置时使用。涵盖用户脚本头部(@match, @grant, @require)、同步和异步GM_* API函数、常见模式(DOM变更、URL变化检测、元素等待)、安全沙箱以及跨浏览器兼容性(Chrome、Firefox、Edge)。

person作者: jakexiaohubgithub

Tampermonkey Userscript Development

Steps

  1. Read the references the task needs from the table at the end. Done when every file whose condition matches the task has been read.
  2. A script that does not run: in Chrome and Edge, first have the user confirm the "Allow User Scripts" toggle on the extension's details page (Developer Mode on older browsers) - common-pitfalls.md Pitfall 15 - then work through debugging.md. Done when the cause is named and the fix is in the script or handed to the user.
  3. Write the header from the template below. Done when every GM API the body calls has its @grant, every cross-origin host has its @connect, and @match covers only the target pages.
  4. Write the body, checking each limit below against what the user asked for. Done when the script is complete, with no placeholder left for the user to fill beyond the customisation points you name.
  5. Run the pre-delivery checklist. Done when every item passes or the failure is stated in the reply.
  6. Reply with: what the script does (1-2 sentences); the complete userscript in one code block; installation ("Copy/paste into Tampermonkey dashboard" or "Save as .user.js"); customisation points the user can safely change (selectors, timeouts); each @grant used and why; browser support if not universal.

Template

// ==UserScript==
// @name         My Script Name                    // unique script name
// @namespace    https://example.com/scripts/      // your unique namespace
// @version      1.0.0                             // increment on every update
// @description  Brief description of the script
// @author       Your Name
// @match        https://example.com/*             // target URL pattern
// @grant        GM.getValue                       // one line per GM API used
// @grant        GM.setValue
// @run-at       document-idle                     // the default
// ==/UserScript==

(async () => {
    'use strict';
    const setting = await GM.getValue('myKey', 'default');
})();

A script that uses no GM APIs takes @grant none (which disables the sandbox) and a plain (function() { 'use strict'; ... })(); wrapper.

Grants

| The script needs to | Grant | |---------------------|-------| | Store persistent data | GM_setValue + GM_getValue | | Batch read/write settings (v5.3+) | GM.getValues + GM.setValues | | Make cross-origin requests | GM_xmlhttpRequest + @connect domain | | Add custom CSS | GM_addStyle | | Access the page's window | unsafeWindow | | Show notifications | GM_notification | | Add menu commands | GM_registerMenuCommand | | Detect URL changes (SPA) | window.onurlchange | | Mute/unmute tab audio | GM_audio |

Limits

A userscript cannot:

  • read or write local files
  • run before page scripts when in isolated sandbox mode
  • reach into cross-origin iframes
  • carry GM storage across machines - it is local to each browser
  • bypass a very strict CSP
  • run in Chrome or Edge until the user enables "Allow User Scripts" - a script cannot request or bypass this

Workarounds for most of these are in common-pitfalls.md.

Pre-delivery checklist

Critical:

  • [ ] No hardcoded API keys, tokens or passwords
  • [ ] @match is specific (not *://*/*)
  • [ ] All external URLs use HTTPS
  • [ ] User input sanitised before DOM insertion
  • [ ] No eval(), new Function() or string-based setTimeout/setInterval

Important:

  • [ ] Wrapped in an IIFE with 'use strict'
  • [ ] Every @grant is necessary
  • [ ] @connect includes every external domain
  • [ ] Error handling for async operations
  • [ ] Null checks before DOM manipulation

Recommended:

  • [ ] @version follows semantic versioning (X.Y.Z)
  • [ ] Works in both Chrome and Firefox

References

| Read | When the task involves | |------|------------------------| | header-reference.md | Any tag beyond the template: @run-at timing, @run-in, @require/@resource with SRI, @noframes, update URLs, i18n names | | url-matching.md | A @match beyond one domain or its subdomains: specific paths, @include regex, @exclude | | patterns.md | Waiting for an element, SPA navigation, mutation observers, element or form manipulation, keyboard shortcuts, table or link extraction, debouncing, retry with backoff - full implementations | | sandbox-modes.md | @sandbox, page-context access, CSP errors, Firefox cloneInto/exportFunction | | api-async.md | GM.* promise APIs - prefer these for new scripts | | api-sync.md | GM_* callback APIs: notifications, menu commands, clipboard, downloads, resources, GM_info | | api-storage.md | Stored settings, batch values, value-change listeners | | http-requests.md | Cross-origin requests with GM_xmlhttpRequest | | web-requests.md | Intercepting or blocking requests: GM_webRequest (Firefox only), page-level interception elsewhere | | api-cookies.md | Reading or changing cookies with GM_cookie | | api-dom-ui.md | GM_addElement, GM_addStyle, unsafeWindow, custom UI, page frameworks | | api-tabs.md | Tab-persistent data, opening or closing tabs, cross-tab communication | | api-audio.md | Muting or unmuting tabs | | common-pitfalls.md | A script that misbehaves, or a workaround for a limit above | | debugging.md | A script that does not run or throws | | browser-compatibility.md | Chrome vs Firefox differences, Manifest V3, Safari | | security-checklist.md | Scripts handling credentials, cookies or user input, or reviewing someone else's script | | version-numbering.md | Version strings beyond X.Y.Z, or auto-update comparison | | typescript.md | TypeScript: @types/tampermonkey, tsconfig, bundling to a single .user.js |