编写一个油猴脚本

油猴(TamperMonkey)是Chrome 的一个扩展插件,使用它你可以从网上找一些方便使用的浏览器脚本,并且还可以自己写一些脚本(可以在网上阅读其他人脚本来学习)。

本人初步接触,下面是在一个简单的脚本:过滤 Bing 搜索结果的广告。

// ==UserScript==
// @name         FilterBingAds
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       Me
// @match        https://cn.bing.com/search?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    setTimeout(function(){
        if (location.origin == "https://cn.bing.com" && location.pathname == "/search") {
            var docs = document.getElementsByClassName("b_ad");
            for (var index in docs) {
                var doc = docs[index]
                doc.style.display = 'none';
            }
        }
    }, 1200);
})();

其中前面的注释是新建脚本时,自己填充的。注意:

  • 在 插件 的 Setting 中调节 Logging LevelDebug,可以开启调试模式。
  • @match 这个是 match 运行此脚本的 url 地址,在这里必须用上通配符才能。
  • 因为 bing 会在搜索结束大约 1s 后插入广告,所以我这里在打开页面 1.2s 后让它执行脚本,移除广告的显示。

你可能感兴趣的:(编写一个油猴脚本)