前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >使用 TamperMonkey 增强生产力

使用 TamperMonkey 增强生产力

作者头像
Dylan Liu
发布于 2022-09-07 06:21:54
发布于 2022-09-07 06:21:54
1K00
代码可运行
举报
文章被收录于专栏:dylanliudylanliu
运行总次数:0
代码可运行

简介

技术人员的日常积累其中的一部分就是总结不同的使用工具。现在各种软件都提供网站形式,在网站场景里,Javascript 是统治语言。TamperMonkey 提供了一种在网站上运行自己脚本的一种方式,应该成为我们工具箱里的一种常用工具。

安装

Tamper Monkey 是一个浏览器插件,可以使用在 Tamper 首页 https://www.tampermonkey.net/ 跳转到安装界面。

语法

语法文档: https://www.tampermonkey.net/documentation.php

新建脚本模版

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @match        http*://*/*
// @connect *
// @require https://code.jquery.com/jquery-2.1.4.min.js
// @run-at       document-idle
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @grant        GM_openInTab
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
})();

Meta 信息

@name, @namespace, @version, @description, @author, @icon 提供脚本的基本信息。

@match

@match 规定了脚本在什么网站上运行,http*://*/* 表明运行在所有的网站上,* 可用作通配符。 @match 的网站格式需要符合<url-pattern>的格式

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<url-pattern> := <scheme>://<host><path>
<scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'urn'
<host> := '*' | '*.' <any char except '/' and '*'>+
<path> := '/' <any chars>

@connect

Tamper Monkey 支持 Ajax 调用函数 GM_xmlhttpRequest,@connect 指定了 ajax 可以调用的网址,* 表示可以调用任何网站,但会请求用户的确认,在不确定调用的网站是比较有用。 connect 规则:

  • domains like tampermonkey.net (this will also allow all sub-domains)
  • sub-domains i.e. safari.tampermonkey.net
  • self to whitelist the domain the script is currently running at
  • localhost to access the localhost
  • 1.2.3.4 to connect to an IP address

@require

加载脚本的依赖,像 jQuery 是比较常用的。

@run-at

定义脚本的运行时机,支持 document-start, document-body, document-end, document-idle(default), context-menu

@grant

@grant 用于给 脚本添加一下GM_* 函数, unsafeWindow 对象权限。 unsafeWindow 是当前页面的 JS window 对象,可以引用到所有的全局变量。 GM 函数列表

函数名

功能

GM_addStyle(css)

加载 css

GM_setValue(name, value)

保存一个值

GM_getValue(name, defaultValue)

获取key

GM_deleteValue(name)

删除一个值

GM_listValues()

列出所有的 keys

GM_log(message)

打印日志

GM_openInTab(url, options)

打开一个新tab

GM_xmlhttpRequest(details)

ajax 调用

GM_setClipboard(data, info)

保存data到剪贴板上,info 格式 "{ type: 'text', mimetype: 'text/plain'}"

GM_openInTab 参数

options 参数

  • active decides whether the new tab should be focused,
  • insert that inserts the new tab after the current one,
  • setParent makes the browser re-focus the current tab on close and
  • incognito makes the tab being opened inside a incognito mode/private mode window.
GM_xmlhttpRequest(details) 参数
  • method one of GET, HEAD, POST
  • url the destination URL
  • headers ie. user-agent, referer, ... (some special headers are not supported by Safari and Android browsers)
  • data some string to send via a POST request
  • cookie a cookie to be patched into the sent cookie set
  • binary send the data string in binary mode
  • nocache don't cache the resource
  • revalidate revalidate maybe cached content
  • timeout a timeout in ms
  • context a property which will be added to the response object
  • responseType one of arraybuffer, blob, json or stream
  • overrideMimeType a MIME type for the request
  • anonymous don't send cookies with the requests (please see the fetch notes)
  • fetch (beta) use a fetch instead of a xhr request (at Chrome this causes details.timeout and xhr.onprogress to not work and makes xhr.onreadystatechange receive only readyState 4 events)
  • user a user name for authentication
  • password a password
  • onabort callback to be executed if the request was aborted
  • onerror callback to be executed if the request ended up with an error
  • onloadstart callback to be executed on load start, provides access to the stream object if responseType is set to "stream"
  • onprogress callback to be executed if the request made some progress
  • onreadystatechange callback to be executed if the request's ready state changed
  • ontimeout callback to be executed if the request failed due to a timeout
  • onload callback to be executed if the request was loaded. It gets one argument with the following attributes:
    • finalUrl - the final URL after all redirects from where the data was loaded
    • readyState - the ready state
    • status - the request status
    • statusText - the request status text
    • responseHeaders - the request response headers
    • response - the response data as object if details.responseType was set
    • responseXML - the response data as XML document
    • responseText - the response data as plain string

