### Blacklist Configuration Example Source: https://violentmonkey.github.io/posts/smart-rules-for-blacklist This example demonstrates how to configure a blacklist using hostnames, @exclude-match, and @exclude rules. Comments are ignored. ```userscript # hostnames www.google.com # @exclude-match rules *://twitter.com/* # @exclude rules @exclude https://example.com/* ``` -------------------------------- ### Whitelist Configuration Example Source: https://violentmonkey.github.io/posts/smart-rules-for-blacklist This example shows how to set up a whitelist using @match and @include rules, followed by a catch-all rule to block other URLs. This effectively disables scripts on all sites except those explicitly whitelisted. ```userscript # match rules @match https://www.google.com/* # include rules @include https://example.com/* # block everything else * ``` -------------------------------- ### Start Development Server Source: https://violentmonkey.github.io/guide/using-modern-syntax Run this command to start the development server. It watches for changes in your source code and recompiles it to the dist directory. ```bash $ npm run dev ``` -------------------------------- ### Install @violentmonkey/shortcut with NPM Source: https://violentmonkey.github.io/guide/keyboard-shortcuts Install the @violentmonkey/shortcut library as a project dependency using NPM. This method is recommended for projects using a bundler and TypeScript. ```bash $ npm i @violentmonkey/shortcut ``` -------------------------------- ### Register a simple keyboard shortcut Source: https://violentmonkey.github.io/guide/keyboard-shortcuts Use the `register` function from VM.shortcut to bind a key combination to a callback function. This example registers 'Ctrl-I'. ```javascript VM.shortcut.register('c-i', () => { console.log('You just pressed Ctrl-I'); }); ``` -------------------------------- ### Register a key sequence shortcut Source: https://violentmonkey.github.io/guide/keyboard-shortcuts The library supports registering sequences of key presses. This example registers the sequence 'Ctrl-A' followed by 'Ctrl-B'. ```javascript VM.shortcut.register('c-a c-b', () => { console.log('You just pressed Ctrl-A Ctrl-B sequence'); }); ``` -------------------------------- ### Basic @include and @exclude Rules Source: https://violentmonkey.github.io/api/matching Shows examples of @include and @exclude rules using normal strings with wildcards and as regular expressions. ```javascript // @include * // @include https://www.google.com/* // @include /\.com\.hk\// ``` ```javascript // @exclude https://www.google.com/exact/url ``` -------------------------------- ### Demonstration of Synchronous vs. Asynchronous Injection Source: https://violentmonkey.github.io/posts/inject-scripts-with-blob-urls This example demonstrates the difference in execution order between `textContent` injection (synchronous) and Blob URL injection (asynchronous). The asynchronous script logs its output last. ```javascript { const s = document.createElement('script'); s.textContent = 'console.log(1)'; document.body.appendChild(s); document.body.removeChild(s); } { const s = document.createElement('script'); const b = new Blob(['console.log(2)'], { type: 'text/javascript' }); const u = URL.createObjectURL(b); s.src = u; document.body.appendChild(s); document.body.removeChild(s); URL.revokeObjectURL(u); } console.log(3); ``` -------------------------------- ### Basic Metadata Block Structure Source: https://violentmonkey.github.io/api/metadata-block The metadata block must start with `// ==UserScript==` and end with `// ==/UserScript==`. Each line within the block must begin with `//` and have exactly one space after it. Place the metadata block at the very beginning of the file. ```javascript // ==UserScript== // @key value // ==/UserScript== ``` -------------------------------- ### Provide Support URL - @supportURL Source: https://violentmonkey.github.io/api/metadata-block The @supportURL metadata key provides a link to where users can get support for the script, displayed via an icon in the user scripts list. ```javascript // @supportURL https://violentmonkey.github.io/ ``` -------------------------------- ### Import @violentmonkey/shortcut after NPM installation Source: https://violentmonkey.github.io/guide/keyboard-shortcuts After installing @violentmonkey/shortcut via NPM, you can import its functions directly into your code. ```javascript import { register } from '@violentmonkey/shortcut'; ``` -------------------------------- ### GM.xmlHttpRequest with Abort Control (VM2.18.3+) Source: https://violentmonkey.github.io/api/gm This example shows how to get a control object from GM.xmlHttpRequest in VM2.18.3+ and Tampermonkey, allowing you to abort the request. The request can then be awaited. ```javascript // VM2.18.3+, TM const control = GM.xmlHttpRequest({ url }); myButton.addEventListener('click', control.abort, { once: true }); const res = await control; ``` -------------------------------- ### GM.download Source: https://violentmonkey.github.io/api/gm Initiates a file download. ```APIDOC ## GM.download ### Description Initiates a file download. ### Method `GM.download` (async) ``` -------------------------------- ### Create New Script Directory Source: https://violentmonkey.github.io/guide/using-modern-syntax Use these commands to create a new directory for your userscript project and navigate into it. ```bash $ mkdir my-script $ cd my-script ``` -------------------------------- ### Download URL to Local File with GM_download (Separate Parameters) Source: https://violentmonkey.github.io/api/gm Download a URL to a local file by providing the URL and filename as separate parameters to GM_download. This is a simpler way to initiate a download when advanced options are not needed. ```javascript GM_download(url, name) ``` -------------------------------- ### Basic @match and @exclude-match Rules Source: https://violentmonkey.github.io/api/matching Demonstrates the basic syntax for @match and @exclude-match rules, which are recommended for their safety and strictness. ```javascript // @match *://*/* // @exclude-match *://*.tk/* ``` -------------------------------- ### Generate Userscript Project Source: https://violentmonkey.github.io/guide/using-modern-syntax This command initializes a new userscript project using Yeoman and the Violentmonkey generator, setting up a JavaScript toolchain with Babel and Rollup. ```bash $ npx -p github:violentmonkey/generator-userscript -p yo yo @violentmonkey/userscript ``` -------------------------------- ### Download URL to Local File with GM_download (Object Options) Source: https://violentmonkey.github.io/api/gm Initiate a download of a URL to a local file using GM_download with an options object. This method supports various configurations similar to GM_xmlhttpRequest, including headers, timeout, and event handlers for download progress and completion. ```javascript GM_download(options) ``` -------------------------------- ### GM.openInTab Source: https://violentmonkey.github.io/api/gm Opens a URL in a new tab. ```APIDOC ## GM.openInTab ### Description Opens a URL in a new tab. ### Method `GM.openInTab` ``` -------------------------------- ### Build Userscript for Production Source: https://violentmonkey.github.io/guide/using-modern-syntax Execute this command to compile your source code once for production. The compiled script will be saved in the dist directory. ```bash $ npm run build ``` -------------------------------- ### Update Menu Command In-Place Source: https://violentmonkey.github.io/api/gm Demonstrates how to change a menu command in-place by using its existing ID, preserving its position. If in-place update is not supported, it shows how to unregister and re-register commands. ```javascript const id = 'status'; const inplace = id === GM_registerMenuCommand('Enabled', onClick, { id }); if (inplace) { // supported: change the command in-place using the same `id` GM_registerMenuCommand('Disabled', onClick, { id, title: 'Status' }); } else { // not supported: recreate the commands GM_unregisterMenuCommand('Enabled'); GM_unregisterMenuCommand('Foo'); GM_unregisterMenuCommand('Bar'); GM_registerMenuCommand('Disabled', onClick); GM_registerMenuCommand('Foo', onClick2); GM_registerMenuCommand('Bar', onClick3); } ``` -------------------------------- ### Script Description with Localization Source: https://violentmonkey.github.io/api/metadata-block The `@description` key provides a summary of the script. It can be localized using `@description:localeCode`. ```javascript // @description This script rocks. // @description:zh-CN 这个脚本很棒! ``` -------------------------------- ### GM_openInTab Source: https://violentmonkey.github.io/api/gm Opens a URL in a new tab, with options for controlling its behavior. ```APIDOC ## GM_openInTab ### Description Opens URL in a new tab. ### Method Signature (Object Options) ```javascript let tabControl = GM_openInTab(url, options) ``` ### Parameters (Object Options) * **url** : string The URL to open in a new tab. URL relative to current page is also allowed. Note that Firefox disallows `data:`, `blob:`, `chrome:`, `file:`, and many `about:` URLs. * **options?** : object * **active** : boolean = true Make the new tab active (i.e. open in foreground). * **container?** : number - since VM2.12.5, Firefox-only Set tab’s container in Firefox: * not specified = reuse script’s tab container * `0` = default (main) container * `1`, `2`, etc. = internal container index * **insert** : boolean = true - since VM2.11.0 Insert the new tab next to the current tab and set its “openerTab” so when it’s closed the original tab will be focused automatically. When `false` or not specified, the usual browser behavior is to open the tab at the end of the tab list. * **pinned** : boolean = false - since VM2.12.5 Pin the tab (i.e. show without a title at the beginning of the tab list). ### Method Signature (Boolean Option) ```javascript let tabControl = GM_openInTab(url, openInBackground) ``` ### Parameters (Boolean Option) * **openInBackground** : boolean Open the tab in background. Note, this is a reverse of the first usage method so for example `true` is the same as `{ active: false }`. ### Returns An object with following properties: * **onclose?** : () => void Сan be assigned to a function. If provided, it will be called when the opened tab is closed. * **closed** : boolean Whether the opened tab is closed. * **close** : () => void A function to explicitly close the opened tab. ### Example ```javascript let tabControl = GM_openInTab(url); tabContro.onclose = () => console.log('tab is closed'); tabContro.close(); ``` ``` -------------------------------- ### Specify Script Download URL - @downloadURL Source: https://violentmonkey.github.io/api/metadata-block The @downloadURL metadata key specifies the URL from which the script can be downloaded. This is used for automatic updates. ```javascript // @downloadURL https://example.com/myscript.user.js ``` -------------------------------- ### Specify Script Execution Time - @run-at Source: https://violentmonkey.github.io/api/metadata-block Use @run-at to control when your script runs. Options include document-start, document-body, document-end (default), and document-idle. ```javascript // @run-at document-idle ``` -------------------------------- ### GM_setClipboard Source: https://violentmonkey.github.io/api/gm Sets data to the system clipboard. Supports plain text by default, but can be extended to other MIME types. ```APIDOC ## GM_setClipboard ### Description Sets data to system clipboard. ### Method Signature ```javascript GM_setClipboard(data, type?) ``` ### Parameters * **data** : string - Required The data to be copied to system clipboard. * **type** : string = 'text/plain' - Optional The MIME type of data to copy. Defaults to 'text/plain'. ### Examples ```javascript GM_setClipboard('This text will be copied to the clipboard.') GM_setClipboard('Hello World', 'text/html') ``` ``` -------------------------------- ### GM.setClipboard Source: https://violentmonkey.github.io/api/gm Sets the content of the system clipboard. ```APIDOC ## GM.setClipboard ### Description Sets the content of the system clipboard. ### Method `GM.setClipboard` ``` -------------------------------- ### GM.registerMenuCommand Source: https://violentmonkey.github.io/api/gm Registers a command that will appear in the Violentmonkey menu. ```APIDOC ## GM.registerMenuCommand ### Description Registers a command that will appear in the Violentmonkey menu. ### Method `GM.registerMenuCommand` ``` -------------------------------- ### Add @violentmonkey/shortcut via @require Source: https://violentmonkey.github.io/guide/keyboard-shortcuts Include the @violentmonkey/shortcut library in your userscript's meta block using `@require` to make it available. ```javascript // ==UserScript== // ... // @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1 // ==/UserScript== ``` -------------------------------- ### Register a menu command with a shortcut Source: https://violentmonkey.github.io/guide/keyboard-shortcuts Associate a keyboard shortcut with a `GM_registerMenuCommand`. The shortcut is displayed in the menu item name using `VM.shortcut.reprShortcut`. ```javascript const shortcut = 'c-g c-g'; const name = 'Good Game'; const handler = () => alert('Good Game!'); // Register menu command const menuName = `${name} (${VM.shortcut.reprShortcut(shortcut)})`; GM_registerMenuCommand(menuName, handler); // Register shortcut VM.shortcut.register(shortcut, handler); ``` -------------------------------- ### Show Desktop Notification (Parameters) Source: https://violentmonkey.github.io/api/gm Displays a desktop notification using separate parameters, compatible with Greasemonkey. ```javascript GM_notification(text, title, image, onclick) ``` -------------------------------- ### Access VM.shortcut exports after @require Source: https://violentmonkey.github.io/guide/keyboard-shortcuts Once @violentmonkey/shortcut is included via `@require`, its exports can be accessed through the `VM.shortcut` object. ```javascript const { register } = VM.shortcut; ``` -------------------------------- ### Show Desktop Notification (Object Options) Source: https://violentmonkey.github.io/api/gm Displays a desktop notification using the browser's notifications API with detailed options. ```javascript let control = GM_notification(options) ``` -------------------------------- ### GM_download Source: https://violentmonkey.github.io/api/gm Downloads a URL to a local file. Supports downloading via an options object or separate parameters. ```APIDOC ## GM_download _Since VM2.9.5_ Downloads a URL to a local file. ### Usage 1. Using an object: ```javascript GM_download(options) ``` 2. Using separate parameters: ```javascript GM_download(url, name) ``` ### Parameters #### options (object) * **url** (string) - The URL to download. * **name** (string) - The filename to save to. Folders/subpaths aren’t supported yet. * **headers?** (object) * **timeout?** (number) - since VM2.9.5 * **context?** (any) - since VM2.13.4 * **user?** (string) - since VM2.13.4 * **password?** (string) - since VM2.13.4 * **anonymous?** (boolean = false) - since VM2.13.4 * **onabort?** (() => void) - since VM2.13.4 * **onerror?** (() => void) * **onload?** (() => void) * **onloadend?** (() => void) - since VM2.13.4 * **onloadstart?** (() => void) - since VM2.13.4 * **onprogress?** (() => void) * **onreadystatechange?** (() => void) - since VM2.13.4 * **ontimeout?** (() => void) #### url (string) - (Separate parameters usage) The URL to download. #### name (string) - (Separate parameters usage) The filename to save to. Folders/subpaths aren’t supported yet. ### Event Handlers * **onload** - The `onload` event handler is called after the data is downloaded from URL, before writing the file. * Other event handlers like `onabort`, `onerror`, `onloadend`, `onloadstart`, `onprogress`, `onreadystatechange`, `ontimeout` are also supported and function similarly to `GM_xmlhttpRequest`. ### Returns A control object with the following properties, same as GM_xmlhttpRequest: * **abort** (() => void) - A function to abort the request. ``` -------------------------------- ### GM.info Source: https://violentmonkey.github.io/api/gm Provides information about the current script. ```APIDOC ## GM.info ### Description Provides information about the current script. ### Method `GM.info` ``` -------------------------------- ### Disable and re-enable all key bindings Source: https://violentmonkey.github.io/guide/keyboard-shortcuts Temporarily disable all registered keyboard shortcuts using `VM.shortcut.disable()` and re-enable them later with `VM.shortcut.enable()`. ```javascript // Disable all key bindings temporarily VM.shortcut.disable(); // Re-enable key bindings VM.shortcut.enable(); ``` -------------------------------- ### Observe DOM Elements with jQuery and @violentmonkey/dom Source: https://violentmonkey.github.io/guide/observing-dom Integrate jQuery with VM.observe to manipulate dynamically created DOM elements using jQuery methods. Ensure both @violentmonkey/dom and jQuery are included as dependencies. ```javascript VM.observe(document.body, () => { // Find the target node const $node = $('.profile'); if ($node.length) { $node.prepend('

