Browser series-oil monkey script writing

Other URL

Write a simple tutorial Grease Monkey scripts - "quality software Area" - My love crack
Grease Monkey scripting tutorial _ that is passed off -CSDN blog

The difference between oil monkey and violent monkey

Other URL

What is the difference between oil monkey and violent monkey? - Know almost

Introduction

There are three types of monkeys: Tampermonkey, Greasemonkey, and Violentmonkey.

item Tampermonkey Greasemonkey Violentmonkey
API number Very comprehensive. Much more than GM Rarely All APIs of TM and GM
compatibility well. Very bad well. But some TM
User number most. Tens/hundreds of times the VM least less.
source foreign foreign domestic

Detail

API

        There are two commonly used functions on Tampermonkey, GM_setValue() and GM_getValue(), both of which are synchronous functions; on Greasemonkey, they have become GM.setValue() and GM.getValue(), both of which are asynchronous functions. All in all, once you use any set of functions in a script, Tampermonkey and Greasemonkey are incompatible with each other, while Violentmonkey cunningly supports these four functions, perfect.

        So some script authors simply don’t use the data storage function of the script manager to store it directly in localStorage or Cookie for compatibility; but this has serious disadvantages, one is that it is opaque to users and there is a potential risk, and the other is that data is cross-scripting. I strongly oppose this approach.

        As mentioned earlier, Violentmonkey is not a panacea. Fields like GM_info.script.author are not available to Violentmonkey; not to mention Greasemonkey, the entire GM_info is disabled compared to Tampermonkey.

        In short, if you have written user scripts, you will definitely understand how comfortable Tampermonkey's API is to use. The only drawback is that it does not support a bunch of asynchronous functions at the beginning of GM. But the impact is not very big, the big deal is that you can just put a layer of asynchronous on the outside.

For users, the main thing is to consider the compatibility of scripts downloaded from the Internet:

  1. Greasemonkey should go to the trash.
  2. Violentmonkey has the best theoretical compatibility, but in fact, running some scripts based on Tampermonkey will cause problems. The key is that Tampermonkey has the largest user base, tens or even hundreds of times that of Violentmonkey, so it seems that Violentmonkey may have more incompatible scripts than Tampermonkey. Moreover, the management interface is too ugly, and ICON is even uglier and unbearable. But supporting search scripts that support the current page is probably good for Xiaobai, right?
  3. Tampermonkey is currently the absolute overlord (from the perspective of user base, Greasymonkey does not even have a Chromium version, so don't touch porcelain), there seems to be no reason not to choose.

Metadata

Other URL

https://www.tampermonkey.net/documentation.php

Introduction

The metadata block is a user script part that describes the script. It usually contains the script name, namespace, description, and inclusion and exclusion rules. The metadata block appears in JavaScript line comments and may appear anywhere within the script, but it is usually near the top of the file.

The metadata block must follow the following format:

// ==UserScript==
// @key value
// ==/UserScript==

Metadata

key

Remarks

Example

@name

The name of the script. This item will be displayed in the title of the page and the content of the link. It is required .

//  @name   script name

@namespace

@namespace  and @name these two attributes will help the user script manager to determine whether the script has been installed.

 

@description

The description of the script function, displayed under the script title, is required .

// @description script function description

@version

The version tag of the script will use the Mozilla version format and be displayed on the introduction page of the script. It is required .

// @version  0.0.1

@author Author  

@match

@include
@exclude

Describe the page on which the script will execute. The list will be analyzed and displayed on the introduction page of the script, and used for script classification.

@Match is recommended , see: Match patterns-Chrome Developers

// @match  *://www.52pojie.cn/*

// @match   *://*/*

It doesn’t work like this: // @match *

@require

Reference an external script to your script

// @require http://cdn.bootcss.com/jquery.min.js

@run-at When to run. // @run-at      document-start

@resource

Preloaded resources (used by GM_getResourceURL and GM_getResourceText)  

@connect

The domain/subdomain is defined and can be obtained by GM_xmlhttpRequest.  

@updateURL
@installURL,

@downloadURL

Tell the user where the script manager should get script updates.

 

@license

The name or address of the license agreement used by the script. The agreement shall include whether the user is allowed to distribute or modify the script twice. Failure to provide a license agreement means that the user only allows personal use and no secondary distribution; the agreement will be displayed on the introduction page of the script.

 

@supportURL

用户可获得该脚本技术支持的链接地址 (如:错误反馈系统、论坛、电子邮件),该链接将显示在脚本的反馈页面。

 

@contributionURL

用于捐赠脚本作者的链接,该链接将显示在脚本的反馈页面。

 

@contributionAmount

建议捐赠金额,请配合 @contributionURL 使用。

 

@compatible

标记此脚本与某个浏览器兼容,兼容性信息将显示在脚本的简介页面上。

 

@incompatible

标记此脚本与某个浏览器不兼容,兼容性信息将显示在脚本的简介页面上。

 

@grant

将这些加入白名单:GM_* 功能, unsafeWindow 对象和一些强大的 window 功能。如果不使用这个标签,默认如下:

// @grant GM_setValue
// @grant GM_getValue
// @grant GM_setClipboard
// @grant unsafeWindow
// @grant window.close
// @grant window.focus
// @grant window.onurlchange

 

@antifeature

表示是否通过此脚本赚钱。  

@noframes

脚本运行于主页,但不在iframes  

@nocompat

用于关闭写在GM上的脚本的优化。  

@homepage

@homepageURL

@website

@source

与本脚本的名字相关联的主页。

注意:如果@namespace的tag是“http://”开头的,它的内容也会被用到这里。

 

@icon

@iconURL

@defaulticon

   

@icon64

@icon64URL

   

在生成脚本时,默认是这样的

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

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

编程接口

其他网址

https://www.tampermonkey.net/documentation.php

大全 

unsafeWindow

Subresource Integrity

GM_addStyle(css)

GM_addElement(tag_name, attributes),GM_addElement(parent_node, tag_name, attributes)

GM_deleteValue(name)GM_listValues()

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

GM_removeValueChangeListener(listener_id)

GM_setValue(name, value)

GM_getValue(name, defaultValue)

GM_log(message)

GM_getResourceText(name)

GM_getResourceURL(name)

GM_registerMenuCommand(name, fn, accessKey)

GM_unregisterMenuCommand(menuCmdId)

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

GM_xmlhttpRequest(details)

GM_download(details), GM_download(url, name)

GM_getTab(callback)GM_saveTab(tab)

GM_getTabs(callback)

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

GM_setClipboard(data, info)

GM_info

<><![CDATA[your_text_here]]></>

调试脚本

        编写脚本很难一次成功,大部分时间都花在了调试上面。调试油猴脚本的话有几种调试方法。

        第一种方法就是最原始的打印日志,可以利用console.log和GM_log来将关键信息打印出来,上面的脚本就是我靠打印日志一点点发现各种参数错误的。说实话这种办法有点笨。

        第二种就是利用浏览器的调试功能,在脚本需要调试的地方插入debugger;语句,然后在打开F12开发者工具的情况下刷新页面,就会发现网页已经暂停在相应位置上。这样就可以利用F12开发者工具进行单步调试、监视变量等操作了。

延时函数

其他网址

油猴脚本编写规则_Senreme-CSDN博客

 

 

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/114235964