Returns an object with the following property:

  • abort - function to be called to cancel this request

基本模版:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
GM_xmlhttpRequest({
    method: "GET",
    url: "https://example.com",
    timeout: 5000,
    ontimeout: () => reject(' timeout'),
    onload: function (response) {
        if (response.status === 200) {
            // do sth
            resolve(response);
        }
        reject("Error occurred while retrieving data");
    },
    onerror: function (response) {
        reject("Error occurred while retrieving data");
    }
});
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
最新Tampermonkey 中文文档解析(附基础案例和高级案例)
@homepage, @homepageURL, @website and @source
拿我格子衫来
2022/01/24
5.7K0
使用 Tampermonkey 编写高级跨网站自动化任务脚本
为了照顾读者中一部分对 Tampermonkey(国内成油猴,以下都简称 TM)不熟悉的读者,这里我借助官方对 TM 的介绍和教程帮助入门用户做以下介绍。
拿我格子衫来
2022/01/24
5.4K0
使用 Tampermonkey 编写高级跨网站自动化任务脚本
Tampermonkey的安装与使用
Tampermonkey 是一款免费的浏览器扩展和最为流行的用户脚本管理器,虽然有些受支持的浏览器拥有原生的用户脚本支持,但 Tampermonkey 将在您的用户脚本管理方面提供更多的便利。 它提供了诸如便捷脚本安装、自动更新检查、标签中的脚本运行状况速览、内置的编辑器等众多功能, 同时Tampermonkey还有可能正常运行原本并不兼容的脚本。
PayneWu
2021/06/10
2.4K0
Tampermonkey的安装与使用
Tampermonkey 高级API的使用 附Demo
如果我们要做更多 这就需要使用Tamermokey提供的应用程序接口, 即高级API
拿我格子衫来
2022/01/24
1.9K0
油猴脚本编写教程
油猴脚本(Tampermonkey)是一个非常流行的浏览器扩展,它可以运行由广大社区编写的扩展脚本,来实现各式各样的功能,常见的去广告、修改样式文件、甚至是下载视频。今天我们就来看看如何编写自己的油猴脚本。当然为了运行油猴脚本,你应该在浏览器中安装油猴插件。
乐百川
2020/02/18
7.3K0
油猴脚本编写教程
如何通过 Tampermonkey 快速查找 JavaScript 加密入口
在很多情况下,我们可能想要在网页中自动执行某些代码,帮助我们完成一些操作。如自动抢票、自动刷单、自动爬虫等等,这些操作绝大部分都是借助 JavaScript 来实现的。那么问题来了?在浏览器里面怎样才能方便地执行我们所期望执行的 JavaScript 代码呢?在这里推荐一个插件,叫做 Tampermonkey。这个插件的功能非常强大,利用它我们几乎可以在网页中执行任何 JavaScript 代码,实现我们想要的功能。
崔庆才
2020/01/02
2.5K0
国培计划油猴脚本
国培计划油猴脚本 功能 跳过确认按钮,一直学呀学 视频0.1倍速 老师培训网址http://study.teacheredu.cn // ==UserScript== // @name 2021教师能力提升 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 跳过确认按钮,一直学呀学 // @AuThor bhl // @match http://study.
纯情
2023/04/26
6740
5 分钟,教你从零快速编写一个油猴脚本!
Tampermonkey,又称 Greasemonkey 油猴脚本,是一款免费的浏览器扩展,可用于管理用户脚本,它本质上是对浏览器接口的二次封装
AirPython
2022/05/22
3.3K0
5 分钟,教你从零快速编写一个油猴脚本!
油猴脚本入坑指南
即每个油猴脚本都有的,脚本开头很多行注释的内容,这是油猴脚本关键的基础部分,刚开始接触可能会一头雾水,但你绝不能忽视这部分内容
子润先生
2021/06/09
4.5K0
油猴脚本重写fetch和xhr请求
写过几个油猴脚本,经常对页面请求返回的数据进行拦截或者覆盖,这篇文章就做个总结,涉及到 fetch 和 xhr 两种类型的请求。
windliang
2022/09/27
3.9K0
油猴脚本重写fetch和xhr请求
CTT: CSDN文章迁移到头条自动化脚本
油猴脚本,如果有用请点赞收藏,关注此专栏。谢谢。 // ==UserScript== // @name CTT // @namespace https://fizzz.blog.csdn.net/ // @version 0.1 // @description try to take over the world! // @author Fizz // @match https://**.csdn.net/**/** // @match
拿我格子衫来
2022/01/24
5010
CTT: CSDN文章迁移到头条自动化脚本
使用Tampermonkey(油猴) 插件,重新实现了,百度搜索热点过滤功能
    昨天晚上,花了点时间学习了Chrome插件的制作方法,并书写了《Chrome 百度搜索热点过滤插件 - 开源软件》这一文章,简单地介绍自己实现的百度搜索热点过滤神器的原理和使用方式,并进行了开源(https://github.com/yaowenxu/Hot-Search-Killer)(哈哈,很简单的代码,很羞耻得拿出去开源了...)。过滤神器的原理很简单,功能也很简单。就当是学习一次chrome插件书写吧。
西湖醋鱼
2020/12/30
1.2K1
使用Tampermonkey(油猴) 插件,重新实现了,百度搜索热点过滤功能
青骄第二课堂刷课时视频+知识竞赛浏览器油猴插件
去年分享过一次,今年又开始了,发现了一个可以刷课时的插件,之前我用的时候还是去年的版本,但是今天发现更新了,自带账号自动登录功能
纯情
2023/04/27
2.5K4
青骄第二课堂刷课时视频+知识竞赛浏览器油猴插件
自动播放传智播客课程视频
这学期还弄了个1+web的什么考核, 天天让看视频做那个作业, 打游戏的时候还要盯着时长, 回来切视频 太麻烦了, 干脆写了个脚本自动帮我切换, 如果有习题就会播放语音提醒 (一点小提示, 可以配合tampermonkey的H5播放器控制来实现16倍速播放, 畅享极致丝滑, 几秒一个视频, 我也是听我朋友说的传智不计观看视频时长, 如果计视频观看时长给分数的话就GG了, 酌情使用)
NothAmor
2022/06/08
2.3K0
从零实现的浏览器Web脚本
在之前我们介绍了从零实现Chrome扩展,而实际上浏览器级别的扩展整体架构非常复杂,尽管当前有统一规范但不同浏览器的具体实现不尽相同,并且成为开发者并上架Chrome应用商店需要支付5$的注册费,如果我们只是希望在Web页面中进行一些轻量级的脚本编写,使用浏览器扩展级别的能力会显得成本略高,所以在本文我们主要探讨浏览器Web级别的轻量级脚本实现。
WindRunnerMax
2023/11/04
8950
第 26 期 - 子舒周刊 23/11/09
2023 年 11 月 2 日,Fndroid/clash_for_windows_pkg 作者由于某些原因,删除仓库中的 releases 包。
子舒
2023/11/17
4340
第 26 期 - 子舒周刊 23/11/09
ChatGPT - 让ChatGPT更持久
https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo
小小工匠
2023/05/01
6681
ChatGPT - 让ChatGPT更持久
JS逆向快速定位关键点之9大通用hook脚本
大部分网站都会对关键参数进行加密,JS 逆向时,我们首要任务是定位参数具体的加密逻辑。
Python兴趣圈
2023/11/10
3.6K0
JS逆向快速定位关键点之9大通用hook脚本
浏览器用户脚本—打造自己的专属页面
一段用户脚本就是一个程序,通常用JavaScript语言来写,用于修改web页面以提升浏览体验。通常通过浏览器的用户脚本管理插件来开启,例如Tampermonkey、Greasemonkey等。
俗可耐
2018/11/07
5.5K2
浏览器用户脚本—打造自己的专属页面
油猴脚本:markdown生成带网页标题的链接
你好,我是喵喵侠。在日常浏览网页和编写Markdown文档时,我们常常需要将网页链接插入到Markdown文档中,并附上网页的标题。然而,手动复制链接和标题不仅耗时,而且容易出错。为了解决这个问题,我们可以编写一个油猴脚本(Tampermonkey Script),自动生成带网页标题的Markdown格式链接。本文将详细介绍如何实现这一功能。
喵喵侠
2024/08/03
3450
油猴脚本:markdown生成带网页标题的链接
相关推荐
最新Tampermonkey 中文文档解析(附基础案例和高级案例)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验