Profile

'); // disconnect observer return true; } }); ``` -------------------------------- ### GM_registerMenuCommand Source: https://violentmonkey.github.io/api/gm Registers a command in the Violentmonkey popup menu. It can optionally take an options object for advanced configurations like setting an ID, icon, title, or auto-close behavior. ```APIDOC ## GM_registerMenuCommand ### Description Registers a command in Violentmonkey popup menu. ### Method Signature ```javascript GM_registerMenuCommand(caption, onClick, options?) ``` ### Parameters * **caption** : string This text will be shown in the popup menu. * **onClick** : (event) => void Callback function when the command is clicked. * **event** : [object Object] - since VM2.13.1 The event that activated the command, providing details like `event.button`, `event.shiftKey`, `event.key`, etc. * **options?** : object - since VM2.15.9 Optional configuration for the menu command. * **id?** : string - since VM2.15.9 Default: `caption` text since VM2.16.2. Default in 2.15.9-2.16.1: a randomly generated string. * **icon?** : string - since VM2.31.1 URL of the icon to display. * **title?** : string A hint shown in the status bar when hovering the command. * **autoClose?** : boolean = true Whether to auto-close the popup after the user invokes the command. ### Returns Returns the command’s `caption` since VM2.12.5 or `id` since VM2.15.9. ### Examples ```javascript GM_registerMenuCommand('Text', onClick) const id2 = GM_registerMenuCommand('Text2', onClick, { title: 'Two' }) const id3 = GM_registerMenuCommand('Text3', onClick, { autoClose: false }) // Example for in-place command change const id = 'status'; const inplace = id === GM_registerMenuCommand('Enabled', onClick, { id }); if (inplace) { GM_registerMenuCommand('Disabled', onClick, { id, title: 'Status' }); } else { GM_unregisterMenuCommand('Enabled'); GM_unregisterMenuCommand('Foo'); GM_unregisterMenuCommand('Bar'); GM_registerMenuCommand('Disabled', onClick); GM_registerMenuCommand('Foo', onClick2); GM_registerMenuCommand('Bar', onClick3); } ``` ``` -------------------------------- ### Resource Declaration Source: https://violentmonkey.github.io/api/metadata-block The `@resource` key declares static resources that can be accessed using `GM_getResourceText` and `GM_getResourceURL`. It includes a resource name and its URL. ```javascript // @resource logo https://my.cdn.com/logo.png ``` ```javascript // @resource text https://my.cdn.com/some-text.txt ``` -------------------------------- ### Version Declaration Source: https://violentmonkey.github.io/api/metadata-block The `@version` key specifies the script's version, composed of parts joined by dots. If omitted, the script will not be updated automatically. ```javascript // @version 1.0 ``` ```javascript // @version 1.2a.3 ``` -------------------------------- ### Script Name with Localization Source: https://violentmonkey.github.io/api/metadata-block The `@name` key is required and can be localized. It defines the script's name shown in the script list and menus. Use `@name:localeCode` for localized names. ```javascript // @name Violentmonkey Script // @name:zh-CN 暴力猴脚本 ``` -------------------------------- ### Enable Top-Level Await Source: https://violentmonkey.github.io/api/metadata-block The `// @top-level-await` metadata block enables the use of `await` at the top level of your script. This is useful for asynchronous operations that your script depends on, such as waiting for DOM elements or fetching network data. Avoid using it for importing dependencies that should always run; prefer `@require` and `@resource` instead. It cannot be used with `@unwrap`. ```javascript // @top-level-await ``` -------------------------------- ### Require External Script Source: https://violentmonkey.github.io/api/metadata-block The `@require` key allows specifying a URL to another script that must be executed before the current one. Local files are not permitted. ```javascript // @require https://my.cdn.com/jquery.js ``` -------------------------------- ### Observe DOM Elements with @violentmonkey/dom Source: https://violentmonkey.github.io/guide/observing-dom Use VM.observe to detect and manipulate dynamically created DOM elements. Return true from the callback to automatically disconnect the observer after the target node is found and processed. ```javascript const disconnect = VM.observe(document.body, () => { // Find the target node const node = document.querySelector('.profile'); if (node) { const h1 = document.createElement('h1'); h1.textContent = 'Profile'; node.prepend(h1); // disconnect observer return true; } }); // You can also disconnect the observer explicitly when it's not used any more disconnect(); ``` -------------------------------- ### GM.xmlHttpRequest Basic Usage Source: https://violentmonkey.github.io/api/gm Use this for basic cross-origin HTTP requests compatible with multiple userscript managers. The `onload` callback handles the response. ```javascript // compatible with multiple userscript managers GM.xmlHttpRequest({ url, onload: res => {/*....*/} }); ``` -------------------------------- ### Set System Clipboard Data Source: https://violentmonkey.github.io/api/gm Sets data to the system clipboard with an optional MIME type. ```javascript GM_setClipboard(data, type) ``` -------------------------------- ### GM_info Source: https://violentmonkey.github.io/api/gm Provides information about the script's environment, including platform, a unique script ID, and the Violent Monkey version. ```APIDOC ## GM_info ### Description Provides information about the script's environment. ### Properties - **platform** (string): The current platform. - **getHighEntropyValues** (function): A function that returns high entropy values. It accepts an array of strings as hints and returns a Promise resolving to UADataValues. - **uuid** (string): A unique ID of the script. - **version** (string): The version of Violent Monkey. ``` -------------------------------- ### GM.xmlHttpRequest Async/Await (VM2.18.3+) Source: https://violentmonkey.github.io/api/gm For Violentmonkey version 2.18.3 and later, and Tampermonkey, you can use `await` directly with GM.xmlHttpRequest for simplified asynchronous operations. ```javascript // VM2.18.3+, TM const res = await GM.xmlHttpRequest({ url }); ``` -------------------------------- ### GM.listValues Source: https://violentmonkey.github.io/api/gm Asynchronously lists all stored values. ```APIDOC ## GM.listValues ### Description Asynchronously lists all stored values. ### Method `GM.listValues` (async) ``` -------------------------------- ### GM.xmlHttpRequest Source: https://violentmonkey.github.io/api/gm Makes an HTTP request. ```APIDOC ## GM.xmlHttpRequest ### Description Makes an HTTP request. This function is asynchronous and returns a Promise in newer versions. ### Method `GM.xmlHttpRequest` (async since VM2.18.3) ### Request Example ```javascript // compatible with multiple userscript managers GM.xmlHttpRequest({ url, onload: res => {/*....*/} }); ``` ### Response Example ```javascript // compatible with multiple userscript managers const res = await new Promise((resolve, reject) => { GM.xmlHttpRequest({ url, onload: resolve, onerror: reject }); }); ``` ### Response Example (VM2.18.3+, TM) ```javascript // VM2.18.3+, TM const res = await GM.xmlHttpRequest({ url }); ``` ### Response Example (Abort Control) ```javascript // VM2.18.3+, TM const control = GM.xmlHttpRequest({ url }); myButton.addEventListener('click', control.abort, { once: true }); const res = await control; ``` ``` -------------------------------- ### GM.setValues Source: https://violentmonkey.github.io/api/gm Asynchronously stores multiple key-value pairs. ```APIDOC ## GM.setValues ### Description Asynchronously stores multiple key-value pairs. ### Method `GM.setValues` (async) ``` -------------------------------- ### GM.addStyle Source: https://violentmonkey.github.io/api/gm Adds CSS styles to the document. ```APIDOC ## GM.addStyle ### Description Adds CSS styles to the document. ### Method `GM.addStyle` ``` -------------------------------- ### Specify Homepage URL - @homepageURL Source: https://violentmonkey.github.io/api/metadata-block The @homepageURL metadata key provides a link to the script's homepage, displayed via an icon in the user scripts list. ```javascript // @homepageURL https://violentmonkey.github.io/ ``` -------------------------------- ### GM_addStyle Source: https://violentmonkey.github.io/api/gm Appends and returns a `