Tampermonkey油猴用戶腳本API文檔

官方文檔地址: www.tampermonkey.net/documentati…php

GDPR 通常數據保護條例(General Data Protection Regulation,簡稱GDPR)css

用戶腳本 Header

@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_getResourceURLGM_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
複製代碼

API

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 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
  • details 關於錯誤的詳細信息

下載擴展白名單設置以下:

下載擴展白名單

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 highlighted
  • onclick - 用戶點擊通知時調用

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的一些信息

相關文章
相關標籤/搜索