去除leetcode网页顶部中文站点的广告

脚本描述

最近准备秋招一直在刷leetcode,不过leetcode网页顶部一直有一个中文站点的广告。

这个东西除了影响视线,还会在使用playground的时候挤占自定义输入的空间,如下图。于是就想写个脚本把这个去掉。

定位问题

简单研究了一下油猴的自定义脚本的开发方法。
点击chrome的油猴插件,选择添加新脚本...就可以了。

返回leetcode网页,右键广告中的叉叉,选择检查元素,找到关闭按键的标签。这样就可以得到buttonclass名。

1
2
3
<button class="btn btn-link pull-right close-btn" onclick="closeRegion()">
<i class="fa fa-times-circle" aria-hidden="true"></i>
</button>

解决方法

Talk is cheap.代码如下,只用一行解决问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ==UserScript==
// @name 去LeetCode 中文版广告
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match *://*.leetcode.com/*
// @match *://leetcode.com/*
// @grant none
// ==/UserScript==


(function() {
'use strict';
$(".fa.fa-times-circle").click();
//fa.fa-times-circle
//btn.btn-link.pull-right.close-btn
})();

header中是一些基本信息,主要就是@match比较有用,定义脚本在哪个网站上运行。
使用$()方法得到buttonclass,然后click()就可以了。关于$()方法:

$() 方法是在DOM中使用过于频繁的 document.getElementById() 方法的一个便利的简写,就像这个DOM方法一样,这个方法返回参数传入的id的那个元素。

脚本我已经发到GreasyFork上了,点我。以后再也不用看这个广告啦。