The official documentation address: www.tampermonkey.net/documentati…

General Data Protection Regulation (GDPR)

User script Header

@name

The name of the script

@namesapce

Script namespace

@include

Sets the page in which the script can be run, allowing multiple tags to be set. @include does not support URL hash arguments.

// @include http://123.com/*
// @include https://123.com/*
// @include https://*
Copy the code

@match

Like the @include tag, you can set more than one.

// @match http*://
Copy the code

@exclude

Excludes urls that do not run scripts on these pages, even if the address is included in the @include or @match tag. Multiple Settings are allowed.

@require

Represents the JavaScript file that needs to be loaded and run before the script can be run. Multiple Settings are allowed. Note: If the loaded script uses strict mode, user scripts may also be affected by strict mode

/ / / / @ the require https://code.jquery.com/jquery-2.1.4.min.js @ the require https://code.jquery.com/jquery-2.1.3.min.js#sha256=23456.../ / @ the require https://code.jquery.com/jquery-2.1.2.min.js#md5=34567... ,sha256=6789..
Copy the code

@resource

Define resource files that need to be preloaded. These resources can be accessed in the script via GM_getResourceURL, GM_getResourceText. Multiple Settings are allowed.

// @resource icon2 /images/icon.png
// @resource html http://www.tampermonkey.net/index.html
// @resource xml http://www.tampermonkey.net/crx/tampermonkey.xml
// @resource SRIsecured1 http://www.tampermonkey.net/favicon.ico#md5=123434...
Copy the code

@connect

Sets the domain name (including subdomain name) that is allowed to be accessed over the GM_xmlhttpRequest connection.

// @connect *
// @connect *://*.qidian.com/
Copy the code

The @connect tag allows setting values:

  • The domain name, such astampermonkey.net, all subdomain names under the domain name are allowed to access
  • Subdomain, such assafari.tampermonkey.net
  • selfDomain name that the script is currently running
  • localhost
  • 2.IP address allowed for connection
  • *All domain names

@run-at

Set the time to inject the script. @run-at defines the first possible moment a script wants to run.

  • @run-at document-start The script will be injected as fast as possible.
  • @run-at document-body The script will be injected if the body element exists.
  • @run-at document-end The script will be injected when or after the DOMContentLoaded event was dispatched.
  • @run-at document-idleThe script will be injected after the DOMContentLoaded event was dispatched. This is the default value if no @run-at tag is given.
  • @run-at content-menu The script will be injected if it is clicked at the browser context menu (desktop Chrome-based browsers only).

@grant

The @grant tag is used to whitelist GM_* methods, unsafeWindow objects, and window object methods. If no @grant tag is given TM guesses the scripts needs.

// @grant GM_setValue
// @grant GM_getValue
// @grant GM_setClipboard
// @grant unsafeWindow
// @grant window.close
// @grant window.focus
Copy the code

API

unsafeWindow

Access the page’s JS methods and variables through the unsafeWindow object

Subresource Integrity

The @require @resource tag sets the hash part of the URL

/ / @ the require https://code.jquery.com/jquery-2.1.1.min.js#md5=45eef.../ / @ the require https://code.jquery.com/jquery-2.1.2.min.js#md5=ac56d... ,sha256=6e789.
Copy the code

GM_addStyle(css)

Adds the given style to the document and returns the injected style element.

GM_deleteValue(name)

Deletes ‘name’ from storage.

GM_listValues()

List all names of the storage.

GM_addValueChangeListener(name, function(name, old_value, new_value, remote) {})

Add listener to variable stored in storage, return listener ID. The name argument is the name of the variable to listen on

GM_removeValueChangeListener(listener_id)

Removing listeners

GM_setValue(name, value)

Set the value of ‘name’ to the storage.

GM_getValue(name, defaultValue)

Fetch ‘name’ from storage

GM_log(message)

Console Output Logs

GM_getResourceText(name)

Gets the content predefined in the header of the script with the @Resource tag

GM_getResourceURL(name)

Gets the Base64-encoded URI predefined with the @Resource tag in the script header

GM_registerMenuCommand(name, fn, accessKey)

Register the new menu in the Tampermonkey menu of the script run page and return to the menu Command ID

GM_unregisterMenuCommand(menuCmdId)

Unregister the menu registered with GM_registerMenuCommand

GM_openInTab(url, options), GM_openInTab(url, loadInBackground)

Open the URL in a new TAB. Options Optional values:

  • activeDefines whether the focus is on the new TAB
  • insert
  • setParent

GM_xmlhttpRequest(details)

Make an xmlHttpRequest.

GM_download(details), GM_download(url, name)

Download the URL specified resource to the local disk

Details can have the following attributes:

  • url– Download address (required)
  • name– File name – Need to whitelist the file extension on the Tampermonkey configuration page for security reasons (for security reasons the file extension needs to be whitelisted at Tampermonkey’s options page) (Required)
  • headers– seeGM_xmlhttpRequest
  • saveAsbooleanThe Save as dialog box is displayed
  • onerror– Download failed callback
  • onload– Download complete callback
  • onprogressCallbacks when downloading progress changes
  • ontimeoutCallback when download fails due to timeout

The onerror callback takes the following arguments:

  • error– Cause of failure
    • not_enabled– Users cannot use the download function
    • not_whitelisted– Download file suffixes are not whitelisted
    • not_permitted – the user enabled the download feature, but did not give the downloads permission
    • not_supported – the download feature isn’t supported by the browser/version
    • not_succeeded – the download wasn’t started or failed, the details attribute may provide more information
  • detailsDetails about the error

Download extended whitelist Settings as follows:

Chrome can use Tampermonkey’s GM_download function to bypass the CSP(Content Security Policy) restrictions

GM_getTab(callback)

Get a object that is persistent as long as this tab is open.

GM_saveTab(tab)

Save the tab object to reopen it after a page unload.

GM_getTabs(callback)

Get all tab objects as a hash to communicate with other script instances.

GM_notification(details, ondone) GM_notification(text, title, image, onlick)

Displays an H5 desktop notification and/or highlights the current Tab

Details has the following features:

  • text– The text of the notification (requiredhighlightSet tofalse)
  • title– The title of the notice
  • imagePictures –
  • highlightbooleanWhether to highlight the TAB for sending notifications (not settextWhen)
  • silentbooleanWhether to play the prompt tone
  • timeouttimeoutNotifications will be hidden after the set time (0 = disabled)
  • ondone– No matter if this was triggered by a timeout or a click or the TAB was highlighted
  • onclick– Called when the user clicks on the notification

GM_setClipborad(data, info)

The parameter ‘info’ can be an object like “{type: ‘text’, mimeType: ‘text/plain’}” or just a string expressing the type (“text” or “html”)

GM_info

Get some information about scripts and GM