### 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('