# 脚本构成:

https://violentmonkey.github.io/guide/creating-a-userscript/

metadata and program.即元数据和代码

元数据API文档 (opens new window)

// ==UserScript==
// @name        New script
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       none
// @version     1.0
// @author      -
// @description 3/8/2020, 8:42:28 PM
// ==/UserScript==
1
2
3
4
5
6
7
8
9

脚本编写教程 (opens new window)

@match / @exclude-match

定义规则来决定是否应该执行脚本。建议使用@match代替@include。

查看有关匹配的更多信息 (opens new window)。

@include / @exclude

决定是否应该执行脚本的旧方法。

查看有关匹配的更多信息 (opens new window)。

https://link.segmentfault.com/?enc=4%2BfmPed%2Fq7R3JMkE4kmkKg%3D%3D.J1evnHMSrwF0OjzRhtUBK0lfrwWp%2Bus7I%2FO%2Fs4wtqo3jRuP1vtwf1s2y21FNsGkfS811O2EOA%2Fv3cBntauspQA%3D%3D)

以下是一个油猴脚本的基本模板:

// ==UserScript==
// @name         脚本名称
// @namespace    脚本命名空间
// @version      脚本版本号
// @description  脚本描述
// @author       作者姓名
// @match        匹配的网址
// @grant        需要的权限
// ==/UserScript==

// 在这里编写你的脚本代码

(function() {
    'use strict';

    // 脚本逻辑代码

})();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 油猴脚本等待dom加载完毕再执行

DOMContentLoaded事件 (opens new window)

document.addEventListener('DOMContentLoaded', function (event) {
  try {
    unsafeWindow.PageData.user.is_login = 1;
  } catch (error) {
    //alert(error);
  }
}, true);
1
2
3
4
5
6
7

demo

// ==UserScript==
// @name        樱花去广告
// @namespace   Violentmonkey Scripts
// @match       http://m.yinghuacd.com/show/*.html
// @grant       none
// @version     1.0
// @author      -
// @description 2022/9/30 14:39:53
// ==/UserScript==

function removeBanner() {
  const element = document.querySelector('div[id*=sjtb_div]')
  element.style.display = 'none'
}

document.addEventListener('load', function(e) {
  try {
    removeBanner()
  } catch (error) {
    console.log('error: ', error);
  }
}, true)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 油猴脚本监听元素生成

// ==UserScript==
// @name         监听特定元素生成示例
// @namespace    http://your-namespace.com
// @version      1.0
// @description  示例:监听特定元素生成并执行操作
// @author       Your Name
// @match        https://example.com/*  // 匹配你想要脚本运行的网页
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 获取要监听的目标元素
    const targetElement = document.getElementById('market-selling-list');

    if (!targetElement) {
        console.error('目标元素不存在');
        return;
    }

    // 创建一个 MutationObserver 监听器
    const observer = new MutationObserver(function(mutationsList, observer) {
        // 在每次 DOM 变化时调用此函数
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                // 检查是否有元素被添加到 DOM 中
                for (let addedNode of mutation.addedNodes) {
                    // 在这里执行你的操作,例如:
                    if (addedNode instanceof HTMLElement) {
                        console.log('新元素已生成:', addedNode);
                        // 在这里执行你的自定义操作
                        // 例如,可以添加事件监听器、修改元素属性等
                    }
                }
            }
        }
    });

    // 配置 MutationObserver 监听哪些 DOM 变化
    const config = { childList: true, subtree: true };

    // 开始监听目标元素
    observer.observe(targetElement, config);
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45