官方文档地址: www.tampermonkey.net/documentati…php
GDPR 通常数据保护条例(General Data Protection Regulation,简称GDPR)css
@name
脚本名称html
@namesapce
脚本命名空间jquery
@include
设置脚本在哪些网页中能够运行,容许设置多个标签。 @include
不支持URL hash参数。express
// @include http://123.com/*
// @include https://123.com/*
// @include https://*
复制代码
@match
与 @include
标签相似,容许设置多个。安全
// @match http*://
复制代码
@exclude
排除的URL, 在这些页面不运行脚本, 即便地址包含在 @include
或@match
标签内。容许设置多个。bash
@require
表示在运行脚本前须要加载和运行的JavaScript文件。容许设置多个。 注:若是加载的脚本使用use strict
模式,用户脚本可能也会受严格模式影响ide
// @require https://code.jquery.com/jquery-2.1.4.min.js
// @require https://code.jquery.com/jquery-2.1.3.min.js#sha256=23456...
// @require https://code.jquery.com/jquery-2.1.2.min.js#md5=34567...,sha256=6789..
复制代码
@resource
定义一些须要预加载的资源文件,这些资源能够在脚本中经过GM_getResourceURL
,GM_getResourceText
访问。容许设置多个。函数
// @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...
复制代码
@connect
设置容许经过GM_xmlhttpRequest
链接访问的域名(包括子域名)。ui
// @connect *
// @connect *://*.qidian.com/
复制代码
@connect
标签容许设置的值:
tampermonkey.net
, 设置后该域名下的全部子域名都是容许访问的safari.tampermonkey.net
self
当前脚本正在运行的域名localhost
1.2.3.4
容许链接的IP地址*
全部域名@run-at
设置注入脚本的时间。@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-idle
The 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
@grant
标签用于设置GM_*
方法, unsafeWindow
对象, window
对象方法的白名单。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
复制代码
unsafeWindow
经过unsafeWindow
对象访问页面的js方法和变量
Subresource Integrity
@require``@resource
标签设置URL的hash部分
// @require https://code.jquery.com/jquery-2.1.1.min.js#md5=45eef...
// @require https://code.jquery.com/jquery-2.1.2.min.js#md5=ac56d...,sha256=6e789.
复制代码
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) {})
对storage存储的变量添加监听器,返回监听器ID。 name
参数是要监听的变量名
GM_removeValueChangeListener(listener_id)
移除监听器
GM_setValue(name, value)
Set the value of 'name' to the storage.
GM_getValue(name, defaultValue)
从storage里面获取'name'的值
GM_log(message)
控制台输出日志
GM_getResourceText(name)
获取在脚本头部用@resource
标签预约义的的内容
GM_getResourceURL(name)
获取在脚本头部用@resource
标签预约义的的base64编码的URI
GM_registerMenuCommand(name, fn, accessKey)
在脚本运行页面的Tampermonkey菜单中注册新的菜单,返回菜单command ID
GM_unregisterMenuCommand(menuCmdId)
注销用GM_registerMenuCommand
注册的菜单
GM_openInTab(url, options), GM_openInTab(url, loadInBackground)
在新标签页打开URL。options
可选的值:
active
定义焦点是否在新标签页上insert
setParent
GM_xmlhttpRequest(details)
Make an xmlHttpRequest.
GM_download(details), GM_download(url, name)
下载URL指定资源到本地磁盘
details
能够有以下属性:
url
- 下载地址 (必需)name
- 文件名 - 因为安全缘由须要在Tampermonkey的配置页把文件扩展名设为白名单(for security reasons the file extension needs to be whitelisted at Tampermonkey's options page) (必需)headers
- 参见 GM_xmlhttpRequest
saveAs
- boolean
, 弹出“保存为”的弹框onerror
- 下载失败的回调onload
- 下载完成回调onprogress
下载进度变化时的回调ontimeout
因为超时致使下载失败时的回调onerror
回调函数的参数:
error
- 失败缘由
not_enabled
- 用户不能使用下载功能not_whitelisted
- 下载文件后缀不在白名单内not_permitted
- the user enabled the download feature, but did not give the downloads permissionnot_supported
- the download feature isn't supported by the browser/versionnot_succeeded
- the download wasn't started or failed, the details attribute may provide more informationdetails
关于错误的详细信息下载扩展白名单设置以下:
Chrome 能够使用 Tampermonkey 的 GM_download 函数绕过 CSP(Content Security Policy) 的限制
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)
显示一个H5桌面通知,并/或 高亮显示当前Tab
details
有以下特性:
text
- 通知的文本 (须要 highlight
设置为false
)title
- 通知的标题image
- 图片highlight
- boolean
是否高亮发送通知的标签页 (未设置text
时)silent
- boolean
是否播放提示音timeout
- timeout
设置的时间以后通知会被隐藏 (0 = disabled)ondone
- 通知被关闭时调用 (no matter if this was triggered by a timeout or a click) or the tab was highlightedonclick
- 用户点击通知时调用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
获取关于脚本和GM的一些信息