[toc]

什么是 userscript

按照 https://greasyfork.org/en 网站的描述:

User scripts put you in control of your browsing experience. Once installed, they automatically make the sites you visit better by adding features, making them easier to use, or taking out the annoying bits. The user scripts on Greasy Fork were written by other users and posted to share with the world. They’re free to install and easy to use.

简单理解,就是可以控制浏览器按照你自己的要求呈现内容。常见的脚本功能比如去广告,或者增强功能。

user script manager

如果需要在浏览器使用 user script,需要安装 userscript manager,比如 Chrome 可以安装 Tampermonkey

如何编写自己的 userscript

在 Tampermonkey 扩展上菜单选择「Create a new script」,出现如下所示的代码片段,就可以开始编写了。

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

(function() {
    'use strict';

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

有几点说明

  1. 语法就是 javascript
  2. 脚本将会在页面加载完毕后开始执行